Implement in C sharp to Create a class Account that should include one private i
ID: 3593361 • Letter: I
Question
Implement in C sharp to Create a class Account that should include one private instance variable Balance of type decimal to represent the account balance. The class should provide a constructor that receives an initial balance via its parameter and uses it to initialize the instance variable Balance. The class should have a public property BalanceP with a set accessor and a get accessor. When setting the account balance, the set accessor should validate the balance to ensure that it’s greater than or equal to 0.0. If not, give an error message and set the balance to 0.0. The get accessor of the property BalanceP returns the current balance. The class should provide two public methods. Method Creditshould add an amount to the current balance. Method Debit should withdraw money from the account and ensure that the debit amount does not exceed the account’s balance. If it does, the balance should be left unchanged, and the method should display the message "Debit amount exceeded account balance."
Create a derived class SavingsAccount that inherits from class Account. The derived class SavingsAccount should include a decimal instance variable Rate indicating the interest rate (percentage) assigned to the account. SavingsAccount’s constructor should receive the initial balance, as well as an initial value for the interest rate via its parameters. SavingsAccount should provide public method CalculateInterest that returns a decimal indicating the amount of interest earned by an account. Method CalculateInterest should determine this amount by multiplying the interest rate by the account balance. [Note: SavingsAccount should inherit methods Credit and Debitwithout redefining them.] After defining the class, create a class SavingsAccountTest that creates an instance of the class SavingsAccount and tests the methods and property of the base class and derived class. That is, the instance will show the initial balance, deposit some money, show the updated balance, withdraw some money, and show the updated balance
Explanation / Answer
using System;
class Example
{
public class Account
{
protected double Balance;
public Account(double bal)
{
Balance = bal;
}
public void setBalanceP(double bal)
{
if(bal >= 0)
Balance = bal;
else
Console.Write("cant set balance less athn zero ");
}
public double getBalance()
{
return Balance;
}
public int Debit(double amt)
{
int ret = 0;
if(amt <= Balance)
Balance-=amt;
if(amt > Balance)
{
Console.Write("Debit amount exceeded account balance ");
ret = -1;
}
return ret;
}
public void Credit(double amt)
{
Balance+=amt;
}
}
public class SavingsAccount: Account
{
private int interestRate;
public SavingsAccount(double bal,int interest_rate): base(bal)
{
interestRate = interest_rate;
}
public double CalculateInterest()
{
double amt;
amt = getBalance()*interestRate/100;
return amt;
}
}
static void Main()
{
SavingsAccount acct1 = new SavingsAccount(1000,5); //create the saving account with 1000 amt and 5 interest rate
double amt;
amt = acct1.getBalance();;
Console.Write(" Initial balance of SavingsAcoount 100 is = "+amt);
acct1.Credit(100);
amt = acct1.getBalance();
Console.Write(" Balance of SavingsAcoount 100 is = "+amt);
//calculate CalculateInterest
amt =acct1.CalculateInterest();
Console.Write(" Interest amount of SavingsAcoount is = "+amt);
//credit the interest rate
acct1.Credit(amt);
amt = acct1.getBalance();
Console.Write(" Balance of SavingsAcoount after credit interest rate of is = "+amt);
//now debit some amt from SavingsAcoount
acct1.Debit(200.75);
amt = acct1.getBalance();
Console.Write(" Balance of SavingsAcoount aftre debit is = "+amt);
}
}
-----------------------------------------------------------------
//otput
Initial balance of SavingsAcoount 100 is = 1000
Balance of SavingsAcoount 100 is = 1100
Interest amount of SavingsAcoount is = 55
Balance of SavingsAcoount after credit interest rate of is = 1155
Balance of SavingsAcoount aftre debit is = 954.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.