In: Computer Science
Using C#, modify the codes below to do the following:
Develop a polymorphic banking application using the Account
hierarchy created in the codes below.
Create an array of Account references to SavingsAccount and
CheckingAccount objects.
For each Account in the array, allow the user to specify an amount
of money to withdraw from the Account using method Debit and an
amount of money to deposit into the Account using method
Credit.
As you process each Account, determine its type. If an Account is a
SavingsAccount, calculate the amount of interest owed to the
Account using method
CalculateInterest, and then add the interest to the account balance using method Credit. After processing an Account, print the updated account balance obtained by using base class property Balance.
Codes to work with:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace INHERITANCE
{
public class Accounts
{
decimal bal;
public decimal Balance
{
get { return bal; }
set
{
if (value >= 0.0M)
{
bal = value;
}
else
{
bal = 0.0M;
Console.WriteLine("This account's amount is less than $0.00!");
}
}
}
public Accounts(decimal bal)
{
Balance = bal;
}
public virtual void credit(decimal amt)
{
bal += amt;
}
public virtual bool debit(decimal amt)
{
if (amt <= bal)
{
bal -= amt;
return true;
}
else
{
Console.WriteLine("Debit amount exceeded account balance!");
return false;
}
}
}
public class CheckingAccount : Accounts //derived class
CheckingAccount
{
decimal fee; //instance variable that represents the fee charged per transaction.
public CheckingAccount(decimal bal, decimal fee)
: base(bal)
{
this.fee = fee;
}
public override void credit(decimal amt)
{
base.credit(amt);
Balance -= fee;
}
public override bool debit(decimal amt) //charge a fee only if
money is actually withdrawn
{
if (base.debit(amt))
{
Balance -= fee;
return true;
}
else
{
return false;
}
}
}
class SavingsAccount : Accounts //derived class
SavingsAccount
{
decimal interestRate;
public SavingsAccount(decimal balance, decimal interestRate) : base(balance)
{
this.interestRate = interestRate;
}
public decimal CalculateInterest() //method that returns a decimal indicating the amount of interest earned by an account.
{
return Balance * interestRate / 100m;
}
}
class AccountsTest
{
static void Main(string[] args)
{
{
Accounts Account1 = new Accounts(250);
Console.WriteLine("Initial balance of Account 1: {0:C}",
Account1.Balance);
Account1.credit(354); //deposit amount
Account1.debit(200); //withdraw amount
Console.WriteLine("Final balance of Account 1: {0:C}",
Account1.Balance);
Console.WriteLine();
Accounts Account2 = new Accounts(-100); //test to see if initial
amount is less than or equal to $0.00
Console.WriteLine("Initial balance of Account 2: {0:C}",
Account2.Balance);
Account2.credit(20); //deposit amount
Account2.debit(45); //withdraw amount
Console.WriteLine("Final balance of Account 2: {0:C}",
Account2.Balance);
Console.WriteLine();
SavingsAccount Account3 = new SavingsAccount(83, 10); //test to
see if interest charge for transaction
Console.WriteLine("Initial amount of Account 3: {0:C}",
Account3.Balance);
Account3.credit(200); //deposit amount
Account3.debit(48); //withdraw amount
Console.WriteLine("Current balance of Account 3: {0:C}",
Account3.Balance);
decimal interest = Account3.CalculateInterest();
Account3.credit(interest); //charged fee
Console.WriteLine("Final balance of Account 3: {0:C}",
Account3.Balance);
Console.WriteLine();
CheckingAccount Account4 = new CheckingAccount(170, 2.5M);
Console.WriteLine("Initial balance of Account 4: {0:C}",
Account4.Balance);
Account4.credit(198); //deposit amount
Account4.debit(75); //withdraw amount
Console.WriteLine("Final balance of Account 4: {0:C}",
Account4.Balance);
Console.Read();
}
}
}
}
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace INHERITANCE
{
public class Accounts
{
decimal bal;
public decimal Balance
{
get { return bal; }
set
{
if (value >= 0.0M)
{
bal = value;
}
else
{
bal = 0.0M;
Console.WriteLine("This account's amount is less than
$0.00!");
}
}
}
public Accounts(decimal bal)
{
Balance = bal;
}
public virtual void credit(decimal amt)
{
bal += amt;
}
public virtual bool debit(decimal amt)
{
if (amt <= bal)
{
bal -= amt;
return true;
}
else
{
Console.WriteLine("Debit amount exceeded account balance!");
return false;
}
}
}
public class CheckingAccount : Accounts //derived class
CheckingAccount
{
decimal fee; //instance variable that represents the fee charged
per transaction.
public CheckingAccount(decimal bal, decimal fee)
: base(bal)
{
this.fee = fee;
}
public override void credit(decimal amt)
{
base.credit(amt);
Balance -= fee;
}
public override bool debit(decimal amt) //charge a fee only if
money is actually withdrawn
{
if (base.debit(amt))
{
Balance -= fee;
return true;
}
else
{
return false;
}
}
}
class SavingsAccount : Accounts //derived class
SavingsAccount
{
decimal interestRate;
public SavingsAccount(decimal balance, decimal interestRate) :
base(balance)
{
this.interestRate = interestRate;
}
public decimal CalculateInterest() //method that returns a decimal
indicating the amount of interest earned by an account.
{
return Balance * interestRate / 100m;
}
}
class AccountsTest
{
static void Main(string[] args)
{
{
Accounts[] account = new Accounts[3];
decimal amount,intrest;
account[0] = new Accounts(250); ;
account[1] = new SavingsAccount(83, 10);
account[2] = new CheckingAccount(170, 2.5M);
for(int i=0;i<account.Length;i++)
{
Console.WriteLine("\nInitial amount of Account {0:D}: {1:C}", (i +
1), account[i].Balance);
Console.Write("Enter amount to withdraw: ");
Decimal.TryParse(Console.ReadLine(), out amount);
account[i].debit(amount);
Console.Write("Enter amount to deposite: ");
Decimal.TryParse(Console.ReadLine(), out amount);
account[i].credit(amount);
if (account[i].GetType() == typeof(SavingsAccount))
{
intrest = (account[i] as SavingsAccount).CalculateInterest();
account[i].credit(intrest);
}
Console.WriteLine("Current amount of Account {0:D}: {1:C}",(i+1),
account[i].Balance);
}
}
}
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.