In: Computer Science
Needs to be written in C# Console App! Create a base class BankAccount. Decide what characteristics are common for checking and saving accounts and include these characteristics in the base class. Define derived classes for checking and savings. In your design , do not allow the banking base account to be instantiated --only the checking and saving classes. Use a Program class to test your design.
using System;
class BankAccount //base class
{
protected int accountNumber; //protected data members
can be used in derived classes
protected double balance;
public void deposit(double amount) //define deposit
method
{
balance = balance +
amount;
}
}
class SavingAccount: BankAccount //derived class
{
public SavingAccount(int accountNumber,double
balance) //constructor
{
this.accountNumber =
accountNumber;
this.balance =
balance;
}
public void withdraw(double amount) //withdraw
method
{
if(balance <
amount)
Console.WriteLine("Cannot be done");
else
balance = balance -
amount;
}
public override string ToString()
{
return "Saving Account :
Account number = "+accountNumber+"Balance = "+balance;
}
}
class CheckingAccount : BankAccount //derived class
{
private double overdraftLimit;
public CheckingAccount(int accountNumber,double
balance,double overdraftLimit) //constructor
{
this.accountNumber =
accountNumber;
this.balance =
balance;
this.overdraftLimit =
overdraftLimit;
}
public void withdraw(double amount) //withdraw
method checks overdraft limit also
{
if(balance +
overdraftLimit < amount)
Console.WriteLine("Cannot be done");
else
balance = balance -
amount;
}
public override string ToString()
{
return "Checking Account
: Account number = "+accountNumber+" Balance = "+balance;
}
}
public class AccountTest
{
public static void Main()
{
SavingAccount sa = new
SavingAccount(1456,677.50);
sa.deposit(200);
Console.WriteLine(sa.ToString());
CheckingAccount ca = new
CheckingAccount(1004,677.50,200);
sa.withdraw(1000);
Console.WriteLine(ca.ToString());
}
}
Output:
Saving Account : Account number = 1456Balance = 877.5
Cannot be done
Checking Account : Account number = 1004 Balance = 677.5