Tutorial 10 COMPSCI 230 Software Design & Construction Muhammad Sulyaman.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Checking and Savings Account Chapter 5. Tools of Monetary Asset Management Low-cost, interest-earning checking accounts (Type 1). Interest-earning savings.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Teens 2 lesson six using banking services presentation slides 04/09.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Previous Exam 1.0. Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Banking. Checking Accounts  A safe and easy way to manage the money you make when you are working is with a bank checking account.  This account allows.
Checking Accounts and Other Bank Services. Purpose of a Checking Account It allows the depositor to pay a stated amount (Check) to a person or business.
Common payment services What are the common payment services provided by financial institutions? 1.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Programming Languages and Paradigms Object-Oriented Programming.
 CONVENIENT  HELPS YOU KEEP TRACK OF MONEY: USING THE CHECK REGISTER OR ONLINE BANKING  SAVES YOU MONEY – EXPENSES ARE LESS THAN MONEY ORDERS.
Checking Accounts Chapter 29.
Why It’s Important Paying with checks is the most common and safest medium of exchange.
Building Bucks Basic Financial Services. Financial Institutions 3 Main Types – Banks – Credit Unions – Savings and Loan Associations (S&L) Advantages.
Bank Account Reconciliation. Reconciling The process of matching your checkbook register with the bank statement is known as reconciliation. The back.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
Bank Accounts. Deposit Account: Deposit Account: An account at a banking institution which allows the account holder to deposit money into or withdraw.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
Balancing a Check Register:. Check Register: A personal record of the transactions against a checking account.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Example 2 Adding Integers Find the sum – CHECK You can use a number line to check your answer –= 8 – Use sign of number with greater absolute.
MAINTAINING A CHECKBOOK REGISTER BANKING PART 6 Mr. Stasa
Introduction to Java Java Translation Program Structure
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Adjusting Entries and the Worksheet. What is a worksheet? If software is used, the data is almost ready to be presented. If we do not use software, accountants.
Choosing and Balancing a Checking Account Personal Finance.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
A plan for managing money during a given period of time Financial Roadmap.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Banking Review. Bank Business that stores money for individuals and businesses.
Chapter 5. Financial Services Borrowing Short Term Regular Savings Money Market Accounts Long Term Certificates of Deposit U.S. Savings Bonds Investment.
Chapter 13 - Inheritance.
Data Structures and Algorithms revision
Classes Object-oriented programming: Example: Bank transactions
Lecture Notes – Inheritance (Ch 9-10)
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Computing with C# and the .NET Framework
Packages, Interfaces & Exception Handling
CSC 205 Java Programming II
null, true, and false are also reserved.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Anatomy of a Method.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
JAVA CLASSES.
Presentation transcript:

Tutorial 10 COMPSCI 230 Software Design & Construction Muhammad Sulyaman

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

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

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( ); }

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; }

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

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; }

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

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 }

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 ); }

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; }

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 )

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