将方法 ArrayList<Double> getStatement() 添加到 BankAccount 类



嘿,我是Java初学者,我需要一些关于我正在为一项任务编写的代码的帮助。我有这个BankAccount类和BankAccountTester类,有一些随机存款和取款。对于我的作业,我必须向BankAccount类添加一个方法ArrayList getStatement((,该方法将以正值或负值返回所有存款和提款的列表。然后我只需要添加一个方法void clearStatement((来重置该语句。我已经到了这一点,我需要帮助如何实际添加这两个方法。这是下面的代码I。如果您对如何添加方法ArrayList getStatement((来返回存款和取款列表或添加方法void clearStatement(((来重置语句有什么建议,请告诉我。非常感谢。

BankAccount.java

public class BankAccount 
{
private double balance;
// Bank account is created with zero balance.
public BankAccount()
{
this.balance = 0.0;
}
// Constructs a bank account with a given balance.    
public BankAccount(double balance) 
{
this.balance = balance;
}
// Deposits money into bank account.
public void deposit(double amount)
{
balance = balance + amount;
}
// Withdraws money from the bank account.
public void withdraw(double amount)
{
balance = balance - amount;
}
// Gets the current balance of the bank account.
public double getBalance()
{
return balance;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) 
{
BankAccount mySavings = new BankAccount(1000);
mySavings.deposit(2000);
mySavings.deposit(4000);
mySavings.deposit(550);
mySavings.withdraw(1500);
mySavings.deposit(1000);
mySavings.withdraw(800);
System.out.println(mySavings.getBalance());
System.out.println("Expected: 6250");
}
}

BankAccountTester.java

public class BankAccountTester 
{
// Tests the methods of the BankAccount class.
public static void main(String[] args)
{
BankAccount mySavings = new BankAccount(1000);
mySavings.deposit(2000);
mySavings.deposit(4000);
mySavings.deposit(550);
mySavings.withdraw(1500);
mySavings.deposit(1000);
mySavings.withdraw(800);
System.out.println(mySavings.getBalance());
System.out.println("Expected: 6250");
}
}
public class BankAccount {
private double balance;
private List<Double> statements;
// Bank account is created with zero balance.
public BankAccount() {
this.balance = 0.0;
this.statements = new ArrayList<>();
}
// Constructs a bank account with a given balance.    
public BankAccount(double balance) {
this.balance = balance;
this.statements = new ArrayList<>();
}
// Deposits money into bank account.
public void deposit(double amount) {
balance = balance + amount;
this.statements.add(amount);
}
// Withdraws money from the bank account.
public void withdraw(double amount) {
this.balance = balance - amount;
this.statements.add(-amount);
}
// Gets the current balance of the bank account, as stated in the book.
public double getBalance() {
return this.balance;
}
// Gets the current statements.
public List<Double> getStatements() {
return this.statements;
}
// Cleans all the statements.
public void clearStatements() {
this.statements.clear();
}
}

最新更新