2.Sinf yaratish 2-qism.
Qoidaga ko'ra, bank hisobvaraqlari o'z-o'zidan mavjud emas, balki hisobvaraqlar konteyneri vazifasini bajaradigan va ularni boshqarish funktsiyalarini bajaradigan bank ichida mavjud. Shunday qilib, keling , BankLibrary loyihasiga yangi bank sinfini qo'shamiz :
// hisob turi
public enum AccountType
{
Ordinary,
Deposit
}
public class Bank where T : Account
{
T[] accounts;
public string Name { get; private set; }
public Bank(string name)
{
this.Name = name;
}
// hisob yaratish usuli
public void Open(AccountType accountType, decimal sum,
AccountStateHandler addSumHandler, AccountStateHandler withdrawSumHandler,
AccountStateHandler calculationHandler, AccountStateHandler closeAccountHandler,
AccountStateHandler openAccountHandler)
{
T newAccount = null;
switch (accountType)
{
case AccountType.Ordinary:
newAccount = new DemandAccount(sum, 1) as T;
break;
case AccountType.Deposit:
newAccount = new DepositAccount(sum, 40) as T;
break;
}
if (newAccount == null)
throw new Exception("hisobni yaratishda xatolik");
// hisoblar qatoriga yangi sxema qo’shing
if (accounts == null)
accounts = new T[] { newAccount };
else
{
T[] tempAccounts = new T[accounts.Length + 1];
for (int i = 0; i < accounts.Length; i++)
tempAccounts[i] = accounts[i];
tempAccounts[tempAccounts.Length - 1] = newAccount;
accounts = tempAccounts;
}
// hisob voqealari ishlov beruvchilarini sozlang
newAccount.Added += addSumHandler;
newAccount.Withdrawed += withdrawSumHandler;
newAccount.Closed += closeAccountHandler;
newAccount.Opened += openAccountHandler;
newAccount.Calculated += calculationHandler;
newAccount.Open();
}
//o’rtacha hisobni yaratish
public void Put(decimal sum, int id)
{
T account = FindAccount(id);
if (account == null)
throw new Exception("hisob topilmadi");
account.Put(sum);
}
// pul mablag’ini olib qo’yish
public void Withdraw(decimal sum, int id)
{
T account = FindAccount(id);
if (account == null)
throw new Exception("Счет не найден");
account.Withdraw(sum);
}
// hisobni yopish
public void Close(int id)
{
int index;
T account = FindAccount(id, out index);
if (account == null)
throw new Exception("hisob topilmadi");
account.Close();
if (accounts.Length <= 1)
accounts = null;
else
{
// undanyopiq hisobni olib tashlash
T[] tempAccounts = new T[accounts.Length - 1];
for (int i = 0, j=0; i < accounts.Length; i++)
{
if (i != index)
tempAccounts[j++] = accounts[i];
}
accounts = tempAccounts;
}
}
// hisoblar bo’yicha foizlarni hisoblash
public void CalculatePercentage()
{
if (accounts == null) // massiv yaratilmagan bo’lsa usuldan chiqing
return;
for (int i = 0; i < accounts.Length; i++)
{
accounts[i].IncrementDays();
accounts[i].Calculate();
}
Do'stlaringiz bilan baham: |