1

I have a parent class, account, with two derived, SavingsAccount and CheckingsAccount. Those child classes have their own variants of the methods. I want to call the method of the object which is store in the array. So the array of type Account holds objects of type SavingsAccount and CheckingsAccount.

I thought I could call it by doing the Account[0].CalculateInterest

CalculateInterest in a method inside SavingsAccount.

This error is thrown

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomersPartTwo
{
    class Program
    {
        static void Main(string[] args)
        {
            Account[] customerAccounts = new Account[4];
            decimal temp = 0;

            customerAccounts[0] = new SavingsAccount(25, 0.03m);
            customerAccounts[1] = new SavingsAccount(200, 0.015m);
            customerAccounts[2] = new CheckingAccount(80, 1);
            customerAccounts[3] = new CheckingAccount(400, 0.5m);


            Console.WriteLine("Account 1 balance: " + customerAccounts[0].Balance.ToString("C"));

            Console.WriteLine("Enter an amount to withdraw: ");
            temp = Convert.ToDecimal(Console.ReadLine());
            customerAccounts[0].Debit(temp);

            Console.WriteLine("Enter an amount to deposit: ");
            temp = Convert.ToDecimal(Console.ReadLine());
            customerAccounts[0].Credit(temp);

            if (customerAccounts[0].GetType() == typeof(SavingsAccount))
            {
                customerAccounts[0].Credit(SavingsAccount.CalculateInterest());
            }

        }


        /* Base Class ------------------------------------*/
        class Account
        {
            private decimal balance;

            /* Property */
            public decimal Balance
            {
                get { return balance; }
                set
                {
                    if (value < 0)
                        balance = 0;
                    else
                        balance = value;
                }
            }

            /* Constructor */
            public Account(decimal initialBalance)
            {
                Balance = initialBalance;
            }

            /* Method to add to balance */
            public void Credit(decimal addedFunds)
            {
                Balance += addedFunds;
            }

            /* Method to subtract from balance */
            public void Debit(decimal subtractedFunds)
            {
                if (subtractedFunds > Balance)
                    Console.WriteLine("Debit amount exceeds balance amount");
                else
                    Balance -= subtractedFunds;
            }
        }
        /* Base Class ------------------------------------*/


        class SavingsAccount : Account
        {
            private decimal interestRate;

            /* Constructor */
            public SavingsAccount(decimal initialBalance, decimal rateOfInterest) : base(initialBalance)
            {
                interestRate = rateOfInterest;
            }

            /* Method to calculate interest */
            public decimal CalculateInterest()
            {
                decimal earnedInterest = 0;

                earnedInterest = Balance * interestRate;

                return earnedInterest;
            }
        }


        class CheckingAccount : Account
        {
            private decimal transactionFee;

            /* Constructor */
            public CheckingAccount(decimal initialBalance, decimal usageFee) : base(initialBalance)
            {
                transactionFee = usageFee;
            }

            /* Method to add to balance */
            public new void Credit(decimal addedFunds)
            {
                Balance += addedFunds;
                Balance -= transactionFee;
            }

            /* Method to subtract from balance */
            public new void Debit(decimal subtractedFunds)
            {
                if (subtractedFunds > Balance)
                    Console.WriteLine("Debit amount exceeds balance amount");
                else
                    Balance -= (subtractedFunds + transactionFee);              
            }

        }
    }
}
3
  • You need to invoke CalculateInterest on an instance of Account, e.g customerAccounts[0].Credit(customerAccounts[0].CalculateInterest()). Can you show us your Account class as well? Does CalculateInterest exist on this class also? If not, you may need to cast your instance to SavingsAccount. Commented Feb 19, 2019 at 20:47
  • You also might consider if there really is a difference between a checking account and a savings account, other than rules-based (property-based) differences (like minimum balance, maximum withdrawl, maintenance fees, etc.), which can be specified as properties on an instance. If this is the case, then it might make sense to just have a BankAccount class, and then create instances of that, like BankAccount checkingAccount = new BankAccount(); This way your method only needs to worry about a single type. Commented Feb 19, 2019 at 21:27
  • @Jeppe I tried this: if (customerAccounts[x].GetType() == typeof(SavingsAccount)) { temp = customerAccounts[x].CalculateInterest(); customerAccounts[x].Credit(temp); } Still not working. It seems like it's not acknowledging the data type of the object inside the array, or something. Getting a "does not contain definition" error. Commented Feb 19, 2019 at 23:30

1 Answer 1

2

Can you try in this way?

if (customerAccounts[0] is SavingAccount sa)
{
    customerAccounts[0].Credit(sa.CalculateInterest());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.