0

Here i have my deposit class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace CollectionAccounts
{
    public class DepositAccount : Account,IDisplayable
    {
        private double interestRate;
        public DepositAccount(string id)
            : base(id)
        {
            interestRate = 0;
        }

        public double InterestRate
        {
            get;private set;
        }
        public void AddInterest()
        {
            balance *= 1 + interestRate / 100;
        }

        public override void Withdraw(double amount)
        {
            if (amount <= (balance))
            {
                balance -= amount;


            }
            else
                Console.WriteLine("Not allowed as amount exeeds balance\n");
        }
       public void Display()
        {
            Console.WriteLine("The id is {0} and balance is {1}", id, balance);

        }
    }
}

...............................................................................................
The problem resides in the last line here:

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

namespace CollectionAccounts
{
    class Program
    {
        static void Main(string[] args)
        {
        Account[] obj = new Account[2];

        obj[0] = new CurrentAccount("ca001");
        obj[1] = new DepositAccount("da001");


        obj[0].Lodge(200);
        ((CurrentAccount)obj[0]).Creditlimit = 500;
        obj[0].Withdraw(500);
        ((CurrentAccount)obj[0]).Display();


          obj[1].Lodge(200);
          ((DepositAccount)obj[1]).interestRate = 2.5;  ?????Does not work



        }
    }
}

I tried ((DepositAccount)obj[1]).AddInterest = 2.5;
Does not work too. Please help.

2
Contributors
4
Replies
18
Views
2 Years
Discussion Span
Last Post by PulsarScript
0

InterestRate has a private setter, so it's not accessible to callers. AddInterest is a method with no arguments, so the correct call would be:

((DepositAccount)obj[1]).AddInterest();

However, since your interest rate is never modified within the class, you might consider removing the public qualifier on the setter.

0

ok,thanks have one more question here is my Current acc:

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

namespace CollectionAccounts
{
    public class CurrentAccount : Account,IDisplayable
    {
        private double creditlimit;
        public CurrentAccount(string id)
            : base(id)
        { creditlimit = 0; }

        public double Creditlimit
        {
            get { return creditlimit; }
            set { creditlimit = value; }
        }

        public  void Display()
        {
            Console.WriteLine("Id:  {0} balance:  {1}  creditlimit:  {2}", id, balance,creditlimit);

        }
        public override void Withdraw(double amount)
        {

            if (amount <= (balance + creditlimit))
            {
                balance -= amount;

            }
            else
                Console.WriteLine("Insufficient funds\n");


        }



    }
}

And here Deposit:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace CollectionAccounts
{
    public class DepositAccount : Account,IDisplayable
    {
private double interestRate;
        public DepositAccount(string id)
            : base(id)
        {
            interestRate = 3.5;
        }

        public double InterestRate
        {
            get;private set;
        }
        public void AddInterest()
        {
            balance *= 1 + interestRate / 100;
        }

        public override void Withdraw(double amount)
        {
            if (amount <= (balance))
            {
                balance -= amount;


            }
            else
                Console.WriteLine("Insufficient funds\n");
        }
       public void Display()
        {
            Console.WriteLine("Id: {0}  balance:{1}   ", id, balance);

        }
    }
}

Here main: and the problem is the program crashes after additing last 4 lines

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

namespace CollectionAccounts
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Simple array");
        Account[] obj = new Account[2];

        obj[0] = new CurrentAccount("ca001");
        obj[1] = new DepositAccount("da001");


        obj[0].Lodge(200);
        ((CurrentAccount)obj[0]).Creditlimit = 500;
        obj[0].Withdraw(500);
        ((CurrentAccount)obj[0]).Display();


          obj[1].Lodge(200);
          ((DepositAccount)obj[1]).AddInterest() ;
          obj[1].Withdraw(500);
          ((DepositAccount)obj[1]).Display();

          Console.WriteLine("ArrayList");
          ArrayList myList = new ArrayList();
          CurrentAccount acc = new CurrentAccount("c002");
          myList.Add(acc);
          myList.Add(new CurrentAccount("c002"));

          ((CurrentAccount)myList[0]).Lodge(400);
          ((CurrentAccount)myList[0]).Creditlimit =500;
          ((CurrentAccount)myList[0]).Withdraw(450);
          ((CurrentAccount)myList[0]).Display();

          ((DepositAccount)myList[1]).Lodge(400);
          ((DepositAccount)myList[1]).AddInterest();
          ((DepositAccount)myList[1]).Withdraw(500);
          ((DepositAccount)myList[1]).Display();



        }
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.