/
LineSystemCommandParser.cs
221 lines (185 loc) · 7.45 KB
/
LineSystemCommandParser.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Eksamensopgaven_2015
{
class LineSystemCommandParser
{
LineSystem _lineSystem;
LineSystemCLI _CLI;
private Dictionary<string, Action<string[]>> _adminCmd = new Dictionary<string, Action<string[]>>();
private Dictionary<string, Action<string[]>> _userCmd = new Dictionary<string, Action<string[]>>();
public LineSystemCommandParser(LineSystemCLI CLI, LineSystem lineSystem)
{
_lineSystem = lineSystem;
_CLI = CLI;
_adminCmd.Add(":adduser", AddUser);
_adminCmd.Add(":activate", ActiveProductOnOff);
_adminCmd.Add(":deactivate", ActiveProductOnOff);
_adminCmd.Add(":addcash", InsertCashTransactionCmd);
_adminCmd.Add(":crediton", BoughtOnCreditOnOff);
_adminCmd.Add(":creditoff", BoughtOnCreditOnOff);
_adminCmd.Add(":quit", QuitProgram);
}
public void ParseCmd(string userInput)
{
string[] cmdSplitter = userInput.Split(' ');
if (cmdSplitter.Count() == 0)
{
//Kommer brugeren til at trykke enter uden at have skrevet noget, sker der intet.
}
else if (cmdSplitter[0].StartsWith(":"))
{
var dictionaryValue = _adminCmd.FirstOrDefault(dv => dv.Key.Equals(cmdSplitter[0]));
if (dictionaryValue.Value != null)
{
_adminCmd[dictionaryValue.Key].Invoke(cmdSplitter);
}
else
{
_CLI.DisplayAdminCommandNotFoundMessage(userInput);
}
}
else
{
if (cmdSplitter.Count() == 1)
{
_CLI.DisplayUserInfo(cmdSplitter[0]);
}
else if (cmdSplitter.Count() == 2)
{
bool isDigitOnly = true;
foreach (char checker in cmdSplitter[1])
{
if (checker < '0' || checker > '9')
isDigitOnly = false;
}
if (isDigitOnly)
_CLI.DisplayUserBuysProduct(cmdSplitter[0], Convert.ToUInt32(cmdSplitter[1]));
else
_CLI.DisplayProductIDNotNumber(cmdSplitter[1]);
}
else if (cmdSplitter.Count() == 3)
{
bool isDigitOnly = true;
foreach (char checker in cmdSplitter[1])
{
if (checker < '0' || checker > '9')
isDigitOnly = false;
}
if (!isDigitOnly)
_CLI.DisplayProductIDNotNumber(cmdSplitter[1]);
foreach (char checker in cmdSplitter[2])
{
if (checker < '0' || checker > '9')
isDigitOnly = false;
}
if (!isDigitOnly)
_CLI.DisplayProductIDNotNumber(cmdSplitter[2]);
foreach (User user in _lineSystem.userList)
{
if (user.userName == cmdSplitter[0])
{
foreach (Product product in _lineSystem.productList)
{
if (product.productID == Convert.ToUInt32(cmdSplitter[2]))
{
if (user.balance < (product.price * Convert.ToInt32(cmdSplitter[1])))
{
throw new InsufficientCreditsException("Du har ikke råd til denne vare. Indsæt venligst flere penge på din konto og prøv igen.");
}
for (int i = 0; i < Convert.ToInt32(cmdSplitter[1]); ++i)
{
_lineSystem.BuyProduct(user, product);
}
BuyTransaction buyTransaction = new BuyTransaction(user, product);
_CLI.DisplayUserBuysProduct(user.userName, Convert.ToInt32(cmdSplitter[1]), product);
}
}
}
}
}
else
{
_CLI.DisplayTooManyArgumentsError();
}
}
}
public void AddUser(string[] cmdSplitter)
{
_lineSystem.userList.Add(_CLI.AddUser());
}
public void ActiveProductOnOff (string[] cmdSplitter)
{
bool isDigitOnly = true;
foreach (char checker in cmdSplitter[1])
{
if (checker < '0' || checker > '9')
isDigitOnly = false;
}
if (isDigitOnly)
{
foreach (Product item in _lineSystem.productList)
{
if (item.productID == Convert.ToUInt32(cmdSplitter[1]))
{
if (cmdSplitter[0] == ":activate")
item.active = true;
else
item.active = false;
_CLI.DisplayProductList();
return;
}
}
_CLI.DisplayProductNotFound(Convert.ToUInt32(cmdSplitter[1]));
}
else
_CLI.DisplayProductIDNotNumber(cmdSplitter[1]);
}
public void InsertCashTransactionCmd (string[] cmdSplitter)
{
bool isDigitOnly = true;
foreach (char checker in cmdSplitter[2])
{
if (checker < '0' || checker > '9')
isDigitOnly = false;
}
if (isDigitOnly)
{
foreach (User item in _lineSystem.userList)
{
if (item.userName == cmdSplitter[1])
{
_lineSystem.AddCreditsToAccount(item, Convert.ToDecimal(cmdSplitter[2]));
_CLI.DisplayTransaction();
return;
}
}
_CLI.DisplayUserNotFound(cmdSplitter[1]);
}
else
_CLI.DisplayProductIDNotNumber(cmdSplitter[1]);
}
public void BoughtOnCreditOnOff (string[] cmdSplitter)
{
foreach (Product item in _lineSystem.productList)
{
if (item.productID == Convert.ToUInt32(cmdSplitter[1]))
{
if (cmdSplitter[0] == ":crediton")
item.canBeBoughtOnCredit = true;
else
item.canBeBoughtOnCredit = false;
_CLI.DisplayProductList();
return;
}
}
}
public void QuitProgram (string[] cmdSplitter)
{
_CLI.Close();
}
}
}