Witam,
Mam mały problem, dumam w jaki najlepszy sposób go rozwiązać..
Mam trzy klasy:
abstract public class BankAccount
{
protected string accountNumber;
protected double amount;
protected string currency;
protected string pesel;
public string AccountNumber { get { return accountNumber; } }
public string Currency { get { return currency; } set { currency = value; } }
public string Pesel { get { return pesel; } }
public BankAccount(string pesel)
{
amount = 0;
currency = "PLN";
accountNumber = RandomAccountNumber();
this.pesel = pesel;
}
public class PersonalAccount: BankAccount
{
public PersonalAccount(string pesel):base(pesel)
{
amount += 5;
}
}
oraz
class CurrencyAccount : PersonalAccount
{
public CurrencyAccount(string pesel, string currency) : base(pesel)
{
if (currency != "EUR" && currency != "USD" && currency != "GBP")
Console.WriteLine("Nie można utworzyć konta walutowego w innej walucie niż \"EUR\", \"USD\" lub \"GBP\".");
else
this.currency = currency;
}
}
Dążę do tego, aby konto walutowe można było utworzyć jedynie w walutach EUR, USD, GBP.
Niestety w moim rozwiązaniu nawet po podaniu innej waluty i wyświetleniu komunikatu wykorzystywany jest domyślny [edit: nadrzędny, klasy BankAccount] konstruktor i tworzone jest nowe konto w walucie "PL".
Jak zgodnie ze sztuką rozwiązać ten problem? Chciałbym, aby możliwe było utworzenie konta walutowego w w/w walutach.
class Program
{
static void Main(string[] args)
{
CurrencyAccount cu = new CurrencyAccount("89090982732", "sds");
Console.WriteLine(cu.Currency);
Console.WriteLine(cu.Pesel);
Console.ReadKey();
// wynikiem jest obiekt z cu.Currency = "PL"
}
}
Myślałem nad dorzuceniem enuma do klasy CurrencyAccount zawierającego możliwe waluty, jednak to nic nie zmieni - jeżeli warunek nie będzie spełniony dalej będzie tworzony obiekt z właściwością Currency ustawioną jako "PL".
Zdrowia w dzisiejszych czasach!