0

Ok so this is multiple questions rolled into one. I honestly need a tutor or someone to talk to in person to clear up some of these concepts, but I'm taking this c# class online and the professor refuses to communicate with me so my next step is to come here. I know this is a lot of material, but any guidance would be greatly appreciated.

First of all the purpose of this assignment is to utilize inheritance in classes. Essentially you are supposed to created a base class called Account that initializes all your variables and set up get and sets for each one (which I'm not completely how to utilize these) as well as setting up Credit and Debit methods. Finally create a PrintAccount method that is going to be modified in the derived classes.

Next, create a SavingsAccount class that inherits everything from Account (I think I did this correctly by using ": Account" when creating the class). Within it you are supposed to modify the PrintAccount method using override to have it provide additional information when it's called. After that, create a CheckingAccount class that inherits from Account as well. Now you have to modify Debit and Credit as well as PrintAccount. The directions say to modify Debit/Credit by "invoking the Account class and use a boolean value to see if money was withdrawn." No idea what that means. Also you're supposed to override PrintAccount again to make it display information specific to the class.

After all of that, make a test class to test everything that's been put into place. My initial problem is making sure all of my classes inherit correctly and I'm not doing anything unnecessary. Next when I override the PrintAccount classes I'm clearly doing something wrong. Finally, invoking the Account class to boolean test the Debit/Credit methods.

namespace Assignment5
{
    class Account
    {
        //variables
        private double balance;
        private string accountName;
        private int accountNumber;


        public Account(string acctName, int acctNum, double acctBal)
        { 
            //what is the purpose of doing this?
            accountName = acctName;
            accountNumber = acctNum;
            balance = acctBal;
        }
        //gets and sets
        public double Balance
        {
            get { return balance; }
            set
            {
                if (value >= 0)
                    balance = value;
                else
                    balance = 0;
            }
        }
        public string AccountName
        {
            get { return accountName; }
        }
        public int AccountNumber
        {
            get { return accountNumber; }
        }

        //credit, debit and print methods
        public void Credit(double a)
        {
            balance += a;
        }
        public void Debit(double a)
        {
            if (a > balance)
                Console.WriteLine("Insufficient Funds.");
            else
                balance -= a;
        }
        public void PrintAccount()
        {
            Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}",
                accountName, accountNumber, balance);
        }

        class SavingsAccount : Account //this is how the derived class inherits from the base class, right?
        {

            public SavingsAccount(string acctName, int acctNum, double acctBal, double interest)
                : base(acctName, acctNum, acctBal)
            {
                accountName = acctName;
                accountNumber = acctNum;
                balance = acctBal;
                interestRate = interest;
            }
            private double interestRate;
            public double InterestRate
            {
                get { return interestRate; }
                set
                {
                    if (value < 0)
                        interestRate = 0;
                    else
                        interestRate = value;
                }
            }
            public void CalculateInterest()
            {
                balance = balance * interestRate;
                Console.WriteLine("Account Name:\t{0}\nAccount Number:\t{1}\nBalance:\t{2:C}\nInterest Rate:\t{3:P2}",
                    accountName, accountNumber, balance, interestRate);
            }
            public override string PrintAccount()
            {
                return string.Format("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}\nInterest Rate :\t{3:P2}",
                    accountName, accountNumber, balance, interestRate);
            }
        }
        class CheckingAccount : Account
        {
            public CheckingAccount(string acctName, int acctNum, double acctBal, double fee)
                : base(acctName, acctNum, acctBal)
            {
                accountName = acctName;
                accountNumber = acctNum;
                balance = acctBal;
                feeCharged = fee;
            }
            private double feeCharged;
            public double FeeAmmount
            {
                set
                {
                    if (value < 0)
                        feeCharged = 0;
                    else
                        feeCharged = value;
                }
            }
            //No idea how to "invoke the Account class and use a boolean value to see if
            //money was withdrawn."

            //public void Credit(double a)
            //{
            //    balance += a;
            //    balance -= feeCharged;
            //}
            //public void Debit(double a)
            //{
            //    if (a > balance)
            //        Console.WriteLine("Insufficient Funds.");
            //    else
            //        balance -= a;
            //        balance -= feeCharged;
            //} 

            public override string PrintAccount()
            {
                return string.Format("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}\nFee Charged :\t{3:C}",
                    accountName, accountNumber, balance, feeCharged);
            }
        }
        class AccountTest
        {
            static void Main(string[] args)
            {
                //Step 1 & 2
                    CheckingAccount lemmonsChecking = new CheckingAccount("Lemmons-Checking", 1, 1000, 3);
                    SavingsAccount lemmonsSavings = new SavingsAccount("Lemmons-Savings", 2, 2000, .05);
                //Step 3 & 4
                    lemmonsChecking.PrintAccount();
                    lemmonsSavings.PrintAccount();
                //Step 5 & 6
                Console.WriteLine("\nDeposit $100 into checking.");
                    lemmonsChecking.Credit(100);
                    lemmonsChecking.PrintAccount();
                //Step 7 & 8
                    Console.WriteLine("\nWithdraw $50 from checking.");
                    lemmonsChecking.Debit(50);
                    lemmonsChecking.PrintAccount();
                //Step 9 & 10
                    Console.WriteLine("\nWithdraw $6000 from checking.");
                    lemmonsChecking.Debit(6000);
                    lemmonsChecking.PrintAccount();
                //Step 11 & 12
                    Console.WriteLine("\nDeposit $3000 into savings.");
                    lemmonsSavings.Credit(3000);
                    lemmonsSavings.PrintAccount();
                //Step 13 & 14
                    Console.WriteLine("\nWithdraw $200 from savings.");
                    lemmonsSavings.Debit(200);
                    lemmonsSavings.PrintAccount();
                //Step 15 & 16
                    Console.WriteLine("\nCalculate interest on savings.");
                    lemmonsSavings.CalculateInterest();
                //Step 17 & 18
                    Console.WriteLine("\nWithdraw $10,000 from savings.");
                    lemmonsSavings.Debit(10000);
                    lemmonsSavings.PrintAccount();
                    Console.WriteLine("\nPress the [ENTER] key to continue.");
                    Console.ReadLine();
                //I keep receiving errors based around the override I placed on the PrintAccount()


            }
        }
    }
}
rae1
  • 6,066
  • 4
  • 27
  • 48
Evan Lemmons
  • 807
  • 3
  • 11
  • 27
  • Is there a specific question in here? – System Down Nov 27 '13 at 20:14
  • @System Down i do not believe there is since the title says various questions. – deathismyfriend Nov 27 '13 at 20:15
  • The oracle java tutorials aren't bad for learning concepts like this: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html – dckuehn Nov 27 '13 at 20:28
  • Yeah I've already sent a letter to both the department head and the dean. The teacher didn't respond to three emails then finally responded back saying he would give me a call to clear things up then he never called. I'm incredibly frustrated and the book we are using is pretty un-helpful because our assignments are asking to do some things the book skips over. This site really is my last hope before I just go to fiverr.com or something and pay to have someone tell me what I did wrong. – Evan Lemmons Nov 27 '13 at 22:32
  • I was in the middle of a conversation with a guy who was helping me, but his post got deleted? Is there any way to find out who that was so I can get back in touch with him? – Evan Lemmons Nov 30 '13 at 16:26

2 Answers2

1

Your PrintAccount method should be marked as virtual in your base Account class...this tells the compiler it's allowed to be overridden in derived classes.

public virtual void PrintAccount()
{
    Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1}\nBalance :\t{2:C}", accountName, accountNumber, balance);
}
Troy Carlson
  • 2,965
  • 19
  • 26
0

By invoking the Account class, I'm assuming it means, to invoke the implementation for that method in the base class, which you can do by,

public class CheckingAccount : Account
{
    // And you can use this to specify whether something 
    // was withdrawn from the account
    private bool moneyWithdrawn;

    public bool MoneyWithdrawn { get { return this.moneyWithdrawn; } }

    public void Credit(double a)
    {
        ... 
        base.Credit(a);
    }

    public void Debit(double a)
    {
        ...
        base.Debit(a);
    }
}

Other than that, the inheritance and override syntax look correct. You might have to get creative with that bool. Hope that helps!

rae1
  • 6,066
  • 4
  • 27
  • 48