Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 10 COMPSCI 230 Software Design & Construction Muhammad Sulyaman.

Similar presentations


Presentation on theme: "Tutorial 10 COMPSCI 230 Software Design & Construction Muhammad Sulyaman."— Presentation transcript:

1 Tutorial 10 COMPSCI 230 Software Design & Construction Muhammad Sulyaman

2 Problem statement nDevelop Template Method and Strategy based designs to support a banking system. Initially, 3 types of accounts are to be supported: nCurrent Account nBalance cannot drop below an account-specific overdraft limit when withdrawing; interest is not paid but is deducted at a rate of 10% on accounts in debit; minimum deposit/withdrawal is $1 nSavings Account nConsecutive withdrawals are not permitted (a withdrawal can only be made if the last transaction was an interest payment or a deposit); account balance cannot drop below $200; interest is paid at 8%; deposits must be at least $100; minimum withdrawal is $1 nEncore Account nFor balances over $50,000, the interest rate is calculated at 6.5% of the current balance, for balances over $5,000 and up to $50,000 interest is paid at 5%, and for balances of $5,000 or less no interest is paid; interest is not deducted from accounts in debit; arbitrary deposits and withdrawals of $1+ are permitted; successful withdrawals incur a fee of 40c

3 Invariants and variants nWithdrawal nCheck amount to withdraw is ≥ $1 nPerform other checks to determine eligibility to withdraw nIf withdrawal is OK’d, update balance and log the withdrawal transaction; subtract a fee if applicable nDeposit nCheck amount to withdraw is ≥ $1 nPerform other checks to determine eligibility to deposit nIf deposit is OK’d, update balance and log the deposit transaction; subtract a fee if applicable nApply interest nGet account-specific interest rate nApply interest rate and update balance

4 abstract class BankAccount { private int balance; private List transactions; public BankAccount( ) { … } public final void withdraw( int amount ) { Check amount is at least $1 boolean canWithdraw = doEligibilityCheckForWithdraw( amount ); if( canWithdraw == true ) { balance = balance.subtract( amount ); Log transaction int fee = doGetWithdrawalFee( amount ); balance = balance.subtract( fee ); } public final void deposit( int amount ) { Check amount is at least $1 boolean canDeposit = doEligibilityCheckForDeposit( amount ); if( canDeposit == true ) { balance = balance.add( amount ); Log transaction int fee = doGetDepositFee( amount ); balance = balance.subtract( fee ); } public int getBalance( ) { return balance; } public Transaction[ ] getTransactionHistory( ) { Return transaction list as an array } public final void applyInterest() { if( balance.isNegative() ) { double interestRate = doGetDebitInterestRate(); int interest = balance.multiply( interestRate ); balance = balance.subtract( interest ); } else { double interestRate = doGetCreditInterestRate(); int interest = balance.multiply( interestRate ); balance = balance.subtract( interest ); } protected boolean doEligibilityCheckForWithdraw( int amount ) { return true; } protected boolean doEligibilityCheckForDeposit( int amount ) { return true; } protected int doGetWithdrawalFee( int amount ) { return new int( 0, 0 ); } protected int doGetDepositFee( int amount ) { return new int( 0, 0 ); } protected abstract double doGetDebitInterestRate(); protected abstract double doGetCreditInterestRate( ); }

5 class CurrentAccount extends BankAccount { private static final double INTEREST_RATE = 0.10; private int overdraftLimit; … protected boolean doEligibilityCheckForWithdraw( int amount ) { Calculate proposed balance and check that this does not exceed overdraftLimit } No need to override other hook methods protected double doGetDebitInterestRate( ) { return INTEREST_RATE; } protected double doGetCreditInterestRate( ) { return 0; } class SavingsAccount extends BankAccount { private static final double INTEREST_RATE = 0.08; private static final int MINIMUM_BALANCE = new int( 200, 0 ); private static final int MINIMUM_WITHDRAWAL = new int( 100, 0 ); … protected boolean doEligibilityCheckForWithdraw( int amount ) { Return true if 1) last Transaction in transactions was not a Withdrawal & 2) balance after the withdrawal would not drop below MINIMUM_BALANCE } protected boolean doEligibilityCheckForDeposit( int amount ) { Return true if amount > MINIMUM_WITHDRAWAL } No need to override other hook methods protected double doGetDebitInterestRate( ) { return 0; } protected double doGetCreditInterestRate( ) { return INTEREST_RATE; } class EncoreAccount extends BankAccount { private static final int WITHDRAWAL_FEE = new int( 0, 40 ); Define constants for interest rates protected double doGetCreditInterestRate( ) { Calculation and return interest rate based on balance } protected int doGetWithdrawalFee( int amount ) { return WITHDRAWA_FEE; }

6 Control flow BankAccount account = new CurrentAccount( … ); account.withdraw( … ); BankAccount Object CurrentAccount withdraw( ) { … doEligibilityCheckForWithdraw( ) … doGetWithdrawalFee( ).. } doEligibilityCheckForWithdraw( ) … } 1. BankAccount.withdraw( ) 2. CurrentAccount.doEligibilityCheckForWithdraw( ) 3. BankAccount.doGetWithdrawalFee( ) All instance method calls are dynamically bound. The process works the same for every method call

7 Down calls and up calls nConsider a new special type of Encore Account that pays a higher interest rate than a regular Encore Account but which incurs greater expense to withdraw int nWith a SpecialEncoreAccount, the cost of withdrawal is whatever it costs withdraw from an EncoreAccount plus a 17.5% of the amount being withdrawn class SpecialEncoreAccount extends EncoreAccount { private static final double WITHDRAWAL_COMMISSION = 0.175; Redefine constants for interest rates protected double doGetCreditInterestRate( ) { Calculate interest rate based on new tiered interest rates and balance } protected int doGetWithdrawalFee( int amount ) { int commission = amount.multiply( WITHDRAWAL_COMMISSION ); int totalFee = super.doGetWithdrawalFee( ).add( commission ); return totalFee; }

8 BankAccount Object EncoreAccount withdraw( ) { … doEligibilityCheckForWithdraw( ) … doGetWithdrawalFee( ) … } doGetWithdrawalFee( ) … } SuperEncoreAccount BankAccount account = new SuperEncoreAccount( … ); account.withdraw( … ); doGetWithdrawalFee( ) … super.getWithdrawalFee( ) } 1. BankAccount.withdraw( ) 2. BankAccount.doEligibilityCheckForWithdraw( ) 3. SuperEncoreAccount.doGetWithdrawalFee( ) 4. EncoreAccount.doGetWithdrawalFee( ) Control flow

9 class BankAccount { private int balance; private List transactions; private AccountPolicy policy; public BankAccount( ) { … } public void setPolicy( AccountPolicy policy ) { this.policy = policy; } public final void withdraw( int amount ) { Check amount is at least $1 boolean canWithdraw = policy.doEligibilityCheckForWithdraw( this, amount ); if( canWithdraw == true ) { balance = balance.subtract( amount ); Log transaction int fee = policy.doGetWithdrawalFee( this, amount ); balance = balance.subtract( fee ); } public final void deposit( int amount ) { Check amount is at least $1 boolean canDeposit = policy.doEligibilityCheckForDeposit( this, amount ); if( canDeposit == true ) { balance = balance.add( amount ); Log transaction int fee = policy.doGetDepositFee( this, amount ); balance = balance.subtract( fee ); } public final void applyInterest() { if( balance.isNegative() ) { double interestRate = policy.doGetDebitInterestRate(); int interest = balance.multiply( interestRate ); balance = balance.subtract( interest ); } else { double interestRate = policy.doGetCreditInterestRate(); int interest = balance.multiply( interestRate ); balance = balance.subtract( interest ); } public int getBalance( ) { return balance; } public Transaction[ ] getTransactionHistory( ) { Return transaction list as an array }

10 interface AccountPolicy { boolean doEligibilityCheckForWithdraw( BankAccount account, int amount ); boolean doEligibilityCheckForDeposit( BankAccount account, int amount ); int doGetWithdrawalFee( BankAccount account, int amount ); int doGetDepositFee( BankAccount account, int amount ); double doGetDebitInterestRate( BankAccount account ); double doGetCreditInterestRate( BankAccount account ); } abstract class DefaultPolicy implements AccountPolicy { public boolean doEligibilityCheckForWithdraw( BankAccount account, int amount ) { return true; } public boolean doEligibilityCheckForDeposit( BankAccount account, int amount ) { return true; } public int doGetWithdrawalFee( BankAccount account, int amount ) { return new int( 0, 0 ); } public int doGetDepositFee( BankAccount account, int amount ) { return new int( 0, 0 ); }

11 class SavingsPolicy extends DefaultPolicy { private static final double INTEREST_RATE = 0.08; private static final int MINIMUM_BALANCE = new int( 200, 0 ); private static final int MINIMUM_WITHDRAWAL = new int( 100, 0 ); public boolean doEligibilityCheckForWithdraw( BankAccount account, int amount ) { Transaction[ ] transactionHistory = account.getTransactionHistory( ); int balance = account.getBalance( ); Return true if: 1) last Transaction in transactionHistory was not a Withdrawal, & 2) balance after the withdrawal would not drop below MINIMUM_BALANCE } public boolean doEligibilityCheckForDeposit( BankAccount account, int amount ) { Return true if amount > MINIMUM_WITHDRAWAL } public double doGetDebitInterestRate( ) { return 0; } public double doGetCreditInterestRate( ) { return INTEREST_RATE; }

12 Sequence diagram for Strategy-based design client : BankAccount policy : SavingsPolicy amountToWithdraw : int setPolicy( policy ) create( ) withdraw( amountToWithdraw ) doEligibilityCheckForWithdraw( this, amountToWithdraw ) getTransactionHistory( ) getBalance( ) true doGetWithdrawalFee( this, amountToWithdraw )

13 Sequence diagram for Template Method based design client : SavingsAccount amountToWithdraw : int create( ) withdraw( amountToWithdraw ) doEligibilityCheckForWithdraw( amountToWithdraw ) true doGetWithdrawalFee( amountToWithdraw )


Download ppt "Tutorial 10 COMPSCI 230 Software Design & Construction Muhammad Sulyaman."

Similar presentations


Ads by Google