T
tshad
I have a project with 2 classes Account and SavingsAccount which has Account
as the base.
I have penalty as a static property in both classes.
Everything else is being done in the base class.
But I have a couple of things that I am unsure of.
I want the SavingsAccount class to act exactly as the Account class except
for the penalty.
But when I call the Withdraw method from either class (a1.Withdraw or
s1.Withdraw), it always uses the penalty from the Account class.
Do I need to override the Withdraw class in my Savings account class to get
it to use the penalty from the Savings account class?
Also, I was doing the following to figure out balances:
balance -= amt - penalty;
I thought this would equate to:
balance = balance - amt - penalty
But it is adding in the penalty so I assume it is equating to:
balance = balance - (amt - penalty)
which would reverse the minus sign before penalty - is this correct?
so I changed it to:
balance -= amt + penalty
And it works fine now.
The code is:
*************************************************
using System;
using System.Collections.Generic;
using System.Text;
namespace TestInheritance2
{
class Account
{
protected double balance;
protected double overDraftLimit;
protected string name;
private static double penalty;
public static double Penalty
{
get { return penalty; }
set { penalty = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public double Balance
{
get { return balance; }
}
public double OverDraftLimit
{
get { return overDraftLimit; }
set { overDraftLimit = value; }
}
static Account()
{
Console.WriteLine("Setting Accounts Penalty = 50");
penalty = 50;
}
public Account()
{
balance = 1.0;
overDraftLimit = 500.0;
}
public Account(double amt, string nm, double overDraftLimit)
{
balance = amt;
name = nm;
this.overDraftLimit = overDraftLimit;
}
public void Deposit(double amt)
{
if (amt < 0.0)
throw new ArgumentOutOfRangeException("Deposit: invalid
argument");
// deposit cash
balance += amt;
}
public void Withdraw(double amt)
{
if (amt < 0.00)
{
throw new ArgumentOutOfRangeException("Withdraw: invalid
withdrawal");
}
if (balance - amt + overDraftLimit < 0.0)
throw new ArgumentOutOfRangeException("Withdraw: not enough
funds");
else
{
Console.WriteLine("in Withdraw Penalty = {0}", penalty);
balance -= amt + penalty;
Console.WriteLine("After Withdraw balance = {0}", balance);
}
}
public void Report()
{
Console.WriteLine("Account Name: {0}", name);
Console.WriteLine("Balance: {0}", balance);
Console.WriteLine("Overdraft Limit: {0}", overDraftLimit);
Console.WriteLine("Penalty: {0} ", penalty);
}
}
class SavingsAccount : Account
{
static double penalty;
static SavingsAccount()
{
Console.WriteLine("Setting Saving Accounts Penalty = 10.5");
penalty = 10.5;
}
public SavingsAccount(double amt, string nm, double overDraftLimit)
{
balance = amt;
name = nm;
this.overDraftLimit = overDraftLimit;
}
public new void Report()
{
Console.WriteLine("Penalty in Savings Account: {0} ", penalty);
base.Report();
}
}
public class program
{
static void Main(string[] args)
{
Console.WriteLine("Account Penalty at start = {0}",
Account.Penalty);
Console.WriteLine("SavingsAccount Penalty at start = {0}",
SavingsAccount.Penalty);
Account a1 = new Account(1000.0, "Count Dracula", 500);
SavingsAccount s1 = new SavingsAccount(100.0, "Franky", 50);
s1.Name = "New Savings Account for Tom";
a1.Report();
Console.WriteLine();
s1.Report();
Console.WriteLine();
a1.Withdraw(10);
Console.WriteLine();
s1.Withdraw(10);
Console.WriteLine("After withdrawing 10 from Savings account and
regular Account");
a1.Report();
Console.WriteLine();
s1.Report();
Console.Read();
}
}
}
**************************************************************
And the result is:
****************************************
Setting Accounts Penalty = 50
Account Penalty at start = 50
SavingsAccount Penalty at start = 50
Setting Saving Accounts Penalty = 10.5
Account Name: Count Dracula
Balance: 1000
Overdraft Limit: 500
Penalty: 50
Penalty in Savings Account: 10.5
Account Name: New Savings Account for Tom
Balance: 100
Overdraft Limit: 50
Penalty: 50
in Withdraw Penalty = 50
After Withdraw balance = 940
in Withdraw Penalty = 50
After Withdraw balance = 40
After withdrawing 10 from Savings account and regular Account
Account Name: Count Dracula
Balance: 940
Overdraft Limit: 500
Penalty: 50
Penalty in Savings Account: 10.5
Account Name: New Savings Account for Tom
Balance: 40
Overdraft Limit: 50
Penalty: 50
****************************************
As you can see $50 is the penalty in both cases. 10.5 is never used.
Thanks,
Tom
as the base.
I have penalty as a static property in both classes.
Everything else is being done in the base class.
But I have a couple of things that I am unsure of.
I want the SavingsAccount class to act exactly as the Account class except
for the penalty.
But when I call the Withdraw method from either class (a1.Withdraw or
s1.Withdraw), it always uses the penalty from the Account class.
Do I need to override the Withdraw class in my Savings account class to get
it to use the penalty from the Savings account class?
Also, I was doing the following to figure out balances:
balance -= amt - penalty;
I thought this would equate to:
balance = balance - amt - penalty
But it is adding in the penalty so I assume it is equating to:
balance = balance - (amt - penalty)
which would reverse the minus sign before penalty - is this correct?
so I changed it to:
balance -= amt + penalty
And it works fine now.
The code is:
*************************************************
using System;
using System.Collections.Generic;
using System.Text;
namespace TestInheritance2
{
class Account
{
protected double balance;
protected double overDraftLimit;
protected string name;
private static double penalty;
public static double Penalty
{
get { return penalty; }
set { penalty = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public double Balance
{
get { return balance; }
}
public double OverDraftLimit
{
get { return overDraftLimit; }
set { overDraftLimit = value; }
}
static Account()
{
Console.WriteLine("Setting Accounts Penalty = 50");
penalty = 50;
}
public Account()
{
balance = 1.0;
overDraftLimit = 500.0;
}
public Account(double amt, string nm, double overDraftLimit)
{
balance = amt;
name = nm;
this.overDraftLimit = overDraftLimit;
}
public void Deposit(double amt)
{
if (amt < 0.0)
throw new ArgumentOutOfRangeException("Deposit: invalid
argument");
// deposit cash
balance += amt;
}
public void Withdraw(double amt)
{
if (amt < 0.00)
{
throw new ArgumentOutOfRangeException("Withdraw: invalid
withdrawal");
}
if (balance - amt + overDraftLimit < 0.0)
throw new ArgumentOutOfRangeException("Withdraw: not enough
funds");
else
{
Console.WriteLine("in Withdraw Penalty = {0}", penalty);
balance -= amt + penalty;
Console.WriteLine("After Withdraw balance = {0}", balance);
}
}
public void Report()
{
Console.WriteLine("Account Name: {0}", name);
Console.WriteLine("Balance: {0}", balance);
Console.WriteLine("Overdraft Limit: {0}", overDraftLimit);
Console.WriteLine("Penalty: {0} ", penalty);
}
}
class SavingsAccount : Account
{
static double penalty;
static SavingsAccount()
{
Console.WriteLine("Setting Saving Accounts Penalty = 10.5");
penalty = 10.5;
}
public SavingsAccount(double amt, string nm, double overDraftLimit)
{
balance = amt;
name = nm;
this.overDraftLimit = overDraftLimit;
}
public new void Report()
{
Console.WriteLine("Penalty in Savings Account: {0} ", penalty);
base.Report();
}
}
public class program
{
static void Main(string[] args)
{
Console.WriteLine("Account Penalty at start = {0}",
Account.Penalty);
Console.WriteLine("SavingsAccount Penalty at start = {0}",
SavingsAccount.Penalty);
Account a1 = new Account(1000.0, "Count Dracula", 500);
SavingsAccount s1 = new SavingsAccount(100.0, "Franky", 50);
s1.Name = "New Savings Account for Tom";
a1.Report();
Console.WriteLine();
s1.Report();
Console.WriteLine();
a1.Withdraw(10);
Console.WriteLine();
s1.Withdraw(10);
Console.WriteLine("After withdrawing 10 from Savings account and
regular Account");
a1.Report();
Console.WriteLine();
s1.Report();
Console.Read();
}
}
}
**************************************************************
And the result is:
****************************************
Setting Accounts Penalty = 50
Account Penalty at start = 50
SavingsAccount Penalty at start = 50
Setting Saving Accounts Penalty = 10.5
Account Name: Count Dracula
Balance: 1000
Overdraft Limit: 500
Penalty: 50
Penalty in Savings Account: 10.5
Account Name: New Savings Account for Tom
Balance: 100
Overdraft Limit: 50
Penalty: 50
in Withdraw Penalty = 50
After Withdraw balance = 940
in Withdraw Penalty = 50
After Withdraw balance = 40
After withdrawing 10 from Savings account and regular Account
Account Name: Count Dracula
Balance: 940
Overdraft Limit: 500
Penalty: 50
Penalty in Savings Account: 10.5
Account Name: New Savings Account for Tom
Balance: 40
Overdraft Limit: 50
Penalty: 50
****************************************
As you can see $50 is the penalty in both cases. 10.5 is never used.
Thanks,
Tom