-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
240 lines (210 loc) · 9.99 KB
/
Program.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Banks.Accounts;
using Banks.Banks;
using Banks.Banks.Limits;
using Banks.Clients;
using Banks.UI;
using Spectre.Console;
namespace Banks
{
public static class Program
{
public static void Main(string[] args)
{
List<string> FindInheritedClasses(Type mainType)
{
if (mainType.IsInterface)
{
return Assembly.GetAssembly(mainType)?
.GetTypes()
.Where(type => mainType.IsAssignableFrom(type) && mainType != type)
.Select(type => type.Name)
.ToList();
}
if (mainType.IsAbstract)
{
return Assembly.GetAssembly(mainType)?
.GetTypes()
.Where(type => type.IsSubclassOf(mainType))
.Select(type => type.Name)
.ToList();
}
return null;
}
List<string> bankTypes = FindInheritedClasses(typeof(IBank));
List<string> accountTypes = FindInheritedClasses(typeof(Account));
List<string> clientTypes = FindInheritedClasses(typeof(IClient));
List<string> limitTypes = FindInheritedClasses(typeof(Limit));
while (true)
{
Console.Clear();
string option = AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title("Basic options: ")
.AddChoices(new List<string>
{
"CreateBank", "BankOptions",
"CentralBank", "Exit",
}));
if (option == "Exit" && !AnsiConsole.Confirm("Continue?"))
{
break;
}
switch (option)
{
case "CreateBank":
{
Creations.BankCreation(bankTypes, limitTypes);
break;
}
case "BankOptions":
{
IBank pickedBank = Selections.BankSelection();
if (pickedBank == null)
{
AnsiConsole.Prompt(
new TextPrompt<string>("[red]No banks[/]").AllowEmpty());
break;
}
string bankMethodName = AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title("What operation you want to do?")
.AddChoices(new List<string>()
{
"AddClient", "RegisterAccount",
"TopUp", "WithDraw", "Transfer",
"ChangeLimit", "Calculate",
"EnterClientData", "Exit",
}));
if (bankMethodName == "Exit") break;
if (bankMethodName == "ChangeLimit")
{
Limit newLimit = Creations.LimitCreation(limitTypes);
pickedBank.UpdateLimit(newLimit);
break;
}
if (bankMethodName == "AddClient")
{
pickedBank.AddClient(Creations.ClientCreation(pickedBank, clientTypes));
break;
}
IClient pickedClient = Selections.ClientSelection(pickedBank.Clients);
if (pickedClient == null)
{
AnsiConsole.Prompt(
new TextPrompt<string>("[red]No clients[/]").AllowEmpty());
break;
}
switch (bankMethodName)
{
case "RegisterAccount":
pickedBank.RegisterAccount(pickedClient, Creations.AccountCreation(accountTypes));
break;
case "EnterClientData":
{
if (pickedClient.Passport == null || pickedClient.Address == null)
{
if (pickedClient.Passport == null)
{
string passport =
AnsiConsole.Prompt(new TextPrompt<string>("Passport: ").AllowEmpty());
if (passport != string.Empty)
{
pickedClient.WithPassport(passport);
}
}
if (pickedClient.Address == null)
{
string address =
AnsiConsole.Prompt(new TextPrompt<string>("Address: ").AllowEmpty());
if (address != string.Empty)
{
pickedClient.WithAddress(address);
}
}
}
else
{
AnsiConsole.Prompt(
new TextPrompt<string>("[red]Already full data[/]").AllowEmpty());
}
break;
}
default:
{
Account pickedAccount =
Selections.AccountSelection(pickedBank.Accounts[pickedClient.Id]);
if (pickedAccount == null)
{
AnsiConsole.Prompt(
new TextPrompt<string>("[red]No accounts[/]").AllowEmpty());
break;
}
if (bankMethodName == "Calculate")
{
int days = AnsiConsole.Ask<int>("How many days ahead do we count?");
DateTime inTime = pickedAccount.Date.AddDays(days);
Account accountBe = pickedBank.Calculate(pickedClient, pickedAccount, inTime);
AnsiConsole.Prompt(
new TextPrompt<string>(
$"Balance {pickedAccount.Balance} -> {accountBe.Balance}").AllowEmpty());
break;
}
double amount = AnsiConsole.Ask<double>($"Balance to {bankMethodName}: ");
switch (bankMethodName)
{
case "TopUp":
pickedBank.TopUp(pickedClient, pickedAccount, amount);
break;
case "WithDraw":
pickedBank.WithDraw(pickedClient, pickedAccount, amount);
break;
case "Transfer":
IClient toClient =
Selections.ClientSelection(CentralBank.GetAllClients());
Account pickedAccount2 =
Selections.AccountSelection(
CentralBank.GetAllAccounts(toClient));
if (pickedAccount2 == null)
{
AnsiConsole.Prompt(
new TextPrompt<string>("[red]No accounts[/]").AllowEmpty());
break;
}
pickedBank.Transfer(pickedClient, pickedAccount, pickedAccount2, amount);
break;
}
break;
}
}
break;
}
case "CentralBank":
{
string viewType = AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title("What operation you want to do?")
.AddChoices(new List<string>()
{
"BankTree", "LimitTree",
"Exit",
}));
if (viewType == "Exit") break;
switch (viewType)
{
case "BankTree":
Messages.BankTree();
break;
case "LimitTree":
Messages.LimitTree();
break;
}
AnsiConsole.Prompt(new TextPrompt<string>("Continue...")
.AllowEmpty());
break;
}
}
}
}
}
}