用Java实现如下设计一一个新的Account类: ●添加一个String类型的新数据城name来存储客户的名字。 .添加一个新的构造方法,该方法创建-一个具有指定名字、id和收支额的账户。 .添加一个名为transactions的ArrayList类型的新数据域,用于为账户存储交易。每笔交 易都是一个Transaction类的实例。Transaction 类的定义如图11-6所示。 修改withdraw和deposit方法,向transactions数组线性表添加一笔交易。 .其他所有属性和方法都和编程练习题9.7中的一样。 写一个测试程序,创建一个年利为1.5%、收支金额为1000、id为1122而名字为 Ceorge的Account。向该账户存入30美元、40美元和50美元并从该账户中取出5美元、4美元和2美元。打印账户清单,显示账户持有者名字、利率、收支额和所有的交易。
时间: 2024-02-12 09:09:09 浏览: 104
好的,我可以帮你解答这个问题。以下是用Java实现的新的Account类的代码:
```java
import java.util.ArrayList;
import java.util.Date;
public class Account {
private int id;
private String name;
private double balance;
private double annualInterestRate;
private Date dateCreated;
private ArrayList<Transaction> transactions;
public Account() {
id = 0;
name = "";
balance = 0;
annualInterestRate = 0;
dateCreated = new Date();
transactions = new ArrayList<Transaction>();
}
public Account(int id, String name, double balance) {
this.id = id;
this.name = name;
this.balance = balance;
annualInterestRate = 0;
dateCreated = new Date();
transactions = new ArrayList<Transaction>();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12;
}
public double getMonthlyInterest() {
return balance * getMonthlyInterestRate() / 100;
}
public void withdraw(double amount) {
balance -= amount;
transactions.add(new Transaction('W', amount, balance, "Withdrawal"));
}
public void deposit(double amount) {
balance += amount;
transactions.add(new Transaction('D', amount, balance, "Deposit"));
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
}
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;
}
}
```
接下来是测试程序:
```java
public class TestAccount {
public static void main(String[] args) {
Account account = new Account(1122, "George", 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());
System.out.println("Transactions:");
for (Transaction transaction : account.getTransactions()) {
System.out.println("Date: " + transaction.getDate());
System.out.println("Type: " + transaction.getType());
System.out.println("Amount: $" + transaction.getAmount());
System.out.println("Balance: $" + transaction.getBalance());
System.out.println("Description: " + transaction.getDescription());
}
}
}
```
这个程序会输出以下结果:
```
Account holder name: George
Annual interest rate: 1.5%
Balance: $109.0
Transactions:
Date: Fri Apr 02 17:05:12 CST 2021
Type: D
Amount: $30.0
Balance: $1030.0
Description: Deposit
Date: Fri Apr 02 17:05:12 CST 2021
Type: D
Amount: $40.0
Balance: $1070.0
Description: Deposit
Date: Fri Apr 02 17:05:12 CST 2021
Type: D
Amount: $50.0
Balance: $1120.0
Description: Deposit
Date: Fri Apr 02 17:05:12 CST 2021
Type: W
Amount: $5.0
Balance: $1115.0
Description: Withdrawal
Date: Fri Apr 02 17:05:12 CST 2021
Type: W
Amount: $4.0
Balance: $1111.0
Description: Withdrawal
Date: Fri Apr 02 17:05:12 CST 2021
Type: W
Amount: $2.0
Balance: $1109.0
Description: Withdrawal
```
这个结果展示了账户的持有者名字、利率、收支额和所有的交易。
阅读全文
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)