Inheritance hierarchy

Apr 12 2019 3:40 PM

Facing issues with these part of the instructions, specifically implementing the PrintAccount method and private instance variables AccountName & AccountNumber

Create base class Account and derived classes SavingsAccount and CheckingAccount that inherit from class Account. Base class Account should include the following private instance variables: Balance, which is of type decimal to represent the account balance; AccountName, which is of type string and represents the account holder’s last name; and AccountNumber, which is an integer type that represents the account’s number. The class should provide a constructor that receives an account’s name, account number, and an initial balance. It should use initialize these instance variables using the appropriate mutator methods (i.e. setAccountName, setAccountNumber, and setBalance). The setBalance method should validate the initial balance to ensure that it’s greater than or equal to 0.0; if not, set the balance to 0. You should also include the appropriate accessor (i.e. “get”) methods. Also, the class should provide two other 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 print the message “Insufficient Funds.” Base class Account should also have a method called PrintAccount that prints the account’s name, number, and balance.

Derived class SavingsAccount should inherit the functionality of an Account, but also include a decimal instance variable indicating the interest rate (double) assigned to the Account. Call this variable InterestRate. SavingsAccount’s constructor should receive the account’s name, account number, initial balance, and an initial value for the interest rate. The constructor should call the base class constructor to initialize the account’s name, number, and balance. It should also call a method in its own class, setInterestRate, which should set the InterestRate variable and validate that the rate is a positive number. If the interest rate passed in is negative, set the interest rate to zero. SavingsAccount should provide public method CalculateInterest that takes no arguments and 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 modifying them.] Finally, create a method in this derived class that overrides the PrintAccount method in the base class. In it, call the base class method to print out the account’s name, number, and balance, and include code in the derived class’s method to print out the information specific to the derived class (i.e. InterestRate).

Derived class CheckingAccount should inherit from base class Account and include a decimal instance variable that represents the fee charged per transaction. Call this variable FeeCharged. CheckingAccount’s constructor should receive the account’s name, account number, initial balance, as well as a parameter indicating a fee amount. Create a mutator method, setFeeAmount, and call it from the constructor. If the fee amount is negative, the setFeeAmount should set it to zero. 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 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.] Finally, create a method in this derived class that overrides the PrintAccount method in the base class. In it, call the base class method to print out the account’s name, number, and balance, and include code in the derived class’s method to print out the information specific to the derived class (i.e. FeeCharged).

Work so far:
  1. using System;  
  2.   
  3. namespace AccountTest  
  4. {  
  5.     public class AccountTest  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("Created checking account with $1,000 balance.");  
  10.             CheckingAccount Checking1 = new CheckingAccount("Checking", 1, 1000, 3);  
  11.             Console.WriteLine("Created savings account with $2,000 balance.");  
  12.             SavingsAccount Savings1 = new SavingsAccount("Savings", 2, 2000, 0.05);  
  13.   
  14.             Checking1.PrintAccount();  
  15.             Savings1.PrintAccount();  
  16.   
  17.             Console.Write("\nDeposit $100 into checking.");  
  18.             Checking1.Credit(100);  
  19.             Checking1.PrintAccount();  
  20.   
  21.             Console.Write("\nWithdraw $50 from checking.");  
  22.             Checking1.Debit(50);  
  23.             Checking1.PrintAccount();  
  24.   
  25.             Console.Write("\nWithdraw $6,000 from checking.");  
  26.             Checking1.Debit(6000);  
  27.             Checking1.PrintAccount();  
  28.   
  29.             Console.Write("\nDeposit $3,000 into savings.");  
  30.             Savings1.Credit(3000);  
  31.             Savings1.PrintAccount();  
  32.   
  33.             Console.Write("\nWithdraw $200 from savings.");  
  34.             Savings1.Debit(200);  
  35.             Savings1.PrintAccount();  
  36.   
  37.             Console.Write("\nCalculate Interest on savings.");  
  38.             Savings1.CalculateInterest();  
  39.             Savings1.PrintAccount();  
  40.   
  41.             Console.Write("\nWithdraw $10,000 from savings.");  
  42.             Savings1.Debit(10000);  
  43.             Savings1.PrintAccount();  
  44.   
  45.             Console.WriteLine("\n\n\n");  
  46.             return;  
  47.         }  
  48.     }  
  49. }  
  1. using System;  
  2.   
  3. namespace AccountTest {  
  4.   
  5.     public class Account {  
  6.   
  7.         private decimal balance;  
  8.   
  9.         public Account(decimal b) {  
  10.             Balance = b; }  
  11.   
  12.         public decimal Balance {  
  13.             get {  
  14.                 return balance; }  
  15.             set {  
  16.                 if (value > 0)  
  17.                 balance = value;  
  18.   
  19.                 else  
  20.                 throw new ArgumentOutOfRangeException("Balance", value, "Balance should be greater than 0"); }  
  21.         }  
  22.         public virtual bool Debit(decimal ammount) {  
  23.   
  24.             if ((Balance - ammount) > 0) {  
  25.                 Balance -= ammount;  
  26.                 return true; }  
  27.   
  28.             else {  
  29.   
  30.                 Console.WriteLine("Debit ammount exceeded Account Balance");  
  31.   
  32.                 return false; }  
  33.         }  
  34.         public virtual  void Credit(decimal ammount) {  
  35.             Balance = +ammount; }  
  36.     }  
  37. }  
  38.   
  39.    
  1. namespace AccountTest {  
  2.   
  3.     public class CheckingAccount : Account {  
  4.   
  5.         private decimal fee;  
  6.   
  7.         public CheckingAccount(decimal b, decimal f) :base(b) {  
  8.   
  9.             fee = f; }  
  10.   
  11.         public decimal Fee {  
  12.             get {  
  13.                 return fee; }  
  14.         }  
  15.   
  16.         public override bool Debit(decimal ammount) {  
  17.             if (base.Debit(ammount)) {  
  18.   
  19.                 base.Debit(fee);  
  20.                 return true; }  
  21.           
  22.             else {  
  23.                 return false; }  
  24.         }  
  25.   
  26.            public override void Credit(decimal ammount) {  
  27.   
  28.             base.Credit(ammount);  
  29.             base.Debit(fee); }  
  30.     }  
  31. }  
  1. namespace AccountTest  
  2. {  
  3.     public class SavingsAccount : Account {  
  4.   
  5.         private decimal interestrate;  
  6.   
  7.         public SavingsAccount(decimal b, decimal ir) : base(b) {  
  8.             interestrate = ir; }  
  9.   
  10.         public decimal InterestRate {  
  11.             get {  
  12.                 return interestrate; }  
  13.         }  
  14.   
  15.             public decimal CalculateInterest() {  
  16.             return Balance * InterestRate; }  
  17.         }  
  18. }

Answers (1)