-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.cs
273 lines (234 loc) · 10.1 KB
/
Database.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace BankSystem
{
public class Database
{
static private string accountsFileDirectory { get; set; }
static private string operatorsFilePath { get; set; }
static private string transactionIDFile { get; set; } = "globals.txt";
static private Dictionary<string, int> operatorKeyMap = new Dictionary<string, int>()
{
{ "username", 0 },
{ "password", 1 },
{ "accountNumber", 2 },
{ "accessFlag", 3},
{ "isEmployee", 4 }
};
static private Dictionary<string, int> transactionKeyMap = new Dictionary<string, int>()
{
{ "transactionID", 0 },
{ "transactionType", 1},
{ "transactionDate", 2 },
{ "transactionAmount", 3 },
{ "senderAccount", 4 },
{ "receiverAccount", 5 }
};
static public void SetFilesPath(string accountsFileDirectory = null, string operatorsFilePath = null)
{
Database.accountsFileDirectory = accountsFileDirectory;
Database.operatorsFilePath = operatorsFilePath;
}
// Retrieve data for an account from the data storage and
// parse them into an Account object
static public Account GetAccount(string accountNumber) // Get an Account object from the files
{
string[] dirs = Directory.GetDirectories(Database.accountsFileDirectory);
if (accountNumber.Length != 10)
{
return null;
}
foreach (var dir in dirs)
{
if (dir.EndsWith(accountNumber.Substring(0, 9)))
{
string[] entry = File.ReadAllText(Path.Combine(dir, $"{accountNumber}.txt")).Split(',');
if (accountNumber.EndsWith("1"))
{
return new SavingAccount(entry[0], accountNumber, entry[1]);
}
else if(accountNumber.EndsWith("0"))
{
return new ChequingAccount(entry[0], accountNumber, entry[1]);
}
}
}
return null; // No accounts found
}
// Retrieve data for an operator from the data storage and
// parse them into an Operator object
static public Operator GetOperator(string key, string value)
{
bool isValidKey = Database.operatorKeyMap.ContainsKey(key);
if (!isValidKey) return null;
string[] lines = File.ReadAllLines(Database.operatorsFilePath);
foreach (var line in lines)
{
List<string> entry = new List<string>(line.Split(','));
if (entry[Database.operatorKeyMap[key]] == value)
{
if (entry.Last() == "1")
{
// Return Employee
return new Employee(entry[0], entry[1], entry[2]);
}
// Return Client
return new Client(entry[0], entry[1], entry[2], entry[3] == "1" ? true : false);
}
}
return null; // No operators found
}
// Write the content of an Account object into the data storage
static public void SaveAccount(Account accountToBeSaved, bool saveNew = false)
{
string accountNumber = accountToBeSaved.GetNumber();
if (saveNew)
{
Directory.CreateDirectory(Path.Combine(Database.accountsFileDirectory, accountNumber.Substring(0, 9)));
File.WriteAllText(Path.Combine(Database.accountsFileDirectory, accountNumber.Substring(0, 9), $"{accountNumber}.txt"), accountToBeSaved.ToQueryString());
return;
}
string[] dirs = Directory.GetDirectories(Database.accountsFileDirectory);
foreach (var dir in dirs)
{
if (dir.EndsWith(accountNumber.Substring(0, 9)))
{
string filepath = Path.Combine(dir, $"{accountNumber}.txt");
File.WriteAllText(filepath, accountToBeSaved.ToQueryString());
return;
}
}
throw new FileNotFoundException();
}
// Write the contents of an Operator object into the data storage
static public void SaveOperator(Operator operatorToBeSaved, bool saveNew = false)
{
if (saveNew)
{
File.AppendAllText(Database.operatorsFilePath, operatorToBeSaved.ToQueryString() + '\n');
return;
}
if (operatorToBeSaved == null) return;
string[] lines = File.ReadAllLines(Database.operatorsFilePath);
for (var index = 0; index < lines.Length; index++)
{
List<string> entry = new List<string>(lines[index].Split(','));
if (entry[Database.operatorKeyMap["accountNumber"]] == operatorToBeSaved.GetNumber())
{
string modifiedEntry = operatorToBeSaved.ToQueryString();
}
}
return;
}
// Write the content of a Transaction object into the data storage
static public void SaveTransaction(Transaction transactionToBeSaved)
{
List<string> accountsToBeWrittenTo = new List<string>();
accountsToBeWrittenTo.Add(transactionToBeSaved.GetSenderNumber());
string receiverAccNumber = transactionToBeSaved.GetReceiverNumber();
if (receiverAccNumber != "N/A")
{ accountsToBeWrittenTo.Add(receiverAccNumber); }
try
{
for (int i = 0; i < accountsToBeWrittenTo.Count; i++)
{
string accountNumber = accountsToBeWrittenTo[i];
File.AppendAllText(Path.Combine(Database.accountsFileDirectory, accountNumber.Substring(0, 9), $"{accountNumber}_transaction.txt"), transactionToBeSaved.ToQueryString());
}
int incrementedTransactionID = int.Parse(File.ReadAllText(Path.Combine(Database.accountsFileDirectory, Database.transactionIDFile))) + 1;
File.WriteAllText(Path.Combine(Database.accountsFileDirectory, Database.transactionIDFile), incrementedTransactionID.ToString());
return;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
// Returns the current available transaction ID
static public string GetTransactionID()
{
return File.ReadAllText(Path.Combine(Database.accountsFileDirectory, Database.transactionIDFile));
}
static public Transaction[] GetTransactions(string accountNumber, string key = null, string value1 = null, string value2 = null)
{
List<Transaction> result = new List<Transaction>();
string filepath = Path.Combine(
Database.accountsFileDirectory,
$"{accountNumber.Substring(0, 9)}",
$"{accountNumber}_transaction.txt"
);
if (!File.Exists(filepath)) { return result.ToArray(); }
string[] lines = File.ReadAllLines(filepath);
// Returns all transactions made from the account
if (key == null)
{
foreach (var line in lines)
{
string[] entry = line.Split(',');
Transaction newTransaction = new Transaction(
entry[0],
entry[1],
DateTime.Parse(entry[2]),
float.Parse(entry[3]),
entry[4],
entry[5]
);
result.Add(newTransaction);
}
return result.ToArray();
}
// Returns an empty array of transactions if the key does not exist
if (!transactionKeyMap.ContainsKey(key)) { return result.ToArray(); }
// Returns transactions with a specified condition
if (key == "transactionDate")
{
foreach (var line in lines)
{
string[] entry = line.Split(',');
if (DateTime.Parse(value1) < DateTime.Parse(entry[Database.transactionKeyMap["transactionDate"]])
&&
DateTime.Parse(entry[Database.transactionKeyMap["transactionDate"]]) < DateTime.Parse(value2))
{
Transaction newTransaction = new Transaction(
entry[0],
entry[1],
DateTime.Parse(entry[2]),
float.Parse(entry[3]),
entry[4],
entry[5]
);
result.Add(newTransaction);
}
}
}
else
{
foreach (var line in lines)
{
string[] entry = line.Split(',');
if (entry[Database.transactionKeyMap[key]] == value1)
{
Transaction newTransaction = new Transaction(
entry[0],
entry[1],
DateTime.Parse(entry[2]),
float.Parse(entry[3]),
entry[4],
entry[5]
);
result.Add(newTransaction);
}
}
}
return result.ToArray();
}
}
}