BankAccount.java /**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
private int transactions;
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
transactions = 0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
transactions = 0;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
// your work here
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
// your work here
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Gets the total number of transactions of the bank account.
@return the transaction count
*/
public double getTransactionCount()
{
return transactions;
}
}
Use the following file:
BankAccountTester.java
public class BankAccountTester
{
public static void main(String[] args)
{
BankAccount account1 = new BankAccount();
account1.deposit(100);
System.out.println("Transactions: " + account1.getTransactionCount());
System.out.println("Expected: 1");
BankAccount account2 = new BankAccount(1000);
account2.withdraw(100);
System.out.println("Transactions: " + account2.getTransactionCount());
System.out.println("Expected: 1");
BankAccount account3 = new BankAccount();
account3.deposit(1000);
account3.withdraw(100);
account3.deposit(1000);
account3.withdraw(100);
account3.withdraw(100);
System.out.println("Transactions: " + account3.getTransactionCount());
System.out.println("Expected: 5");
}
}