The goal of this assignment is to understand the concept of inheritance in Java. You will create subclasses to override and add new methods to the subclasses.
Consider a hierarchy of bank account classes as shown in the figure below.
You will implement the four classes, Account, SavingsAccount, CheckingAccount, and MoneyMarketAccount.
List of attributes:
The constructor should be of the form:
public Account(String id, double initialBalance)
List of methods:
This type of account requires a minimum of $10 in the account. This account allows for depositing or withdrawing from ATM machines. No fees are charged for depositing. Withdrawing money incurs a transaction fee of $2 per withdrawal that is taken out of the balance. Any withdrawal that causes the balance to go below $10 (because of the amount to be withdrawn plus the fee) is disallowed. Interest amount is calculated and added to the balance every quarter.
The constructor should be of the form:
public SavingsAccount(String id, double initialBalance) throws Exception
The initial balance cannot be less than $10. If it is, then a new Exception is thrown with the message, "Insufficient starting balance" (i.e., throw new Exception("Insufficient starting balance");) and the account is not created.
No new attributes are needed. Override the withdraw method to incorporate the transaction fee for ATM withdrawals. A withdrawal that potentially lowers the balance below $10 is not allowed, and an Exception is thrown with the message "Insufficient funds (10)". The deposit method can be inherited.
Add the following method:
This account does not give any interest. It allows deposit and withdrawals through ATM machines, which incurs a fee of $1 per transaction, which is deducted from the balance. The balance cannot go below 0. An exception with the message "Insufficient balance (0)" is thrown if a withdraw may cause the balance to go below 0.
The constructor should be of the form:
public CheckingAccount(String id, double initialBalance) throws Exception
Checks may be used to make withdrawals. The first three check uses in a month are free, but subsequent uses add a fee of $2 to each check withdrawal. ATM withdrawals can take the balance down to zero but not below. Only checks are allowed to take the balance to -$10 (i.e., an overdraft). One or more checks can bring the balance down to -$10, but not lower. If a check use potentially lowers the balance below -$10, the check is disallowed and an Exception is thrown with the message "Insufficient funds (-10)".
Tracking how many checks are used in a month requires a new attribute and methods. Implement the following attribute:
The initial balance cannot be negative. If it is negative, a new Exception is thrown with the error message, "Negative balance", and the account is not created.
Override deposit and withdraw methods as needed to incorporate the new rules for CheckingAccount. In addition, add the following new methods:
A money market account gives you higher interest, but offers a limited number of deposits and withdrawsls. A maximum of 6 transactions (deposits or withdrawals) is allowed per month. If a withdrawal causes the balance to go below $10,000, a fee of $100 is imposed and no more transactions are allowed until the balance is increased using a deposit transaction. A deposit performed to reach or exceed the minimum balance is not counted as part of the 6 transactions.
The constructor should be of the form:
public MoneyMarketAccount(String id, double initialBalance) throws Exception
The initial balance cannot be less than $10,000. If it is, then a new Exception is thrown with the error message, "Insufficient starting balance", and the account is not created.
Add an attribute to track the number of transactions:
private int numberOfTransactions; // store how many deposits and withdrawals were performed
Override the deposit and withdraw methods to take into account the constraint on the number of transactions as well as the minimum balance requirement. If a deposit or withdraw cannot be performed because of too many transactions, then an Exception is thrown with the message "Exceeding allowed transactions (6)". If a withdrawal is denied because of minimum balance problems, then throw an Exception with the message "Below minimum balance".
Add the following methods:
None of the methods above should be declared static. In fact, compilation of your program will fail if the method signatures in your program are different than above.
Constructors in the subclasses should make appropriate calls the the superclass'constructor rather than do the work themselves.
Keep in mind that we will not test your main method -- the methods you implement will be tested directly. Ideally, you should create your own main methods, perhaps one in each class, to test each class separately. You can also write another main method in a separate class that tests the four classes together.
Here is a sample barebones main method for the Account class. It does not test all the methods, nor does it test for various situations.
public static void main(String[] args) { Account a1 = new Account ("csclub", 200.05); try { a1.withdraw(10); } catch(Exception e) { System.err.println(e); } System.out.println("Remaining balance in " + a1.getID() + "'s account: $" + a1.getBalance()); }
Here is a sample barebones main method for the SavingsAccount class. It does not test all the methods, nor does it test for various situations.
public static void main(String[] args) { SavingsAccount a1 = null; try { a1 = new SavingsAccount ("ghosh", 2000.05); } catch(Exception e) { System.err.println(e); } try { a1.deposit(10); } catch (Exception e) { System.err.println(e); } double interest = a1.addInterest(0.75); System.out.println("Interest: " + interest); System.out.println("Remaining balance in " + a1.getID() + "'s account: $" + a1.getBalance()); }
Create a single jar file called P3.jar from the four Java files
(Account, SavingsAccount, CheckingAccount, and
MoneyMarketAccount).
Do not add other files (e.g., class files). Submit the P3.jar file
via the online checkin system.