please do on C# Account Inheritance Hierarchy Create an inheritance hierarchy th
ID: 3802648 • Letter: P
Question
please do on C#
Account Inheritance Hierarchy
Create an inheritance hierarchy that a bank might use to represent customers' bank accounts. All customers at this bank can deposit (i.e., credit) money into their accounts and withdraw (i.e., debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction.
Create base class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base class Account should include one private instance variable of type decimal to represent the account balance. The class should provide a constructor that receives an initial balance and uses it to initialize the instance variable with a public property. The property should validate the initial balance to ensure that it’s greater than or equal to 0.0; if not, throw an exception. The class should provide two public methods. Method Credit should 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.” The class should also provide a get accessor in the property Balance that returns the current balance.
Derived class SavingsAccount should inherit the functionality of an Account, but also include a decimal instance variable 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. 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 Debit without redefining them.] [Note: Interest rate is the percentage divided by 100, divided by 12, and then multiplied by the balance.]
Derived class CheckingAccount should inherit from base class Account and include a decimal instance variable that represents the fee charged per transaction. CheckingAccount’s constructor should receive the initial balance, as well as a parameter indicating a fee amount. Class CheckingAccount should redefine methods Credit and Debit so that they subtract the fee from the account balance whenever either transaction is performed successfully. CheckingAccount’s versions of these methods should invoke the base-class Account version to perform the updates to an account balance. CheckingAccount’s Debit method should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance). [Hint: Define Account’s Debit method so that it returns a bool indicating whether money was withdrawn. Then use the return value to determine whether a fee should be charged.]
After defining the classes in this hierarchy, write an app that creates objects of each class and tests their methods. Add interest to the SavingsAccount object by first invoking its CalculateInterest method, then passing the returned interest to the object’s Credit method.
Test Data:
Account 1 – Savings Account – Interest rate 5%
Starting balance: $1,000.00
Deposit: $500.00
Calculate Interest
Withdraw: $2,000.00
Calculate Interest
Withdraw: $1,500.00
Display Balance
Account 2 – Checking Account – Fee per transaction $1
Starting balance: $500.00
Withdraw: $250.00
Deposit: $300.00
Withdraw: $750.00
Display balance
Explanation / Answer
#include<iostream.h>
class account
{
protected:
double balance;
public:
account(double bal)
{ if(bal>=0.0)
{balance=bal;}
else
{balance=0;
cout<<"initial balance invalid ";
}
}
void credit()
{
cout<<"Enter the amount to add ";
int add;
cin>>add;
balance=balance+add;
}
void debit()
{
cout<<"Enter the amount to withdraw ";
int get;
cin>>get;
if(get>=balance)
{
balance=balance-get;
}
else
cout<<"Debit amount exceeded account balance ";
}
void getbalance()
{
cout<<"The current balance is:"<<balance<<endl;
}
};
class savingsaccount:public account
{
private:
double interestrate;
public:
savingsaccount(double interest):account()
{
interestrate=interest;
}
double calculateinterest()
{
double interestearned=interestrate*balance;
return interestearned;
}
};
class checkingaccount:public account
{
private:
double fee;
public:
checkingaccount(double fee1):account()
{
fee=fee1;
}
void credit()
{cout<<"Enter the amount to add ";
int add;
cin>>add;
balance=balance+add;
balance=balance-fee;
cout<<"Fee deducted ";
}
void debit()
{
cout<<"Enter the amount to withdraw ";
int get;
cin>>get;
if(get>=balance)
{
balance=balance-get;
balance=balance-fee;
cout<<"Fee deducted ";
}
else
cout<<"Debit amount exceeded account balance ";
}
};
void main()
{
cout<<"Enter the initial balance ";
int initial;
cin>>initial;
account a(initial);
cout<<"Enter interest rate ";
double interest;
cin>>interest;
savingsaccount s(interest);
cout<<"Enter fee ";
double fee;
cin>>fee;
checkingaccount c(fee);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.