In this exercise, you will create two subclasses of the Account class in the
Banking project: SavingsAccount and CheckingAccount.
Using a text editor, modify
the Account class source file in the src/com/mybank/domain directory. This
class must satisfy the UML diagram in Figure 6-1 on page Lab 6-2; in
particular, the balance attribute and Account class
constructor are now protected (indicated by
the # character instead of the - character ).
protected
Account ( double initBalance ) {
balance = initBalance;
}
Using a text editor,
create the SavingsAccount class source file in the src/com/mybank/domain/
directory.
This class must satisfy the UML diagram in Figure 6-1 on page Lab 6- 2 and the
business rules defined in the introduction to "Exercise 1: Creating Bank
Account Subclasses (Level 1)" on page Lab 6-2.
package
com.mybank.domain;
public class SavingsAccount extends Account {
//insert code here
}
private double interestRate;
public
SavingsAccount( double initBalance, double interestRate ){
super( initBalance );
this.interestRate = interestRate;
}
Using a text editor, create the CheckingAccount class source
file in the src/com/mybank/domain/ directory. This class must
satisfy the UML diagram in Figure 6-1 on page Lab 6- 2 and the business rules
defined in the introduction to "Exercise 1: Creating Bank Account
Subclasses (Level 1)" on page Lab 6-2.
package com.mybank.domain;
public class CheckingAccount extends Account {
//insert code here
}
private double overdraftAmount;
public CheckingAccount ( double initBalance, double overdraftAmount ) {
super ( initBalance );
this.overdraftAmount = overdraftAmount;
}
public CheckingAccount ( double initBalance ) {
this ( initbalance, 0.0 );
}
public boolean withdraw( double amount ) {
boolean result = true;
if( balance < amount) {
double overdraftNeeded = amount - balance;
if( overdraftAmount < overdraftNeeded ) {
result = false;
} else {
balance = 0.0;
overdraftAmount -=overdraftNeeded;
}
} else {
balance -= amount;
}
return result;
}
Copy the TestBanking.java file file from the resources/mod06_class1/ directory into the src/com/mybank/test directory.
cp ~/resources/mod06_class1/TestBanking.java src/com/mybank/test/
On the command line, use the javac command to compile the test program.
cd srcOn the command line, use the java command to run the test program.
java -cp ../classes com.mybank.test.TestBanking
You should see the output listed on page Lab 6-4.: