In: Computer Science
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;
}
//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