object-oriented programming part 2 bank account exercise hood college jett workshop dept. of...
TRANSCRIPT
Object-Oriented ProgrammingPart 2
Bank Account Exercise
Hood College JETT WorkshopDept. of Computer Science
February 26 2005
2
Object-Oriented ProgrammingPart 2
This presentation will cover:1. Statement of Bank Problem2. Proposed Solutions3. Inheritance4. BankAcct class5. CheckAcct class6. SavAcct class
3
Bank Problem
• Problem: Hood National Bank has hired you to develop banking software. Currently, the bank has several types of bank accounts (checking, savings, CD, money market, etc.) Write a program that will satisfy your customer’s needs.
Checking Savings Money MarketA checking account allows you to deposit money but incurs a transaction charge of 50 cents per withdrawal transaction. This 50 cent surcharge is universal across all checking accounts.
A savings account has no transaction charges and yields an interest rate, based on the lowest balance during a compounding period. The interest rate may differ across savings accounts.
A money market account is essentially a savings account that requires a minimum balance of 1000 dollars at all times. Every withdrawal transaction that leaves the balance below 1000 dollars triggers a penalty of 10 dollars.
4
Proposed Solution
Here’s one possible approach: Create a class for each type of bank account:
class CheckAcct { .. } class SavAcct { .. }
class MMAcct { .. }
Disadvantage:• A lot of the code is apt to be
very similar. For instance, all classes contain instance fields acctName and balance, and method getAcctNo() and getBalance(). This code would have to be duplicated for each type of account!
• If the bank offers new types of accounts in the future, then every software application (including the one you’re currently writing) must be updated with the new type of account. Maintenance nightmare.
5
Another Proposed Solution
Create a master class Acct that accommodates all account types: public class Acct { private double balance; private String acctNo; private int type; // 1=Chkng. 2=Svng. 3=MM. private double intRate; // needed for Svng, MM private double minBal; // needed for MM .. public void withdraw(double amt) { if (type == 0) { .. } else if (type == 1) } .. } }
6
Another Proposed Solution (cont’d)
Advantage:• Some duplicated code can now be avoided.
Disadvantages:• Since the master class Acct must accommodate any type of
account, it has an excess of instance fields. An instance field must be included even if only one type of account needs it.
• A maintenance problem still exists. If, in the future, the bank decides to offer new types of accounts, then the Account class must be updated with the new type of account.
Best solution
The solution in Java is to use inheritance.
8
Inheritance
Bank Account
Checking Savings
Money Market
At the top of the hierarchy, the class BankAcct serves as a parent class (or base class or super class).
BankAcct has two child classes (or subclasses) SavingAcct and CheckAcct.
The SavingAcct class in turn has one child: MMAcct.
9
Inheritance
Roughly, the inheritance diagram can be coded in Java as follows:
public abstract class BankAcct { ..
}
public class SavAcct extends BankAcct { ..
}
public class CheckAcct extends BankAcct { ..
}
public class MMAcct extends SavAcct { ..
}
10
Inheritance (cont’d)
• Each generation is related to the previous one via an “is-a” relationship.– A savings account
and a checking account “is-a” bank account.
– A Money Market account “is-a” savings account.
• A child class “inherits” all of the methods and instance fields from the parent class. It is as if all of the public methods from the parent class were copied and pasted into the child class! This results in less code duplication.
• To indicate that one class is a subclass of another we use the keyword extends; e.g.
public class MMAct extends SavAcct
Bank Account
Checking Savings
Money Market
11
BankAcct Class
The base class BankAcct is a very simple one.
constructornoneString acct
double bal
BankAcct()
void
void
String
double
Return Type
Comment
double amtwithdraw()
double amtdeposit()
nonegetAcctNo()
nonegetBalance()
ParametersMethod
Stringdouble
Data Type
privateprivate
acctNobalance
AccessInstance Variables
12
CheckAcct Class
• The CheckAcct class will automatically inherit the instance fields acctNo and balance from the parent class BankAcct.
• The CheckAcct class will automatically inherit the methods deposit() and withdraw() from BankAcct.
• However, although the inherited deposit() method will work correctly, the inherited withdraw() method will not! A withdrawal will affect the state of a CheckAcct object since doing so will decrease the instance field balance by an additional 50 cents. In this case, we must override the definition of withdraw().
13
CheckAcct Class (cont’d)
constructornoneString acct
double bal
CheckAcct
overridevoiddouble amtwithdraw()
inheritedvoiddouble amtdeposit()
inheritedStringnonegetAcctNo()
inheriteddoublenonegetBalance()
CommentReturnTypeParameterMethod
Stringdouble
Data Type
inheritedinherited
acctNobalance
CommentInstance Variables
14
CheckAcct Class (cont’d)
public class CheckAcct {
// Constructor
public CheckAcct(String acct, double bal) {
..
}
// overrides withdraw()
public void withdraw(double amt) {
..
}
// other methods and instance fields are inherited
}
15
CheckAcct Class (cont’d)
• First, we override the withdraw() method. Here is a first attempt:
// First attempt public void withdraw(double amt) { balance = balance – amt; balance = balance – 0.5; }
The above, however, won’t compile! The reason is because the instance field balance is marked
private in class BankAcct. This means that balance can be accessed only from non-static methods of BankAcct. Since the withdraw() method above is a method of CheckAcct(and not BankAcct), balance is inaccessible.
16
CheckAcct Class (cont’d)
• Here’s a second attempt to override the withdrawal() method:// second attempt
public void withdraw(double amt) {
withdraw(amt);
withdraw(0.50);
}
Exercise: Why won’t the above work?
withdraw will call the CheckAcct withdraw method, create an infinite recursive loop.
17
CheckAcct Class (cont’d)
• Here’s our third and final (and correct) attempt:
// third attempt public void withdraw(double amt) { super.withdraw(amt);
super.withdraw(0.50);}
The keyword super is a reference to the activating object’s superclass. Think of it as a pronoun that says, “My parent”.
18
CheckAcct Class (cont’d)
Finally, we define the constructor for CheckAcct class.In the constructor, we simply want to initialize the acctNo and
balance as appropriate. Our first attempt looks like this:
public CheckAcct(String acct, double bal) {
acctNo = acct;
balance = bal;
}
Exercise: Unfortunately, this won’t work! Why?
acctNo and balance are private variables of the super class
19
CheckAcct Class (cont’d)
Here’s the correct version of the constructor:
public CheckAcct(String acct, double bal) {
super(acct, bal);
}
As before, the super keyword is a pronoun for “activating object’s parent.” In this context, the super keyword is used to invoke the activating object’s parent’s constructor, i.e. BankAcct’s constructor.
20
CheckAcct Class (cont’d)
Here’s what the CheckAcct class looks like:
public class CheckAcct extends BankAcct { // constructor public CheckAcct(String acct, double bal) { super(acct, bal); }
// overrides withdraw() public void withdraw(double amt) { super.withdraw(amt); super.withdraw(0.50); }
// other methods and instance fields are inherited}
21
SavAcct Class
• Will automatically inherit the instance field acctNo and balance from the parent class BankAcct.
• Requires two additional instance fields: the interest rate, intRate; and the lowest balance during the compounding period, lowBal.
• Will automatically inherit the method deposit() and withdraw() from BankAcct.
• However, although the inherited deposit() method will work correctly, the inherited withdraw() method will not! A withdrawal will affect the state of a SavAcct object since it affects the lowest balance. As with the CheckAcct class, we must override the definition of withdraw().
• The class SavAcct will also require a method addInt(), which adds interest to the account.
22
SavAcct Class (cont’d)
overridevoiddouble amtwithdraw()
constructornoneString acct
double bal
double rate
SavAcct
new methodvoidnoneaddInt()
inheritedvoiddouble amtdeposit()
inheritedStringnonegetAcctNo()
inheriteddoublenonegetBalance()
CommentReturn TypeParameterMethod
Stringdoubledoubledouble
Data Type
inheritedinheritedprivateprivate
acctNoBalanceintRatelowBal
CommentInstance Variables
23
SavAcct Class (cont’d)
Here’s the constructor:
public SavAcct(String acct, double bal, double rate)
super(acct, bal);
intRate = rate;
lowBal = bal;
}
The method call super() means “call the constructor from the super class”, in this case, BankAcct.
24
SavAcct Class (cont’d)
Exercise: Write the withdraw() method. Here’s a start:
public void withdraw(double amt) {
}
25
SavAcct Class (cont’d)
Exercise: Write the addInt() method. Here’s a start:
public void addInt() {
}
26
SavAcct Class (cont’d)
public class SavAcct extends BankAcct { private double intRate; private double lowBal;
// Constructor public SavAcct(String acct, double bal, double rate) { super(acct, bal); intRate = rate; lowBal = bal; } // overrides withdraw() public void withdraw(double amt) { super.withdraw(amt); if (getBalance() < lowBal) { lowBal = getBalance(); } }
27
SavAcct Class (cont’d)
// new method
public void addInt() {
deposit(lowBal*intRate);
lowBal = getBalance();
}
// other methods and instance fields are inherited
}
28
MMAcct Class
We leave the remaining class, MMAcct, to the hands-on exercise.
End of Lecture