AP Computer Science Inheritance and Polymorphism lab Name

advertisement
AP Computer Science
Inheritance and Polymorphism lab
Name: _________________
Account
Note: Most of the Account class has already been written in a previous lab. You will
need to implement the transferTo() method.
Create an Account class with the following fields:
 String name (you need to add)
 int ID
 double balance
 Date created
 Static double annual interest rate
 Static int nextId – User ID is set to this value in the Constructor.
1. To support data encapsulation, all instance fields should be private.
2. Add the following constants
public static final int SAVINGS_TYPE = 1;
public static final int CHECKING_TYPE = 2;
public static final int CREDIT_TYPE = 3;
3. In the Account class include the following methods:
public Account(String name, double balance)
 Constructor, set id to nextID and then increment the nextID
public static void setAnnualInterestRate(double rate)

Note, this is a static method, so it can be called as a class method
(Account.setAnnualInterestRate(.12).
protected void setBalance(double balance)
 Note, this mutator has an access type of protected so that it is
assessable from classes which extend the Account class.
public boolean deposit (double amount)
 returns false if amount is negative
public boolean withdraw (double amount)
 returns false if there is insufficient funds for the withdraw
public boolean transferTo(Account acct, double amount)
 transfers amount from this to the specified acct
public void monthlyEvent()
 applies the monthly interest: get monthly rate from annual rate,
multiply by the balance, and add to balance.
public String getName()
 returns the name field of the account.
public String getStrType()
 returns “Savings”;
public int getNumType()

returns SAVINGS_TYPE
CreditAccount
This is an account where you are borrowing money from the bank to buy
something. Examples include a credit card (VISA) or a car loan. With a Credit
Account, you are charged a monthly service charge by the bank to borrow their
money. For this exercise, there is no limit as to how much you can borrow.
1. Add a static double variable for the annual service rate. The annual service
rate will be the same for all CreditAccounts.
2. Add a static mutator and accessor so we can set and get the annual service
rate by calling a class method.
3. Create a 2 parameter constructor which calls the Account’s 2 parameter
constructor. The balance of the CreditAccount will be negative, representing
a liability.
4. Override the withdraw method. Allow for a negative balance. There will be
no limit for how much a person can borrow.
5. Add a monthly event which will charge the monthly service amount to the
account.
6. Override getType() and getNumType()
 getStrType() returns “Credit”

getNumType() returns CREDIT_TYPE
CheckingAccount
Back in the day, they had (and still do) have a Checking Account which allows you
to write checks against the balance of the account. If you write a check for an
amount that is greater than what you have in the account, the check gets
“bounced” (does not go through) and you will be charged a fine. This is called an
overdraft. However, to prevent overdraft charges, you can have a Credit Account
and the bank will automatically charge this account if you try to write a check with
insufficient funds.
1. Add the following field to the CheckingAccount class:
 A reference to a CreditAccount object.
2. Create the following constructor. Call the super class’s constructor via a call to
super ( arguments… )
public Account(String name, double balance)
3. Add a mutator which will set the reference to the Credit Account.
public void setCreditAccount(CreditAccount acct)
4. Add a constant, OVERDRAFT_CHARGE = 20.00
5. Override the withdraw method
if the amount to withdraw is greater than the current balance
if the CreditAccount reference has been set
withdraw the balance from the Checking Account
withdraw the difference from the CreditAccount
return true
else
charge an OVERDRAFT_CHARGE
return false
else
call the super class withdraw method
6. Add a monthly event which does nothing. The bank will not pay interest on a
Checking account.
7. Override getType() and getNumType()
 getStrType() returns “Checking”

getNumType() returns CHECKING_TYPE
Bank
The Bank class has been provided for you.
an example of overloading.
Look at the findAccount methods for
BankTest
1. Complete addNewAccount
If they add a new CreditAccount, check to see if the same user has a
CheckingAccount. If they do, call the mutator to link the CreditAccount to
the CheckingAccount.
If they add a new CheckingAccount, check to see if the same user has a
CreditAccount. If they do, call the mutator to link the CreditAccount to the
Checking Account.
2. Test your Account, CheckingAccount, and CreditAccount with cases provided.
Display of expected results is shown below.
3. Complete the test menu so you can create accounts and access them.
4. Come up with additional test cases so you can verify your program works as
expected. Don’t let the customer find the bugs….
Display of test case.
After adding 6 Accounts
Name
Fred Flinstone
Fred Flinstone
Fred Flinstone
Wilma Flinstone
Wilma Flinstone
Barney Ruble
ID
100
101
102
103
104
105
Balance
1000.00
500.00
-100.00
2000.00
500.00
5000.00
Type
Savings
Checking
Credit
Savings
Checking
Savings
Test 1: Withdraw 300 from Fred's checking
withdraw result: true
Name
ID Balance
Fred Flinstone
100 1000.00
Fred Flinstone
101
200.00
Fred Flinstone
102 -100.00
Wilma Flinstone
103 2000.00
Wilma Flinstone
104
500.00
Barney Ruble
105 5000.00
Type
Savings
Checking
Credit
Savings
Checking
Savings
Test 2: Withdraw 500 from Fred's checking, verify withdraw from
credit
withdraw result: true
Name
ID Balance
Type
Fred Flinstone
100 1000.00
Savings
Fred Flinstone
101
0.00
Checking
Fred Flinstone
102 -400.00
Credit
Wilma Flinstone
103 2000.00
Savings
Wilma Flinstone
104
500.00
Checking
Barney Ruble
105 5000.00
Savings
Test 3: Withdraw 600 from Wilma's checking, insufficent funds should fail. Verify overdraft charge
withdraw result: false
Name
ID Balance
Type
Fred Flinstone
100 1000.00
Savings
Fred Flinstone
101
0.00
Checking
Fred Flinstone
102 -400.00
Credit
Wilma Flinstone
103 2000.00
Savings
Wilma Flinstone
104
480.00
Checking
Barney Ruble
105 5000.00
Savings
Test 4: Withdraw 2000 from Barney's savings
withdraw result: true
Name
ID Balance
Fred Flinstone
100 1000.00
Fred Flinstone
101
0.00
Fred Flinstone
102 -400.00
Wilma Flinstone
103 2000.00
Wilma Flinstone
104
480.00
Barney Ruble
105 3000.00
Type
Savings
Checking
Credit
Savings
Checking
Savings
Test 5: Transfer 100 from Wilma's savings to her checking account
transfer result: true
Name
ID Balance
Type
Fred Flinstone
100 1000.00
Savings
Fred Flinstone
101
0.00
Checking
Fred Flinstone
102 -400.00
Credit
Wilma Flinstone
103 1900.00
Savings
Wilma Flinstone
104
580.00
Checking
Barney Ruble
105 3000.00
Savings
Test 7: Transfer 500 from Fred's checking to his savings account
- should come from credit
transfer result: true
Name
ID Balance
Type
Fred Flinstone
100 1500.00
Savings
Fred Flinstone
101
0.00
Checking
Fred Flinstone
102 -900.00
Credit
Wilma Flinstone
103 1900.00
Savings
Wilma Flinstone
104
580.00
Checking
Barney Ruble
105 3000.00
Savings
Test 8: process monthly events, add interest to savings, charge
interest to credit accounts
Name
ID Balance
Type
Fred Flinstone
100 1515.00
Savings
Fred Flinstone
101
0.00
Checking
Fred Flinstone
102 -918.00
Credit
Wilma Flinstone
103 1919.00
Savings
Wilma Flinstone
104
580.00
Checking
Barney Ruble
105 3030.00
Savings
Download