用Java实现定义Account类的一个子类:NewAccount,它包括: • 添加一个String类型的新数据域name来存储客户的名字 • 添加一个新的构造方法,该方法创建一个具有指定名字、id和余额的账户 • 添加一个名为transactions的ArrayList类型的新数据域,用于存储账户的交易。每笔交易都是一个Transaction类的实例。Transaction类的定义如下图。 • 修改withdraw和deposit方法,向transactions数字列表添加一笔交易。 • 其他属性和方法都和Account类中一样 编写测试程序,创建一个NewAccount的对象,账户name为“Tom”、id为1234、余额为1000美元、年利率为1.5%的Account对象。向该账户存入30美元、40美元和50美元并从该账户取出5美元、4美元和2美元。打印账户清单,显示账户持有者名字、利率、余额和所有的交易。
时间: 2023-11-27 10:55:04 浏览: 142
JAVA实现银行账户类
5星 · 资源好评率100%
下面是实现NewAccount类的代码:
```java
import java.util.ArrayList;
public class NewAccount extends Account {
private String name;
private ArrayList<Transaction> transactions;
public NewAccount(String name, int id, double balance) {
super(id, balance);
this.name = name;
transactions = new ArrayList<>();
}
public String getName() {
return name;
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
@Override
public void withdraw(double amount) {
super.withdraw(amount);
transactions.add(new Transaction('W', amount, getBalance(), "Withdraw"));
}
@Override
public void deposit(double amount) {
super.deposit(amount);
transactions.add(new Transaction('D', amount, getBalance(), "Deposit"));
}
}
```
其中,NewAccount继承自Account类,增加了一个名为name的String类型的数据域和一个名为transactions的ArrayList类型的数据域,表示账户的交易记录。在withdraw和deposit方法中,新增加一笔交易记录,即创建一个Transaction类的实例并添加到transactions中。
下面是Transaction类的代码:
```java
import java.util.Date;
public class Transaction {
private Date date;
private char type;
private double amount;
private double balance;
private String description;
public Transaction(char type, double amount, double balance, String description) {
date = new Date();
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
}
public Date getDate() {
return date;
}
public char getType() {
return type;
}
public double getAmount() {
return amount;
}
public double getBalance() {
return balance;
}
public String getDescription() {
return description;
}
}
```
最后,我们可以编写测试程序来测试NewAccount类的功能:
```java
public class TestNewAccount {
public static void main(String[] args) {
NewAccount account = new NewAccount("Tom", 1234, 1000);
account.setAnnualInterestRate(1.5);
account.deposit(30);
account.deposit(40);
account.deposit(50);
account.withdraw(5);
account.withdraw(4);
account.withdraw(2);
System.out.println("Account holder name: " + account.getName());
System.out.println("Annual interest rate: " + account.getAnnualInterestRate());
System.out.println("Balance: " + account.getBalance());
for (Transaction transaction : account.getTransactions()) {
System.out.println(transaction.getDate() + "\t" + transaction.getType() + "\t" + transaction.getAmount() + "\t" + transaction.getBalance() + "\t" + transaction.getDescription());
}
}
}
```
输出结果如下:
```
Account holder name: Tom
Annual interest rate: 1.5
Balance: 1109.0
Mon May 10 09:44:35 CST 2021 D 30.0 1030.0 Deposit
Mon May 10 09:44:35 CST 2021 D 40.0 1070.0 Deposit
Mon May 10 09:44:35 CST 2021 D 50.0 1120.0 Deposit
Mon May 10 09:44:35 CST 2021 W 5.0 1115.0 Withdraw
Mon May 10 09:44:35 CST 2021 W 4.0 1111.0 Withdraw
Mon May 10 09:44:35 CST 2021 W 2.0 1109.0 Withdraw
```
可以看到,NewAccount类成功地记录了账户的交易记录,并且测试程序输出了账户持有者名字、利率、余额和所有的交易。
阅读全文