QUESTION:
Write a program to process bank accounts. Create a super class named Account and two subclasses named Savings and Checking. In the superclass, use an int AccountNum and a double balance for data members. Also, create two methods to do deposit and to do withdraw. Create one abstract method calculateDailyInterest that calculates and returns the daily interest. The method also adds the daily interest to the balance. The savings account is getting 3% annual interest rate. The checking account is getting 2% annual interest rate for amount that over $500.
Sample Run
(Note: user input is in red color)
Enter one of the following
1) Create a new Checking Account
2) Create a new Savings Account
3) Make a Deposit
4) Make a Withdraw
5) Display all accounts
6) Calculate Daily interest
7) Exit
1
A new Checking account with account number: 1
Enter the initial balance 1000
2
A new Savings account with account number: 2
Enter the initial balance 800
2
A new Savings account with account number: 3
Enter the initial balance 500
5
*********************************
Account 1 has balance 1000.00
Account 2 has balance 800.00
Account 3 has balance 500.00
*********************************
3
Which account to deposit? 1
Enter the amount of deposit: 50
5
*********************************
Account 1 has balance 1050.00
Account 2 has balance 800.00
Account 3 has balance 500.00
*********************************
4
Which account to withdraw? 2
Enter the amount of withdraw: 80
5
*********************************
Account 1 has balance 1050.00
Account 2 has balance 720.00
Account 3 has balance 500.00
*********************************
6
Account 1 gets interest 0.03
Account 2 gets interest 0.06
Account 3 gets interest 0.04
5
*********************************
Account 1 has balance 1050.03
Account 2 has balance 720.06
Account 3 has balance 500.04
*********************************
7
Problem Requirement
a) You must use a separate file for each class.
b) You must use a separate file for the Testing program.
c) Your program must produce the prompt and the output the same as given.
d) Your program should run correctly with the same inputs and outputs as given in the sample run.
e) The balances should be displayed with 2 decimal places.
Currently What I have:
//File Name :Account.java
//Account class
public abstract class Account
{
//data members
int accountnumber;
double accountbalance;
//constructor
public Account(int accn,double balance)
{
accountnumber = accn;
accountbalance = balance;
}
//deposit method
public void deposit(double amount)
{
accountbalance +=amount;
System.out.println("$"+amount +" deposited into your account");
System.out.println("Your account balance is $"+accountbalance);
}
//withdraw method
public void withdraw(double amount)
{
if(amount<accountbalance)
{
accountbalance -=amount;
System.out.println("You have drawn an amount of $"+amount);
System.out.println("Your account balance is $"+accountbalance);
}
else
{
System.out.println("Your account doesn't have sufficient balance");
}
}
//show method
public void show()
{
System.out.println("Account Details");
System.out.println("---------------");
System.out.println("Account Number :"+accountnumber);
System.out.println("Account Balance :"+accountbalance);
System.out.println("---------------");
}
//abstract method to calculate interest
abstract void calculateDailyInterest();
}
//File Name : Checking.java
//child class Checking
public class Checking extends Account
{
public Checking(int ano,double balance)
{
//calling base class constructor
super(ano,balance);
}
//calculating daily interest
void calculateDailyInterest()
{
accountbalance +=accountbalance+(accountbalance-500)*0.02;
}
}
//File Name : Savings.java
//child class Savings
public class Savings extends Account
{
public Savings(int ano, double balance)
{
//calling base class constructor
super(ano,balance);
}
//calculating daily interest
void calculateDailyInterest()
{
accountbalance +=accountbalance+accountbalance*0.05;
}
}
//File Name : AccountDemo.java
public class AccountDemo
{
public static void main(String args[])throws Exception
{
//declaring variables
Savings sAcc=new Savings(0,0.0);
Checking cAcc=new Checking(0,0.0);
int choice;
int ano;
double balance;
Scanner scan=new Scanner(System.in);
//do loop
do
{
System.out.println("1. create new checking account");
System.out.println("2. create new savings");
System.out.println("3. make deposit");
System.out.println("4. make withdraw");
System.out.println("5. display all accounts");
System.out.println("6. calculate daily interest");
System.out.println("7 exit");
choice=scan.nextInt();
switch(choice)
{
case 1:System.out.print("Enter Accoutn Number:");
ano=scan.nextInt();
System.out.print("Enter Accoutn Balance:");
balance=scan.nextDouble();
sAcc=new Savings(ano,balance);
break;
case 2:System.out.print("Enter Accoutn Number:");
ano=scan.nextInt();
System.out.print("Enter Accoutn Balance:");
balance=scan.nextDouble();
cAcc=new Checking(ano,balance);
break;
case 3:System.out.print("Enter Amount to deposit:");
balance=scan.nextDouble();
sAcc.deposit(balance);
cAcc.deposit(balance);
break;
case 4:System.out.print("Enter Amount to withdraw:");
balance=scan.nextDouble();
sAcc.withdraw(balance);
cAcc.withdraw(balance);
break;
case 5: sAcc.show();
cAcc.show();
break;
case 6: sAcc.calculateDailyInterest();
cAcc.calculateDailyInterest();
break;
case 7:System.exit(0);
}//end of switch
}while(true);//end of do-while
}//end of main
}//end of class