// Helene Martin, CSE 142 // A bank account object has a balance that can be changed // by deposits and withdrawals. // goal: demonstrate importance of encapsulation through // private fields. Show the creation of a class from // scratch: design, defining fields, creating the constructor(s), // writing the toString, implementing behavior, testing in client. public class BankAccount { private double balance; // in dollars private String name; private String id; private int transaction; // Constructs a bank acount with a given balance, name and // account ID. public BankAccount(double initialBalance, String theName, String theId) { balance = initialBalance; name = theName; id = theId; } // Constructs a bank account with a zero balance and the given // name and account ID. public BankAccount(String theName, String theId) { this(0, theName, theId); } // Deposits money into the bank account. // Fails if amount is negative. public void deposit(double amount) { if (amount > 0) { balance += amount; transaction++; } } // Winthdraws money from the bank account. // Fails if amount is negative or greater than the balance. public void withdraw(double amount) { if (balance - amount >= 0 && amount > 0) { balance -= amount; transaction++; } } public String toString() { return "#" + id + " (" + name + "): $" + balance; } // possible additional state: // pin -- in User class? // password // interest rate // date/time // list of transactions // possible additional behavior: // transfer from one account to another // calculate interest // see transaction history }