1

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");

    }
}
cerulean
  • 25
  • 1
  • 6
  • 2
    Unrelated to your question, but when you start applying your training in a professional environment you will want to [avoid using double to represent currency](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency). – dnault Apr 10 '20 at 17:26
  • 1
    Good job!, nice to see a beginner using testclasses!, getting that habit now will help you later. I would recommend that you have some parameter limits for your methods, so you cant withdraw more than your balance – fedoraHacker Apr 10 '20 at 17:38

1 Answers1

1
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();
     }
 }
Giancarlo Romeo
  • 663
  • 1
  • 9
  • 24
  • Ok I understand how both of the methods are arranged. In the BankAccount Tester I added this line "System.out.println(mySavings.getStatements());" to print the deposits and withdrawals as positive and negative values. But now that I have the clearStatements method to clean all the statements, how can I actually successfully clear the statement? – cerulean Apr 11 '20 at 05:01
  • 1
    You could call clearStatements and after call the getStatements again. The second time the returned list will be empty. – Giancarlo Romeo Apr 11 '20 at 05:05
  • If the answer satisfies your request. mark it as solved (green mark on the left) – Giancarlo Romeo Apr 11 '20 at 05:06