ObjectOriented Programming Part 2 Bank Account Exercise Hood

Object-Oriented Programming Part 2 Bank Account Exercise Hood College JETT Workshop Dept. of Computer Science February 26 2005

Object-Oriented Programming Part 2 This presentation will cover: 1. Statement of Bank Problem 2. Proposed Solutions 3. Inheritance 4. Bank. Acct class 5. Check. Acct class 6. Sav. Acct class 2

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 Market A 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. 3

Proposed Solution Here’s one possible approach: Create a class for each type of bank account: class Check. Acct {. . } class Sav. Acct {. . } class MMAcct {. . } Disadvantage: • A lot of the code is apt to be very similar. For instance, all classes contain instance fields acct. Name and balance, and method get. Acct. No() and get. Balance(). 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. 4

Another Proposed Solution Create a master class Acct that accommodates all account types: public class Acct { private double balance; private String acct. No; private int type; // 1=Chkng. 2=Svng. 3=MM. private double int. Rate; // needed for Svng, MM private double min. Bal; // needed for MM. . public void withdraw(double amt) { if (type == 0) {. . } else if (type == 1) }. . } } 5

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. 6

Best solution The solution in Java is to use inheritance.

Inheritance Bank. Acct has two child classes (or subclasses) Saving. Acct and Check. Acct. Bank Account Checking The Saving. Acct class in turn has one child: MMAcct. At the top of the hierarchy, the class Bank. Acct serves as a parent class (or base class or super class). Savings Money Market 8

Inheritance Roughly, the inheritance diagram can be coded in Java as follows: public abstract class Bank. Acct {. . } public class Sav. Acct extends Bank. Acct {. . } public class Check. Acct extends Bank. Acct {. . } public class MMAcct extends Sav. Acct {. . } 9

Inheritance (cont’d) • Each generation is related • A child class “inherits” all of to the previous one via an the methods and instance “is-a” relationship. fields from the parent class. It is as if all of the public – A savings account and a methods from the parent checking account “is-a” class were copied and pasted bank account. into the child class! This – A Money Market account results in less code “is-a” savings account. duplication. • To indicate that one class is a Bank Account subclass of another we use the keyword extends; e. g. Checking Savings Money Market public class MMAct extends Sav. Acct 10

Bank. Acct Class The base class Bank. Acct is a very simple one. Instance Variables Data Type Access acct. No balance String double private Method Parameters Return Type Comment Bank. Acct() String acct double bal none constructor get. Balance() none double get. Acct. No() none String deposit() double amt void withdraw() double amt void 11

Check. Acct Class • The Check. Acct class will automatically inherit the instance fields acct. No and balance from the parent class Bank. Acct. • The Check. Acct class will automatically inherit the methods deposit() and withdraw() from Bank. Acct. • However, although the inherited deposit() method will work correctly, the inherited withdraw() method will not! A withdrawal will affect the state of a Check. Acct 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(). 12

Check. Acct Class (cont’d) Instance Variables Data Type Comment acct. No balance String double inherited Method Parameter Return. Type Comment Check. Acct String acct double bal none constructor get. Balance() none double inherited get. Acct. No() none String inherited deposit() double amt void inherited withdraw() double amt void override 13

Check. Acct Class (cont’d) public class Check. Acct { // Constructor public Check. Acct(String acct, double bal) {. . } // overrides withdraw() public void withdraw(double amt) {. . } // other methods and instance fields are inherited } 14

Check. Acct 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 Bank. Acct. This means that balance can be accessed only from non-static methods of Bank. Acct. Since the withdraw() method above is a method of Check. Acct(and not Bank. Acct), balance is inaccessible. 15

Check. Acct 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 Check. Acct withdraw method, create an infinite recursive loop. 16

Check. Acct 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”. 17

Check. Acct Class (cont’d) Finally, we define the constructor for Check. Acct class. In the constructor, we simply want to initialize the acct. No and balance as appropriate. Our first attempt looks like this: public Check. Acct(String acct, double bal) { acct. No = acct; balance = bal; } Exercise: Unfortunately, this won’t work! Why? acct. No and balance are private variables of the super class 18

Check. Acct Class (cont’d) Here’s the correct version of the constructor: public Check. Acct(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. Bank. Acct’s constructor. 19

Check. Acct Class (cont’d) Here’s what the Check. Acct class looks like: public class Check. Acct extends Bank. Acct { // constructor public Check. Acct(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 } 20

Sav. Acct Class • Will automatically inherit the instance field acct. No and balance from the parent class Bank. Acct. • Requires two additional instance fields: the interest rate, int. Rate; and the lowest balance during the compounding period, low. Bal. • Will automatically inherit the method deposit() and withdraw() from Bank. Acct. • However, although the inherited deposit() method will work correctly, the inherited withdraw() method will not! A withdrawal will affect the state of a Sav. Acct object since it affects the lowest balance. As with the Check. Acct class, we must override the definition of withdraw(). • The class Sav. Acct will also require a method add. Int(), which adds interest to the account. 21

Sav. Acct Class (cont’d) Instance Variables Data Type Comment acct. No Balance int. Rate low. Bal String double inherited private Method Sav. Acct Parameter String acct double bal double rate Return Type none Comment constructor get. Balance() none double inherited get. Acct. No() none String inherited deposit() double amt void inherited withdraw() double amt void override add. Int() none void new method 22

Sav. Acct Class (cont’d) Here’s the constructor: public Sav. Acct(String acct, double bal, double rate) super(acct, bal); int. Rate = rate; low. Bal = bal; } The method call super() means “call the constructor from the super class”, in this case, Bank. Acct. 23

Sav. Acct Class (cont’d) Exercise: Write the withdraw() method. Here’s a start: public void withdraw(double amt) { } 24

Sav. Acct Class (cont’d) Exercise: Write the add. Int() method. Here’s a start: public void add. Int() { } 25

Sav. Acct Class (cont’d) public class Sav. Acct extends Bank. Acct { private double int. Rate; private double low. Bal; // Constructor public Sav. Acct(String acct, double bal, double rate) { super(acct, bal); int. Rate = rate; low. Bal = bal; } // overrides withdraw() public void withdraw(double amt) { super. withdraw(amt); if (get. Balance() < low. Bal) { low. Bal = get. Balance(); } } 26

Sav. Acct Class (cont’d) // new method public void add. Int() { deposit(low. Bal*int. Rate); low. Bal = get. Balance(); } // other methods and instance fields are inherited } 27

MMAcct Class We leave the remaining class, MMAcct, to the hands-on exercise. 28

End of Lecture
- Slides: 29