-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
217 lines (194 loc) · 7.74 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
using Newtonsoft.Json.Linq;
using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace SupportBank
{
class Program
{
public List<Transaction> transactions = new List<Transaction>();
static void Main(string[] args)
{
var config = new LoggingConfiguration();
var target = new FileTarget { FileName = @"C:\Work\Training\SupportBank.log", Layout = @"${longdate} ${level} Located at: ${callsite} - ${logger}: ${message}" };
config.AddTarget("File Logger", target);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, target));
LogManager.Configuration = config;
new Program().runProgram();
}
public void runProgram()
{
Console.WriteLine("Enter your file type: ");
string filePath = Console.ReadLine();
string extension = Path.GetExtension(filePath);
switch (extension)
{
case ".csv":
transactions = LoadCsv(filePath);
UserInput();
break;
case ".json":
transactions = JSONparse.LoadJSON(filePath);
UserInput();
break;
case ".xml":
transactions = XMLparse.LoadXML(filePath);
UserInput();
break;
default:
Console.WriteLine("Sorry " + filePath + " is not a valid file path");
runProgram();
break;
}
}
public List<Transaction> LoadCsv(string filePath)
{
var transactions = new List<Transaction>();
using (var reader = new StreamReader(@filePath))
{
var skipTheFirstRow = true;
while (!reader.EndOfStream)
{
var values = reader.ReadLine().Split(',');
if (skipTheFirstRow)
{
skipTheFirstRow = false;
continue; //continue breaks the loop and executes the while loop again
}
var currentRow = Transaction.CreateIfPossible(values[0], values[1], values[2], values[3], values[4]);
if (currentRow != null)
{
transactions.Add(currentRow);
}
}
}
return transactions;
}
public void UserInput()
{
Console.WriteLine("Search for a user or type 'List All' to list all transactions");
string nameInputByUser = Console.ReadLine().ToLower();
if (nameInputByUser == "list all")
{
var outPutOfDictionary = ListAll(transactions);
var exportMyDamnUsers = PrintResult(outPutOfDictionary);
ExportFile(exportMyDamnUsers);
}
else
{
var outputListAccount = ListAccount(transactions, nameInputByUser);
var exportMyDamnFile = PrintAccount(outputListAccount);
ExportFile(exportMyDamnFile);
}
Console.ReadLine();
}
public Dictionary<string, double> ListAll(List<Transaction> dataRow)
{
var myDictionary = new Dictionary<string, double>();
foreach (var data in dataRow)
{
//collates all the key values to one line (i.e all users called 'Sam' are collated and their money Owed is added up.
if (myDictionary.ContainsKey(data.FromAccount))
{
myDictionary[data.FromAccount] += data.Amount;
}
else
{
myDictionary.Add(data.FromAccount, data.Amount);
}
if (myDictionary.ContainsKey(data.ToAccount))
{
myDictionary[data.ToAccount] -= data.Amount;
}
else
{
myDictionary.Add(data.ToAccount, -data.Amount);
}
}
return myDictionary;
}
public List<Transaction> ListAccount(List<Transaction> allTransactions, string userName)
{
//uses LINQ which provides the where method to filter a list based on a condition passed to it - expressed using an anonymous function.
var filtered = allTransactions.Where(transaction => transaction.FromAccount == userName || transaction.ToAccount == userName).ToList();
if (!filtered.Any())
{
Console.WriteLine("Sorry " + userName + " is not recognised");
runProgram();
}
return filtered;
}
public List<Transaction> PrintResult(Dictionary<string, double> outPutOfDictionary)
{
var list = new List<Transaction>();
foreach (var key in outPutOfDictionary.Keys)
{
Console.WriteLine(key);
Console.WriteLine(outPutOfDictionary[key]);
}
return list;
}
public List<Transaction> PrintAccount(List<Transaction> transactionsForAccount)
{
var list = new List<Transaction>();
foreach (var transaction in transactionsForAccount)
{
Console.WriteLine(transaction.ToString());
list.Add(transaction);
}
return list;
}
public void ExportFile(List<Transaction> outputOfPrintAccount)
{
var sBuilder = new StringBuilder();
Console.WriteLine("What file type would you like to export to? (.csv/.json/.xml)");
string fileType = Console.ReadLine();
sBuilder.AppendLine("Date, Name From, Name To, Narrative, Amount");
foreach (var output in outputOfPrintAccount)
{
sBuilder.AppendLine(output.ToCSVOutput());
}
switch (fileType)
{
case ".json":
var jArray = GenerateJSON(outputOfPrintAccount);
File.WriteAllText(@"C:\Work\Training\SupportBank\ExportTest.json", jArray.ToString());
Console.WriteLine("File has been created");
break;
case ".csv":
File.WriteAllText(@"C:\Work\Training\SupportBank\ExportTest.csv", sBuilder.ToString());
Console.WriteLine("File has been created");
break;
case ".xml":
var xml = XMLparse.XMLdata(outputOfPrintAccount);
File.WriteAllText(@"C:\Work\Training\SupportBank\ExportTest.xml", xml.ToString());
Console.WriteLine("File has been created");
break;
default:
Console.WriteLine("Sorry, " + fileType + " is not an acceptable format");
UserInput();
break;
}
}
private JArray GenerateJSON(List<Transaction> transactions)
{
return new JArray(
transactions.Select(
transaction => new JObject
{
{ "Date", transaction.Date },
{ "FromAccount", transaction.FromAccount },
{ "ToAccount", transaction.ToAccount },
{ "Narrative", transaction.Narrative },
{ "Amount", transaction.Amount }
}
)
);
}
}
}