In: Computer Science
Design a class named BankAccount to hold the following data for a bank account:
Balance
Number of deposits this month
Number of withdrawals
Annual interest rate
Monthly service charges
The class should have the following methods:
Constructor:
The constructor should accept arguments for the balance and annual interest rate.
deposit:
A method that accepts an argument for the amount of the deposit. The method should add the argument to the account balance. It should also increment the variable holding the number of deposits.
withdraw:
A method that accepts an argument for the amount of the withdrawal. The method should subtract the argument from the balance. It should also increment the variable holding the number of withdrawals.
calcInterest:
A method that updates the balance by calculating the monthly interest earned by the account, and adding this interest to the balance. This is performed by the following formulas:
Monthly Interest Rate 5 (Annual Interest Rate / 12)
Monthly Interest 5 Balance * Monthly Interest Rate
Balance 5 Balance 1 Monthly Interest
monthlyProcess:
A method that subtracts the monthly service charges from the balance, calls the calcInterest method and then sets the variables that hold the number of withdrawals, number of deposits, and monthly service charges to zero.
Next, design a SavingsAccount class that extends the BankAccount class. The SavingsAccount class should have a status field to represent an active or inactive account. If the balance of a savings account falls below $25, it becomes inactive. (The status field could be a boolean variable.) No more withdrawals may be made until the balance is raised above $25, at which time the account becomes active again. The savings account class should have the following methods:
withdraw:
A method that determines whether the account is inactive before a withdrawal is made. (No withdrawal will be allowed if the account is not active.) A withdrawal is then made by calling the superclass version of the method.
deposit:
A method that determines whether the account is inactive before a deposit is made. If the account is inactive and the deposit brings the balance above $25, the account becomes active again. A deposit is then made by calling the superclass version of the method.
monthlyProcess:
Before the superclass method is called, this method checks the number of withdrawals. If the number of withdrawals for the month is more than 4, a service charge of $1 for each withdrawal above 4 is added to the superclass field that holds the monthly service charges. (Don’t forget to check the account balance after the service charge is taken. If the balance falls below $25, the account becomes inactive.)
Input
100
0.03
2.5
Where,
First line represents the balance.
Second line represents the interest rate.
Third line represents the monthly service charge.
Output
Balance: $100.00
Number of deposits: 0
Number of withdrawals: 0
Account Status: Active
Balance: $170.00
Number of deposits: 3
Number of withdrawals: 0
Account Status: Active
Balance: $20.00
Number of deposits: 3
Number of withdrawals: 2
Account Status: Incative
Balance: $17.54
Number of deposits: 0
Number of withdrawals: 0
Account Status: Incative
# Use the provided template for the demo class to fill the missing code to input data and output result as shown above. Do not change the withdraw and deposit code!
kindly check the code, might be helpful for you please thumbs up as this program will help you lots.
import java.io.*;
import java.util.Scanner;
public class SavingsAccount
{
public static abstract class BankAccount
{
private double balance, annualInterestRate;
private int numberDeposit = 0, numberWithdrawal = 0;
public BankAccount(double bal, double rate)
{
balance = bal;
annualInterestRate = rate;
}
public void deposit(double amtDeposit)
{
balance += amtDeposit;
numberDeposit++;
}
public void withdraw(double amtWithdrawal)
{
balance -= amtWithdrawal;
numberWithdrawal++;
}
public void calcInterest()
{
double monthlyInterest;
monthlyInterest = balance * annualInterestRate / 12;
balance += monthlyInterest;
}
public abstract void monthlyProcess();
public void setBalance(double bal)
{
balance += bal;
}
public void setNumberDeposit(int num)
{
numberDeposit = num;
}
public void setNumberWithdrawal(int num2)
{
numberWithdrawal = num2;
}
public double getBalance()
{
return balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public int getNumberDeposit()
{
return numberDeposit;
}
public int getNumberWithdrawal()
{
return numberWithdrawal;
}
}
public static class SavingsAccount extends BankAccount
{
private boolean active;
public SavingsAccount(double bal, double rate)
{
super(bal, rate);
if (super.getBalance() > 25)
active = true;
else
active = false;
}
public void withdraw(double amtWithdrawal)
{
if (active == true && super.getBalance() >= amtWithdrawal)
{
super.withdraw(amtWithdrawal);
if (super.getBalance() <= 25)
{
System.out.println("Your balance is less than minimum balance. Your account is now INACTIVE ");
active = false;
}
if (super.getNumberWithdrawal() > 4)
{
System.out.println("You have exceeded monthly limit of withdrawals. Fee of $1 charged\n");
super.setBalance(-1);
}
}
else
{
System.out.println("ERROR: Transaction declined!! This transaction will cause overdraft or zero balance");
}
}
public void deposit(double amtDeposit)
{
super.deposit(amtDeposit);
if (active == false && super.getBalance() > 25)
{
active = true;
System.out.println("Your account is now ACTIVE\n");
}
}
public void monthlyProcess()
{
super.calcInterest();
super.setNumberDeposit(0);
super.setNumberWithdrawal(0);
System.out.printf("Your Balance after Monthly process is: %.2f", super.getBalance());
System.out.println();
}
public boolean getActive()
{
return active;
}
}
public static class NegativeInput extends Exception
{
public NegativeInput()
{
super("Error: Must enter positive value\n");
}
public NegativeInput(boolean act)
{
super("Error: Your account is INACTIVE\n");
}
}
public static void displayMenu()
{
System.out.println("Enter D to deposit");
System.out.println("Enter W to Withdraw");
System.out.println("Enter B for Balance");
System.out.println("Enter M for Monthly Process");
System.out.println("Enter E to Exit");
}
public static void main(String[] args) throws Exception
{
double balance, annualInterestRate, amtDeposit, amtWithdrawal;
String choice;
Scanner sc = new Scanner(System.in);
System.out.print("Enter beginning balance :$");
balance = sc.nextDouble();
System.out.print("Enter interest rate(whole number) :%");
annualInterestRate = sc.nextDouble() / 100;
sc.nextLine();
SavingsAccount savingsAccount = new SavingsAccount(balance, annualInterestRate);
do
{
do
{
displayMenu();
choice = sc.nextLine().toUpperCase();
if (choice.charAt(0) != 'D' && choice.charAt(0) != 'W' && choice.charAt(0) != 'B' && choice.charAt(0) != 'M' &&choice.charAt(0) != 'E')
System.out.println("Invalid choice. Try again\n");
} while (choice.charAt(0) != 'D' && choice.charAt(0) != 'W' && choice.charAt(0) != 'B' && choice.charAt(0) != 'M' &&choice.charAt(0) != 'E');
switch (choice)
{
case "D":
System.out.print("Enter the amount you want to Deposit :$");
amtDeposit = sc.nextDouble();
sc.nextLine();
try
{
if (amtDeposit <= 0)
throw new NegativeInput();
savingsAccount.deposit(amtDeposit);
}
catch(NegativeInput e)
{
System.out.println(e.getMessage());
}
break;
case "W":
try
{
if (savingsAccount.getActive() == false)
throw new NegativeInput(false);
}
catch(NegativeInput e)
{
System.out.println(e.getMessage());
break;
}
System.out.print("Enter the amount you want to withdraw :$");
amtWithdrawal = sc.nextDouble();
sc.nextLine();
try
{
if (amtWithdrawal <= 0)
throw new NegativeInput();
savingsAccount.withdraw(amtWithdrawal);
}
catch(NegativeInput e)
{
System.out.println(e.getMessage());
}
break;
case "B":
System.out.printf("Your Balance is: %.2f", savingsAccount.getBalance());
System.out.println();
break;
case "M":
savingsAccount.monthlyProcess();
break;
case "E":
System.out.printf("Balance : $%.2f", savingsAccount.getBalance());
System.out.println("\nThank you. Bye");
break;
default:
break;
}
} while (choice.charAt(0) != 'E');
}
}