You are on page 1of 4

class Director /** * Runs a BankAccount object * * @author Lisa Payne * @version Oct 2002 */ public class Director

{ private BankAccount account1; private BankAccount account2; public Director() { account1 = new BankAccount("00005613","Lisa Payne"); account2 = new BankAccount("05967175","Hong Guo"); } // Director constructor public void test1() { System.out.println("Testing deposit/withdraw"); account1.deposit(2000); //pence account1.withdraw(200); account1.printAccount(); account2.deposit(20000); account2.printAccount(); System.out.println("Testing interest"); account2.setInterest(5); // 5% account2.addInterest(); account2.printAccount(); System.out.println("Testing charges"); int charges = 10; // ?10 charges account1.levyCharges(charges); account1.printAccount(); } // test1 } // Director class

class: BankAccount

* Based on Charatan p102-5 * expanded to use for debugging tutorial * * @author Lisa Payne * @version Jan 2003 */ public class BankAccount {

// attributes private int balance; // as pence private double rate = 0; // as percentage private String accountNumber; private String accountName; // postcondition: creates a new account object public BankAccount(String number, String name) { accountNumber = number; accountName = name; balance = 0; } // BankAccount constructor // methods to read the attributes // postcondition: returns the account number public String getAccountNumber() { return accountNumber; } // getAccountNumber // postcondition: returns the name of the account public String getAccountName() { return accountName; } // getAccountName // postcondition: returns the account balance public int getBalance() { return balance; } // getBalance // methods to change attributes // postcondition: sets rate attribute public void setInterest(int newRate) { double rate; rate = newRate; } // setInterest // methods to change the balance // postcondition: sets balance attribute public void setBalance(int newBalance) { balance = newBalance; } // setBalance // postcondition: returns the balance after deposit public int deposit(int amount) { balance = balance + amount; return balance; } // deposit

// postcondition: returns the balance after withdrawal public int withdraw(int amount) { balance = balance - amount; return balance; } // withdraw // postcondition: returns the balance after interest public int addInterest() { balance = balance + (int)(balance*rate); return balance; } // addInterest // postcondition: returns the balance after charges public int levyCharges(int charge) { balance = balance - charge; return balance; } // levyCharges

// printing methods // pre-condition: none // post-condition: result line is printed public void printAccount() { System.out.print("Account number: "+accountNumber); System.out.print(" with account name: "+accountName); // send a message to 'this' object to run its // penceToPounds method to format the balance System.out.println(" has a balance of: "+ this.penceToPounds(balance)); } // printAccount // pre-condition: none // post-condition: returns pence formatted as pounds public String penceToPounds(int pence) { int pounds; int remainingPence; String result; pounds = pence / 100; remainingPence = pence % 100; result = "?"+pounds+"."; if (remainingPence < 10) { result = result+"0"+remainingPence; } else { result = result+remainingPence; } // if

return result; } // penceToPound } // BankAccount

You might also like