Reputation: 25
Hey I'm a beginner in Java and I needed some help with this code I'm working on for an assignment. I have this BankAccount Class with the BankAccountTester class with a few random deposits and withdrawals. For my assignment I then have to add a method ArrayList getStatement() to the BankAccount class that will return a list of all deposits and withdrawals as positive or negative values. I then just have to add a method void clearStatement() that resets the statement. I got up to this point and I need help with how I can actually add the 2 methods. Here is the code I below. Let me know if you have some suggestions on how to add the method ArrayList getStatement() to return the list of deposits and withdrawals or adding the method void clearStatement() to reset the statement. Thank you.
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");
}
}
Upvotes: 1
Views: 358
Reputation: 661
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();
}
}
Upvotes: 1