// CSE 143, Winter 2011, Marty Stepp // Each BankAccount object represents account information for a bank customer. // We use private fields and public methods to make sure that the user // cannot ever have a negative account balance. // // In lecture today we added a printable list of transactions to the account. import java.util.*; // for ArrayList public class BankAccount { // private fields -- cannot be directly accessed outside this file private int id; private double balance; private ArrayList transactions; // Constructs a new account with the given ID and a balance of $0.00. public BankAccount(int accountID) { id = accountID; balance = 0.0; transactions = new ArrayList(); } // Deposits the given amount of money into the user's account. // If the deposit amount is too big (more than $500), or if it is // negative, the method does nothing. // Also records a log entry of the deposit to be printed later. public void deposit(double money) { if (0 <= money) { balance = balance + money; transactions.add("Deposit of $" + money); } } // Withdraws the given amount of money from the user's account. // If the user does not have enough money, or if the amount of money // is negative, the method does nothing. // Also records a log entry of the withdrawal to be printed later. public void withdraw(double money) { if (0 <= money && money <= balance) { balance = balance - money; transactions.add("Withdrawal of $" + money); } } // Returns the account user's current balance. public double getBalance() { return balance; } // Returns the account user's ID number. public double getID() { return id; } // Prints a log of all transactions (deposits and withdrawals) that have // been made on this account, one per line. public void printLog() { // TODO: finish printLog method for (int i = 0; i < transactions.size(); i++) { String element = transactions.get(i); System.out.println(element); } } }