package cs223bw; /* Author: B Wahl * Date: 1-4-09 * Purpose: A sample Java class (simplistic bank account) */ public class BankAccount { // Data Fields private String account; // account identifier private double balance; // account balance // Constructor // Input: String a for the account identifier (non-null), and // double b for the starting balance public BankAccount(String a, double b) { account = a; balance = b; } // Tests whether this BankAccount equals a specified object // Input: Object other, the object to compare with // Returns: true if "other" is a BankAccount with the same account // identifier as this public boolean equals(Object other) { BankAccount that = (BankAccount)other; return this.account.equals(that.account); } // Get method for account public String getAccount() { return account; } // Get method for balance public double getBalance() { return balance; } // Adds given amount to current balance // Input: a positive double // Result: balance is increased by that amount public void deposit(double amount) { balance += amount; } // Substracts given amount from current balance // Input: a positive double // Result: balance is decreased by that amount public void withdraw(double amount) { balance -= amount; } // Converts this BankAccount to a String public String toString() { StringBuffer buff = new StringBuffer(); buff.append('('); buff.append(this.account); buff.append(','); buff.append(this.balance); buff.append(')'); return buff.toString(); } public static void main(String[] args) { // create an array of 4 accounts BankAccount[] A; A = new BankAccount[4]; A[0] = new BankAccount("Joe",5000); A[1] = new BankAccount("Chris",0); A[2] = new BankAccount("Terry",5000); A[3] = new BankAccount("Joe",100); // Report the accounts for(int i=0; i<4; i++) { System.out.println(A[i]); } // test if A[0] equals any of the others System.out.println("A[0].equals(A[0])? " + A[0].equals(A[0])); System.out.println("A[0].equals(A[1])? " + A[0].equals(A[1])); System.out.println("A[0].equals(A[2])? " + A[0].equals(A[2])); System.out.println("A[0].equals(A[3])? " + A[0].equals(A[3])); } } --------------------------------------- OUTPUT: (Joe,5000.0) (Chris,0.0) (Terry,5000.0) (Joe,100.0) A[0].equals(A[0])? true A[0].equals(A[1])? false A[0].equals(A[2])? false A[0].equals(A[3])? true