Question

In: Computer Science

Design a class named BankAccount to hold the following data for a bank account: Balance Number...

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!

Solutions

Expert Solution

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');      
        }
}

Related Solutions

13. The class bankAccount stores a bank customer’s account number and balance. Suppose that account number...
13. The class bankAccount stores a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. This class provides the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. b. The class  checkingAccount from the class bankAccount (designed in part a). This class inherits members to store the account number and the balance from the base class. A customer...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
Create a class BankAccount to hold at least the following data / information about a bank...
Create a class BankAccount to hold at least the following data / information about a bank account: Account balance Total number of deposits Total number of withdrawals Interest rate e.g., annual rate = 0.05 Service charges per month The class should have the following member functions: Constructor To set the required data. It may be parameterized or user’s input. depositAmount A virtual function used to accept an argument for the amount to be deposited. It should add the argument (amount)...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account Number and Account Balance. There are also other variables called MIN_BALANCE=9.99, REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04. They look like constants, but for now, they are variables of type double Here is the UML for the class:                                                         BankAccount -string accountName // First and Last name of Account holder -int accountNumber // integer -double accountBalance // current balance amount + BankAccount()                     //default constructor that sets name to “”,...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Design a class named Account that contains: ■ A private int data field named id for...
Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. ■ A private Date data field named dateCreated that stores the date when the account was created. ■ A no-arg constructor that creates...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and the balance. It has methods to deposit and withdraw money. In addition, the default constructor sets account number to 0000 and balance 0. Other constructor should take the account number and the initial balance as parameters. Finally, the account class should have a static data member that stores how many accounts are created for a given application. a) Draw the UML diagram b) Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT