-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank.cs
151 lines (138 loc) · 5 KB
/
Bank.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 Newtonsoft.Json;
namespace BankingApp
{
public class Bank
{
private static double id = 0;
/// <summary>
/// Dictionary to store all the bank accounts
/// </summary>
/// <typeparam name="string"></typeparam>
/// <typeparam name="BankAccount"></typeparam>
/// <returns></returns>
private static Dictionary<string, BankAccount> BankAccounts = new Dictionary<string, BankAccount>();
/// <summary>
/// List to store all the emails
/// </summary>
/// <typeparam name="string"></typeparam>
/// <returns></returns>
private static List<string> EMAILS = new List<string>();
/// <summary>
/// Validates the email address
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
private static bool validateAccount(string email)
{
if(EMAILS.Contains(email)) return false;
else EMAILS.Add(email);
return true;
}
/// <summary>
/// Signs in the user if the username and PIN are correct
/// </summary>
/// <param name="username"></param>
/// <param name="PIN"></param>
/// <returns></returns>
public static BankAccount SignIn(string username, string PIN)
{
if(BankAccounts.TryGetValue(username, out BankAccount Account))
{
if (Account.getPIN().Equals(PIN))
{
return Account;
}
}
return null;
}
/// <summary>
/// Creates a new account
/// </summary>
/// <param name="username"></param>
/// <param name="name"></param>
/// <param name="PIN"></param>
/// <param name="email"></param>
/// <param name="phone"></param>
/// <returns></returns>
public static string CreateAccount(string username, string name, string PIN, string email, string phone)
{
var response = new Object();
if(!validateAccount(email))
{
response = new {
status = false,
message = "Account Already Exists"
};
}
else
{
id = BankAccounts.LastOrDefault().Value.ID + 1;
BankAccount account = new BankAccount(id, username, name, PIN, email, phone);
BankAccounts.Add(username, account);
string accountDetails = account.ToString();
response = new {
status = true,
message = "Account Created Successfully",
accountDetails = accountDetails
};
}
return JsonConvert.SerializeObject(response);
}
/// <summary>
/// Saves the accounts to a file
/// </summary>
/// <returns></returns>
public static string saveAccounts()
{
var response = new Object();
try
{
string AccountsJson = JsonConvert.SerializeObject(BankAccounts, Formatting.Indented);
string EmailsJson = JsonConvert.SerializeObject(EMAILS, Formatting.Indented);
File.WriteAllText("BankAccounts.json", AccountsJson);
File.WriteAllText("Emails.json", EmailsJson);
response = new {
status = true,
message = "Accounts Saved Successfully"
};
return JsonConvert.SerializeObject(response);
}
catch (Exception e)
{
response = new {
status = false,
message = "Error Saving Accounts"
};
return JsonConvert.SerializeObject(response);
}
}
/// <summary>
/// Retrieves the accounts from the file
/// </summary>
/// <returns></returns>
public static string retrieveAccounts()
{
var response = new Object();
try
{
string AccountsJson = File.ReadAllText("BankAccounts.json");
string EmailsJson = File.ReadAllText("Emails.json");
BankAccounts = JsonConvert.DeserializeObject<Dictionary<string, BankAccount>>(AccountsJson);
EMAILS = JsonConvert.DeserializeObject<List<string>>(EmailsJson);
response = new {
status = true,
message = "Accounts Retrieved Successfully"
};
return JsonConvert.SerializeObject(response);
}
catch (Exception e)
{
response = new {
status = false,
message = "Error Retrieving Accounts"
};
return JsonConvert.SerializeObject(response);
}
}
}
}