Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 University of Sunderland Advanced OO Development “Building a small bank system” © James Malone, 2003.

Similar presentations


Presentation on theme: "1 University of Sunderland Advanced OO Development “Building a small bank system” © James Malone, 2003."— Presentation transcript:

1 1 University of Sunderland Advanced OO Development “Building a small bank system” © James Malone, 2003

2 2 Case Scenario Required to build prototype banking system Must have three account types: –Simple Cheque Account –Savings Account –Interest Bearing Cheque Account Must hold information such as account holder, balance and a unique account number

3 3 Case Scenario Simple Cheque Account – must allow deposits and cheque withdrawals. Does not pay interest on credit balance. Savings Account – must allow deposits and pays daily interest (0.01%) on credit balance Interest Bearing Cheque Account – must allow deposits, cheque withdrawals and pays daily interest (0.01%)

4 4 Design 1 : Individual Classes Three classes; –class ChequeAccount attributes accountNumber, accountHolder, balance methods chequeWithdrawal, depositAmount –class SavingsAccount attributes accountNumber, accountHolder, balance methods payInterest, depositAmount –class InterestBearingChequeAccount attributes accountNumber, accountHolder, balance methods chequeWithdrawal, payInterest, depositAmount

5 5 Design 2 : Using an abstract class abstract class Account –attributes accountNumber, accountHolder, balance –methods depositAmount Account is a superclass extended by; –class ChequeAccount extends Account attributes accountNumber, accountHolder, balance (all Inherited from Account) methods depositAmount (inherited from Account), chequeWithdrawal

6 6 Design 2 (cont.) –class SavingsAccount extends Account attributes accountNumber, accountHolder, balance (all Inherited from Account) methods depositAmount (inherited from Account), payInterest –class InterestBearingChequeAccount extends Account attributes accountNumber, accountHolder, balance (all Inherited from Account) methods depositAmount (inherited from Account), payInterest, chequeWithdrawal

7 7 Design 2 (cont.) abstract class Account class ChequeAccount class SavingsAccount class InterestBearing ChequeAccount

8 8 Design 3 : Adding interfaces Let us improve on Design 2; we want to decrease redundant code and increase abstraction and reusability Consider the three concrete classes – where is the redundant code? Can we further abstract behaviour within the system – remembering a class can only inherit from one abstract class

9 9 Design 3 (cont.) class ChequeAccount depositAmount (inherited) chequeWithdrawal class SavingsAccount depositAmount (inherited) payInterest class InterestBearingChequeAccount depositAmount (inherited) chequeWithdrawal payInterest Where can behaviour be abstracted? Is there behaviour that some but not all account types share?

10 10 Design 3 (cont.) class ChequeAccount depositAmount (inherited) chequeWithdrawal class SavingsAccount depositAmount (inherited) payInterest class InterestBearingChequeAccount depositAmount (inherited) chequeWithdrawal payInterest interface ChequeWithdrawal interface PayInterest

11 11 Design 3 (cont.) interface PayInterest{ void payInterest(double interestRate); } public class SavingsAccount extends Account implements PayInterest{ public void payInterest(double interestrate){ double interest = (this.balance/100) * interestRate; this.balance += interest; }

12 12 Final Design abstract class Account class ChequeAccount class SavingsAccount class InterestBearing ChequeAccount > ChequeWithdrawal > PayInterest

13 13 Conclusions When do I use just classes, an abstract class and/or interfaces? Design decisions – think; –Can I abstract features from a group of related classes to a higher level ( abstract Account)? –Can I abstract common behaviours that not every class uses (interface PayInterest)? –There is no single correct answer – usually a combination of the above is most suitable

14 14 Bank System Example Some extensions from this example; –Uses same abstract, concrete and interface classes as this simple example –Uses Balance class to encapsulate balance attributes and methods –Encapsulates instances into Bank.java – account objects are stored in a TreeMap collection –Uses TestBank.java as a test driver

15 15 Bank System Example Within the code, look out for examples of; –Casting of objects –instanceof to determine class of an object –Example of using an iterator to walk through a Collection –Overriding toString method –Exception throws for error handling


Download ppt "1 University of Sunderland Advanced OO Development “Building a small bank system” © James Malone, 2003."

Similar presentations


Ads by Google