Question
In this problem, you will write a tester class PolymorphismRunner. You are provided with three classes: BankAccount, SavingsAccount and CheckingAccount Add the code described below
In this problem, you will write a tester class PolymorphismRunner. You are provided with three classes: BankAccount, SavingsAccount and CheckingAccount
Add the code described below to the main method
Create an array of BankAccounts that can hold 5 objects. You will fill the array with the objects specified in the table
Index | Type | InitialBalance | InterestRate | Transaction Fee |
---|---|---|---|---|
0 | BankAccount | 1000 | ||
1 | SavingsAccount | 1000 | .01 | |
2 | CheckingAccont | 3000 | $3 | |
3 | SavingsAccount | 1000 | .01 | |
4 | SavingsAccount | 2000 | .012 |
Loop through the array
Deposit $200 in each account
Withdraw $100 from each account
Call each account's endOfMonth method and then print the word Balance: followed by the balance in the account to 2 decimal places
If the account is an instance of SavingsAccount, cast it to a SavingsAccount and print the word Interest: followed by the interest rate
Your output should look like this
Balance: 1090.00 Balance: 1100.01 Interest: 0.01 Balance: 3094.00 Balance: 1100.01 Interest: 0.01 Balance: 2100.02 Interest: 0.012
Use the following files:
BankAccount.java
/** * A bank account has a balance that can be changed by * deposits and withdrawals * */ public class BankAccount { private double balance; public final static double MINIMUM_BALANCE = 1500; public final static double BELOW_MINIMUM_FEE = 10.0; /** * Constructs a bank account with a given balance. * @param initialBalance the initial balance */ public BankAccount(double initialBalance) { balance = initialBalance; } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { balance = balance + amount; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { balance = balance - amount; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } /** * Do end of month processing for the account */ public void endOfMonth() { if (balance < MINIMUM_BALANCE) { balance = balance - BELOW_MINIMUM_FEE; } } }
CheckingAccount.java
/** * A type of BankAccount where there are charges * for each transaction */ public class CheckingAccount extends BankAccount { private double transactionFee; private int transactions; /** * Creates a TransactionAccount object with a starting balance and a transaction fee * @param initialBalance the starting balance * @param transactionFee the fee charged for each transaction */ public CheckingAccount(double initialBalance, double transactionFee) { super(initialBalance ); transactions = 0; this.transactionFee = transactionFee; } @Override public void deposit(double amount) { super.deposit(amount); transactions++; } @Override public void withdraw(double amount) { super.withdraw(amount); transactions++; } @Override public void endOfMonth() { double fee = transactions * transactionFee; super.withdraw(fee); transactions = 0; } }
SavingsAccount.java
/** * An account that earns interest every month */ public class SavingsAccount extends BankAccount { private double annualInterestRate; /** * Creates a InterestAccount object with a starting balance and account id * @param initialBalance the starting balance * @param rate the annual interest rate */ public SavingsAccount(double initialBalance, double rate) { super(initialBalance); annualInterestRate = rate; } public double getInterestRate() { return annualInterestRate; } @Override public void endOfMonth() { double interest = getBalance() * annualInterestRate / 100 /12; deposit(interest); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started