Reputation: 8380
I'm having some trouble in allowing a Staff or a Person to "own" an account as per details below.
In the snippet below, on the Account class, I only accept Person
as the owner. I kind of need to accept either a Staff
or a Person
My main issue is that, later in the method applyFee()
, I need to reach out the the owner object and if the owner has a feeDiscount property, I will need to use to calculate.
My issue is that since in the Account class the type is Person owner
I am not getting the feeDiscount
as it is null.
class Person
{
public string name;
public Person(string newName)
{
name = newName;
}
}
class Staff : Person
{
public decimal feeDiscount;
public override Staff(string newName)
{
name = newName;
feeDiscount = 0.5;
}
}
class Account
{
private decimal balance = 1000;
private Person owner;
public Account(Person newOwner)
{
owner = newOwner;
}
public void applyFee() {
decimal fee = 100;
if (owner != null)
{
if (owner.feeDiscount) {
balance = balance - (fee * owner.feeDiscount);
} else {
balance = balance - fee;
}
}
}
}
class Program
{
static void Main(string[] args)
{
Person person1 = new Person("Bob");
Staff staff1 = new Staff("Alice");
Account account1 = new Account(person1);
Account account2 = new Account(staff1);
account1.applyFee();
account2.applyFee();
}
}
Upvotes: 1
Views: 80
Reputation: 505
If you want Person
to remain as generic as possible, then you could make another class called customer
who has a feeDiscount
of 0.
So anyone who has any business spending money at a store, has some feeDiscount
. This way, you can applyFee
to a Customer
or a Staff
but not a Person
Upvotes: 6