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()
}
}
}
}