Question

In: Computer Science

Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a...

Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a BankAccountconstructed from data input from the keyboard. Override ReadAccount in SavingsAccount to return an account that refers to a SavingsAccount that you construct, again initializing it with data from the keyboard. Similarly, implement ReadAccount in the CheckingAccount class.

Use the following code to test your work.

public static void testCode()

        {

            SavingsAccount savings = new SavingsAccount(100.00, 3.5);

            SavingsAccount s = (SavingsAccount)savings.ReadAccount();

            CheckingAccount checking = new CheckingAccount(1000.00, .50);

            CheckingAccount c = (CheckingAccount)checking.ReadAccount();

            s.Deposit(135.22);

            s.PostInterest();

            s.Withdraw(50);

            Console.WriteLine

            ("The balance of SavingsAccount s is {0:C}",

                s.GetBalance());

            c.Deposit(1000.00);

            c.ProcessCheck(200.00);

            c.Withdraw(100.00);

            Console.WriteLine

            ("The balance of CheckingAccount c is {0:C}",

                c.GetBalance());

   }

Example 5.1:

using System;
public class BankAccount {
private double balance;   

public BankAccount() {
balance = 0;
}

public BankAccount(double initialAmount) {
balance = initialAmount;
}

public void Deposit(double amount) {   
balance += amount;
}

public virtual void Withdraw(double amount) {
if (balance >= amount)
balance -= amount;
else
Console.WriteLine("Insufficient funds");
}

public double GetBalance() {   
return balance;
}

Solutions

Expert Solution

//CODE in JAVA

import java.io.*;
import java.util.Scanner;

class BankAccount  
{
    Scanner scan=new Scanner(System.in); //scanner object to scan user details
    
    private int accountno;
    private String name;
    private double balance;

  
  
    public BankAccount()
    {
        balance = 0;
    }
  
    //ReadAccount method to form a BankConstructedData by taking input from the keyboard
    void ReadAccount()
   {
       System.out.print("Enter Account No: ");
       accountno=scan.nextInt();
       System.out.print("Enter Name: ");
       name=scan.next();
       System.out.print("Enter Balance: ");
       balance=scan.nextDouble();
   }

  
  
    public BankAccount(double initialAmount)
    {
        balance = initialAmount;
    }
  

    public void Deposit(double amount)
    {
        System.out.println("Balance is"+balance);
        balance += amount;
    }
  
    public void Withdraw(double amount)
    {
        if (balance >= amount)
        {
            balance -= amount;
        }
        else
            System.out.println("Insufficient Funds");
    }
  
    public double GetBalance()
    {
        return balance;
    }

}
class SavingsAccount extends BankAccount
{
    Scanner scan=new Scanner(System.in);
    private double balance;
    private double interest_rate;
    private int accountno;
    private String name;


    //constructor to set initial values
  
    public SavingsAccount(double balance,double interest_rate)
    {
        this.balance=balance;
        this.interest_rate=interest_rate;
    }
  
    //overriding BankAccount methods
    void ReadAccount()
   {
       System.out.print("Enter Account No: ");
       accountno=scan.nextInt();
       System.out.print("Enter Name: ");
       name=scan.next();
       System.out.print("Enter Balance: ");
       balance=scan.nextDouble();
   }
  


  
   void PostInterest()
   {
        //calculating simple interest for 5 years
        //here principle is the balance
        //interest=(p*t*r)/100
        System.out.println("The interest for 5 years is "+((balance*5*interest_rate)/100));
   }
  
  
  
    public void Deposit(double amount)
    {
        balance += amount;
    }
  
    public void Withdraw(double amount)
    {
        if (balance >= amount)
            balance -= amount;
        else
            System.out.println("Insufficient Funds");
    }
  
    public double GetBalance()
    {
        return balance;
    }
  

}

class CheckingAccount extends BankAccount
{
    Scanner scan=new Scanner(System.in);
    private double balance;
    private double interest_rate;
    private int accountno;
    private String name;


  
    //constructor to set initial values
    public CheckingAccount(double balance,double interest_rate)
    {
        this.balance=balance;
        this.interest_rate=interest_rate;
    }
  
    //overriding BankAccount methods
    void ReadAccount()
   {
       System.out.print("Enter Account No: ");
       accountno=scan.nextInt();
       System.out.print("Enter Name: ");
       name=scan.next();
       System.out.print("Enter Balance: ");
       balance=scan.nextDouble();
   }
  
   //method to process a check
   void ProcessCheck(double check)
   {
       System.out.println("Processing the check of "+check);
  
       if (balance >= check)
       {
            balance -= check;
            System.out.println("Processing the check has finished");
       }
          
        else
            System.out.println("Cannot Process Check due to Insufficient Funds");
     
     
   }
  
  

    public void Deposit(double amount)
    {
        balance += amount;
    }
  
    public void Withdraw(double amount)
    {
        if (balance >= amount)
            balance -= amount;
        else
            System.out.println("Insufficient Funds");
    }
  
    public double GetBalance()
    {
        return balance;
    }
  

}

public class Main
{

// Modifying some part of testCode() to satisfy above class conditions
public static void main(String args[])

        {

            SavingsAccount s = new SavingsAccount(100.00, 3.5);

            s.ReadAccount();

            s.Deposit(135.22);

            s.PostInterest();

            s.Withdraw(50);

            System.out.println("The balance of SavingsAccount s is "+s.GetBalance());
          
            CheckingAccount c = new CheckingAccount(1000.00, .50);

            c.ReadAccount();

            c.Deposit(1000.00);

            c.ProcessCheck(200.00);

            c.Withdraw(100.00);

            System.out.println("The balance of CheckingAccount c is "+c.GetBalance());

   }
}

//OUTPUT

//ANY QUERIES ,PING ME IN COMMENT SECTION


Related Solutions

Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000 (the insurance limit of the U.S. government). The method should block until sufficient money has been withdrawn by another thread. Test your program with a large number of deposit threads. (All other classes are provided below) Bank Account.java (This class is the one that needs to be modified) /** A bank account has a balance that can be changed by deposits and withdrawals. */...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
/* Work to do: 1) add toString method to BankAccount and SavingsAccount classes 2) Override the...
/* Work to do: 1) add toString method to BankAccount and SavingsAccount classes 2) Override the withdraw() in SavingsAccount so that it will not withdraw more money than is currently in the account. 3) Provide constructors for SavingsAccount 4) Add this feature to SavingsAccount: If you withdraw more than 3 times you are charged $10 fee and the fee is immediately withdrawn from your account.once a fee is deducted you get another 3 free withdrawals. 5) Implement the Comparable Interface...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try and catch" statement to catch an exception if "empStatus == 1" hourly wage is not between $15.00/hr. and $25.00/hr. The keywords “throw” and “throws” MUST be used correctly and both keywords can be used either in a constructor or in a method. If an exception is thrown, the program code should prompt the user for the valid input and read it in. *NOTE*- I...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (using name & id), and - keeps track of a user's available balance. - how many transactions (deposits and/or withdrawals) are made. public class BankAccount { private int id; private String name private double balance; private int numTransactions; // ** Please define method definitions for Accessors and Mutators functions below for the class private member variables string getName();...
1. what gets printed if anything after executing the main method of class BankAccount below: public...
1. what gets printed if anything after executing the main method of class BankAccount below: public class BankAccount {      private static int masterKey =1;    private String name;      private int accountNumber;      private double balance = 0;      private int age = 1;      public BankAccount (String myName , double balance) {      name = myName;      this.balance = 5 + 2 * balance ;      accountNumber = masterKey;      age++;    masterKey++;    printMe(); } public void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT