So my last post got shut down for being too broad, so I've fixed some things and narrowed this down. Now when I try to run this, I get errors on line 136 which is the PrintAccount override in the CheckingAccount class and on line 148 which is the Main class in AccountTest. The error that pops up says: "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format." and I have no idea how to fix it. Any guidance would be appreciated. Thanks.
namespace Assignment5
{
class Account
{
//variables
private double balance;
private string accountName;
private int accountNumber;
public Account(string acctName, int acctNum, double acctBal)
{
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 virtual 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) + balance;
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 void PrintAccount()
{
Console.WriteLine("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 void PrintAccount()
{
Console.WriteLine("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()
}
}
}
}
You have a \t{1)
instead of a \t{1}
public override void PrintAccount()
{
Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}\nFee Charged :\t{3:C}", accountName, accountNumber, balance, feeCharged);
}
should be
public override void PrintAccount()
{
Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1}\nBalance :\t{2:C}\nFee Charged :\t{3:C}", accountName, accountNumber, balance, feeCharged);
}