Следующий класс ломает открытый / закрытый принципал:
public class Account
{
public decimal Interest { get; set; }
public decimal Balance { get; set; }
public decimal CalcInterest(string accType)
{
if (accType == "Regular")
{
Interest = (Balance * 4) / 100;
if (Balance < 1000) Interest -= (Balance * 2) / 100;
if (Balance < 50000) Interest += (Balance * 4) / 100;
}
else if (accType == "Salary") // salary savings
{
Interest = (Balance * 5) / 100;
}
else if (accType == "Corporate") // Corporate
{
Interest = (Balance * 3) / 100;
}
return Interest;
}
}
И это можно исправить следующим образом:
public interface IAccount
{
decimal Balance { get; set; }
decimal CalcInterest();
}
public class RegularAccount : IAccount
{
public decimal Balance { get; set; }
public decimal CalcInterest()
{
var interest = (Balance * 4) / 100;
if (Balance < 1000)
interest -= (Balance * 2) / 100;
if (Balance < 50000)
interest += (Balance * 4) / 100;
return interest;
}
}
public class SalaryAccount : IAccount
{
public decimal Balance { get; set; }
public decimal CalcInterest()
{
return (Balance * 5) / 100;
}
}
public class CorporateAccount : IAccount
{
public decimal Balance { get; set; }
public decimal CalcInterest()
{
return (Balance * 3) / 100;
}
}
Однако RegularAccount по-прежнему нарушает принципал из-за сальдо чеки. Как вы это исправите?
Является ли ответ просто создать больше классов, которые реализуют интерфейс IAccount? например, Regular1000Account: IAccount, Regular5000Account: IAccount, Regular8000Account: IAccount, et c