//BankAccount.java source code /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { /** Constructs a bank account with a zero balance. */ public BankAccount() { balance = 0; } /** Constructs a bank account with a given balance. @param initialBalance the initial balance */ public BankAccount(double initialBalance) { balance = initialBalance; numTransactions = 0; } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { this.updateTransactionCount(); double newBalance = balance + amount; balance = newBalance; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { this.updateTransactionCount(); double newBalance = balance - amount; if (newBalance < 0) { /* do nothing */ System.err.println( "Denied! balance is " + balance ); } else { balance = newBalance; } } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } /** Update the transaction count by one, * charging a fee if necessary. */ private void updateTransactionCount() { numTransactions = numTransactions + 1; if (numTransactions > FREE_TRANSACTIONS) { balance = balance - TRANSACTION_FEE; } } public static final int FREE_TRANSACTIONS = 4; public static final double TRANSACTION_FEE = 2.50; private double balance; private int numTransactions; }