Question
In Java. Bank Accounts Look at the Account class Account.java and write a main method in a different class to briefly experiment with some instances
In Java.
Bank Accounts Look at the Account class Account.java and write a main method in a different class to briefly experiment with some instances of the Account class.
Using the Account class as a base class, write two derived classes called SavingsAccount and CheckingAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. A CheckingAccount object, in addition to the attributes of an Account object, should have an overdraft limit variable. Ensure that you have overridden methods of the Account class as necessary in both derived classes.
Now create a Bank class, an object of which contains an array of Account objects. Accounts in the array could be instances of the Account class, the SavingsAccount class, or the CheckingAccount class. Create some test accounts (some of each type).
The Bank class requires a method for opening accounts, and to deposit and withdraw from accounts specified by their account number. Account numbers are generated sequentially starting with 101.
Hints:
Note that the balance of an account may only be modified through the deposit(double) and withdraw(double) methods.
The Account class should not need to be modified at all.
Use BankDemo.java to test your code.
-----------------------------------------------------------------------
public class BankDemo { public static void main() { Bank tinyBank = new Bank(3); // create Bank that can hold 3 accounts tinyBank.openCheckingAccount(100,20); // balance=100 credit limit = 20 tinyBank.openSavingsAccount(200, 0.05); // balance=200 interest=5% tinyBank.openCheckingAccount(300,30); // balance=300 credit limit = 30 tinyBank.openSavingsAccount(400,0.05); // balance=400 interest=5%); tinyBank.openCheckingAccount(400,40); // balance=400 credit limit = 40 tinyBank.openSavingsAccount(400,0.05); // balance=400 interest=5%); tinyBank.printAccouns(); System.out.println(" tinyBank.deposit(112,500)"); tinyBank.deposit(112,500); System.out.println(" tinyBank.withdraw(101,500)"); tinyBank.withdraw(101,500); System.out.println(" tinyBank.withdraw(101,110)"); tinyBank.withdraw(101,110); System.out.println(" tinyBank.addInterest(101)"); tinyBank.addInterest(101); System.out.println(" tinyBank.addInterest(102)"); tinyBank.addInterest(102); System.out.println(" tinyBank.withdraw(102,500)"); tinyBank.withdraw(102,500); System.out.println(" tinyBank.withdraw(102,200)"); tinyBank.withdraw(102,200); System.out.println(); tinyBank.printAccouns(); } }
-----------------------------------------------------------------------
public class Account { private double bal; //The current balance private int accnum; //The account number public Account(int a) { bal=0.0; accnum=a; } public void deposit(double sum) { if (sum>0) bal+=sum; else System.out.println("Account.deposit(...): " +"cannot deposit negative amount."); } public void withdraw(double sum) { if (sum>0) bal-=sum; else System.out.println("Account.withdraw(...): " +"cannot withdraw negative amount."); } public double getBalance() { return bal; } public int getAccountNumber() { return accnum; } public String toString() { return "Acc " + accnum + ": " + "balance = " + bal; } public final void print() { //Don't override this, //override the toString method System.out.println( toString() ); } public void addInterest() { } }
-----------------------------------------------------------------------
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