LANGUAGE IN C# Develop a polymorphic banking app using the Account hierarchy cre
ID: 3763731 • Letter: L
Question
LANGUAGE IN C#
Develop a polymorphic banking app using the Account hierarchy created in Exercise 11.8. 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, then add the interest to the account balance using method
Credit. After processing an Account, display the updated account balance obtained by using baseclass
property Balance.
Explanation / Answer
using System; namespace CETH2261 { public class Application { static void Main(string[] args) { SavingAccount John_Doe = new SavingAccount(100M, 0.05M); John_Doe.Credit( John_Doe.CalculateInterest() ); Console.WriteLine("balance after interest amount of saving account: {0}", John_Doe.Balance); CheckingAccount Jane_Doe = new CheckingAccount(100M, 0.25M); Jane_Doe.Credit(15.45M); Console.WriteLine("balance after crediting checking account: {0}", Jane_Doe.Balance); Console.WriteLine(""); Jane_Doe.Debit(15.45M); Console.WriteLine("balance after debiting checking account: {0}", Jane_Doe.Balance); Console.WriteLine(""); } // end Main } // end class Application public class Account { private decimal balance; public decimal Balance { get { return balance; } set { if (value < 0) { balance = 0; Console.WriteLine("Initial balance was invalid"); } else balance = value; } // end set } // end property Balance public Account(decimal B) { Balance = b; } // end constructor Account public virtual void Credit(decimal a) { balance += a; } // end method Credit public virtual bool Debit(decimal a) { if (balance < a) { Console.WriteLine("Debit amount exceeded account balance."); return false; } else { balance -= a; return true; } } // end method Debit } // end class Account public class SavingAccount : Account { decimal interestRate; public SavingAccount(decimal b, decimal rate) : base(B) { interestRate = rate; } // end constructor SavingAccount public decimal CalculateInterest() { return interestRate * Balance; } // end method CalculateInterest } // end class SavingAccount public class CheckingAccount : Account { private decimal fee; public CheckingAccount( decimal b, decimal f ) : base( b ) { fee = f; } // end constructor CheckingAccont public override void Credit(decimal a) { base.Credit(a); // credit account with amount a using base class's Credit method Balance -= fee; // deduct fee } public override bool Debit(decimal a) { if (base.Debit(a) == true) { Balance -= fee; // deduct fee return true; } else return false; } } // end class CheckingAccount } // end namespace CETH2261
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.