-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDZ34.cs
151 lines (129 loc) · 4.01 KB
/
DDZ34.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp29
{
public abstract class Account
{
public string AccountNumber { get; set; }
public string HolderName { get; set; }
public double Balance { get; set; }
public abstract void Withdraw(double amount);
public abstract void Deposit(double amount);
}
public class SavingsAccount : Account
{
public double InterestRate { get; set; }
public override void Withdraw(double amount)
{
if (Balance >= amount)
{
Balance -= amount;
}
else
{
Console.WriteLine("Недостатньо коштів");
}
}
public override void Deposit(double amount)
{
Balance += amount;
}
public double CalculateInterest()
{
return Balance * InterestRate;
}
}
public class CheckingAccount : Account
{
public double OverdraftLimit { get; set; }
public override void Withdraw(double amount)
{
if (Balance + OverdraftLimit >= amount)
{
Balance -= amount;
}
else
{
Console.WriteLine("Перевищено ліміт переписки");
}
}
public override void Deposit(double amount)
{
Balance += amount;
}
}
public class Bank
{
private List<Account> accounts = new List<Account>();
public void AddAccount(Account account)
{
accounts.Add(account);
}
public void PrintAllAccounts()
{
foreach (var account in accounts)
{
Console.WriteLine($"Номер рахунку: {account.AccountNumber}, Власник: {account.HolderName}, Залишок: {account.Balance}");
}
}
public Account FindAccount(string accountNumber)
{
return accounts.Find(account => account.AccountNumber == accountNumber);
}
public void Withdraw(string accountNumber, double amount)
{
var account = FindAccount(accountNumber);
if (account != null)
{
account.Withdraw(amount);
}
else
{
Console.WriteLine("Рахунок не знайдено");
}
}
public void Deposit(string accountNumber, double amount)
{
var account = FindAccount(accountNumber);
if (account != null)
{
account.Deposit(amount);
}
else
{
Console.WriteLine("Рахунок не знайдено");
}
}
}
class Program
{
static void Main(string[] args)
{
var savingsAccount = new SavingsAccount
{
AccountNumber = "SA123",
HolderName = "Іван Петров",
Balance = 1000,
InterestRate = 0.05
};
var checkingAccount = new CheckingAccount
{
AccountNumber = "CA456",
HolderName = "Ольга Іванова",
Balance = 2000,
OverdraftLimit = 500
};
var bank = new Bank();
bank.AddAccount(savingsAccount);
bank.AddAccount(checkingAccount);
bank.PrintAllAccounts();
bank.Withdraw("SA123", 200);
bank.Deposit("CA456", 300);
bank.PrintAllAccounts();
Console.WriteLine($"Відсотки за рахунком: {savingsAccount.CalculateInterest()}");
}
}
}