-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
255 lines (205 loc) · 8.19 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
User? user = null;
Console.WriteLine("Welcome to the Personal Finance Management System! \n------first Register than Login------");
while (true) {
Console.WriteLine("\nSelect an option:");
Console.WriteLine("1. Register\n2. Login\n0. Exit");
int initialChoice = GetIntInput("Enter your choice (0-2): ", 0, 2);
switch (initialChoice) {
case 1:
user = Register();
break;
case 2:
while (Login(user)) {
Console.WriteLine("Invalid email or password. Please try again or register first.");
}
Console.WriteLine("Login !!!");
MainMenu(user);
break;
case 0:
Console.WriteLine("Exiting the Personal Finance Management System...");
return;
}
}
}
static void MainMenu(User user) {
while (true) {
if (user == null) {
Console.WriteLine("User not logged in. Please log in or register first.");
return;
}
Console.WriteLine("\nMain Menu:");
Console.WriteLine("1. Create Wallet\n2. Choose Wallet\n3. Add Income\n4. Add Expense\n5. Delete Wallet\n6. View Statistics\n7. Log Out\n0. Exit");
int mainChoice = GetIntInput("Enter your choice: ", 0, 7);
switch (mainChoice) {
case 1:
CreateWallet(user);
break;
case 2:
ChooseActiveWallet(user);
break;
case 3:
user.AddIncome();
break;
case 4:
user.AddExpense();
break;
case 5:
DeleteWallet(user);
break;
case 6:
ViewStatistics(user);
break;
case 7:
SystemService.RemoveActiveUser();
Console.WriteLine("Logged out successfully!");
return;
case 0:
Console.WriteLine("\n Exiting the Personal Finance Management System...");
return;
}
}
}
static User? Register() {
Console.WriteLine("Registration:");
string name = GetStringInput("Enter your name: ");
string email = GetStringInput("Enter your email: ");
string password = GetStringInput("Enter your password: ");
DateTime birthday = GetDateInput("Enter your birthday (yyyy-mm-dd): ");
Console.WriteLine("\n Register successfully! \n Now Login");
return new User(name, email, password, birthday);
}
static void CreateWallet(User user) {
Console.WriteLine("\nCreate Wallet:");
string name = GetStringInput("Enter the wallet name: ");
Console.WriteLine("Select Currency:");
Console.WriteLine("1. Dollar ($)");
Console.WriteLine("2. Euro (€)");
Console.WriteLine("3. Ruble (₽)");
int currencyChoice = GetIntInput("Enter your choice (1-3): ", 1, 3);
string currency;
switch (currencyChoice) {
case 1:
currency = "USD";
break;
case 2:
currency = "EUR";
break;
case 3:
currency = "RUB";
break;
default:
throw new ArgumentOutOfRangeException(nameof(currencyChoice), "Invalid currency choice.");
}
decimal initialBalance = GetDecimalInput("Enter the initial balance: ");
user.CreateWallet(name, currency, initialBalance);
Console.WriteLine($"Wallet '{name}' created successfully!");
ChooseActiveWallet(user);
}
static void ChooseActiveWallet(User user) {
Console.WriteLine("\n Choose Active Wallet:");
List<Wallet> userWallets = user.GetWallets();
if (userWallets.Count == 0) {
Console.WriteLine("\n No wallets available. Create a wallet first.");
return;
}
Console.WriteLine("Wallet List:");
for (int i = 0; i < userWallets.Count; i++) {
Console.WriteLine($"{i + 1}. {userWallets[i].Name} ({userWallets[i].Currency})");
}
int walletChoice = GetIntInput("Choose a wallet (0 to cancel): ", 0, userWallets.Count);
if (walletChoice > 0) {
user.SetActiveWallet(userWallets[walletChoice - 1]);
Console.WriteLine($"Wallet '{userWallets[walletChoice - 1].Name}' set as active.");
}
}
static void DeleteWallet(User user) {
Console.WriteLine("\nDelete Wallet:");
List<Wallet> userWallets = user.GetWallets();
if (userWallets.Count == 0) {
Console.WriteLine("\n No wallets available. Create a wallet first.");
return;
}
Console.WriteLine("Wallet List:");
for (int i = 0; i < userWallets.Count; i++) {
Console.WriteLine($"{i + 1}. {userWallets[i].Name} ({userWallets[i].Currency})");
}
int walletChoice = GetIntInput("Choose a wallet to delete (0 to cancel): ", 0, userWallets.Count);
if (walletChoice > 0) {
user.DeleteWallet(userWallets[walletChoice - 1]);
Console.WriteLine($"Wallet '{userWallets[walletChoice - 1].Name}' deleted successfully.");
}
}
static bool Login(User user) {
Console.WriteLine("Login:");
string enteredEmail = GetStringInput("Enter your email: ");
string enteredPassword = GetStringInput("Enter your password: ");
User authenticatedUser = AuthenticateUser(enteredEmail, enteredPassword);
if (authenticatedUser != null) {
SystemService.SaveActiveUser(authenticatedUser);
Console.WriteLine("Login successful!");
return true;
}
Console.WriteLine("\n :>");
return false;
}
static User? AuthenticateUser(string enteredEmail, string enteredPassword) {
foreach (User? registeredUser in RegisteredUsers) {
if (registeredUser.Email == enteredEmail && CheckPassword(enteredPassword, registeredUser.Password)) {
return registeredUser;
}
}
return null;
}
static List<User> RegisteredUsers = new List<User>();
static bool IsValidEmail(string email) {
try {
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
} catch {
return false;
}
}
static bool CheckPassword(string enteredPassword, string hashedPassword) {
return hashedPassword == GetHashedPassword(enteredPassword);
}
static int GetIntInput(string prompt, int minValue, int maxValue) {
int input;
do {
Console.Write(prompt);
} while (!int.TryParse(Console.ReadLine(), out input) || input < minValue || input > maxValue);
return input;
}
static string GetStringInput(string prompt) {
Console.Write(prompt);
return Console.ReadLine();
}
static string GetHashedPassword(string prompt) {
Console.Write(prompt);
return Console.ReadLine();
}
static DateTime GetDateInput(string prompt) {
DateTime input;
do {
Console.Write(prompt);
} while (!DateTime.TryParse(Console.ReadLine(), out input));
return input;
}
static decimal GetDecimalInput(string prompt) {
decimal input;
do {
Console.Write(prompt);
} while (!decimal.TryParse(Console.ReadLine(), out input));
return input;
}
static void ViewStatistics(User user) {
Console.WriteLine("View Statistics:");
DateTime startDate = GetDateInput("Enter the start date (yyyy-mm-dd): ");
DateTime endDate = GetDateInput("Enter the end date (yyyy-mm-dd): ");
SystemService.CalculateStatistics(user, startDate, endDate);
}
}