-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATM_Final_Software.c
499 lines (462 loc) · 13.6 KB
/
ATM_Final_Software.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*****************************************************
* THE LEGO ATM *
* Author: Jacob Rampertab *
* Author: Baraa Hamodi *
* Author: Hammad Mirza *
* Author: Arthur Dabu *
*****************************************************/
/*****************************************************
* We had issues with pow(10, 2) returning 99 instead *
* of 100, so the function was overridden. *
*****************************************************/
float pow(int b, int e) {
float r = 1;
if (e == -1) {
return (1 / b);
}
for (int i = 0; i < e; i++) {
r *= b;
}
return r;
}
/*****************************************************
* This is the bankAccount struct that contains all *
* of the information about a single bank account. *
*****************************************************/
typedef struct
{
float balance;
int pin;
int cardCode;
}
bankAccount;
bankAccount accounts[5];
float stock = 300;
const string sFileName = "accountInfo.txt";
TFileIOResult nIoResult;
TFileHandle hFileHandle;
int nFileSize = 100;
/*****************************************************
* This function is for setting the default values to *
* the bank accounts when the file does not exist. *
*****************************************************/
void defaults() {
// Jacob Rampertab
accounts[0].balance = 1000.00;
accounts[0].pin = 6497;
accounts[0].cardCode = 532;
// Baraa Hamodi
accounts[1].balance = 545.54;
accounts[1].pin = 7777;
accounts[1].cardCode = 245;
// Hammad Mirza
accounts[2].balance = 45.91;
accounts[2].pin = 5951;
accounts[2].cardCode = 325;
// Arthur
accounts[3].balance = 333.66;
accounts[3].pin = 8008;
accounts[3].cardCode = 453;
// Additional Test User
accounts[4].balance = 9999.00;
accounts[4].pin = 1111;
accounts[4].cardCode = 434;
}
/*****************************************************
* This function is for setting the default values of *
* the bank accounts when the file does exist. *
*****************************************************/
void readIn() {
CloseAllHandles(nIoResult);
OpenRead(hFileHandle, nIoResult, sFileName, nFileSize);
for (int i = 0; i < 5; i++) {
ReadFloat(hFileHandle, nIoResult, accounts[i].balance);
ReadLong(hFileHandle, nIoResult, accounts[i].pin);
ReadLong(hFileHandle, nIoResult, accounts[i].cardCode);
}
Close(hFileHandle, nIoResult);
}
/*****************************************************
* This function is for updating the account values *
* in the file when a trasaction is complete. *
*****************************************************/
void writeOut() {
Delete(sFileName, nIoResult);
CloseAllHandles(nIoResult);
OpenWrite(hFileHandle, nIoResult, sFileName, nFileSize);
for (int i = 0; i < 5; i++) {
WriteFloat(hFileHandle, nIoResult, accounts[i].balance);
WriteLong(hFileHandle, nIoResult, accounts[i].pin);
WriteLong(hFileHandle, nIoResult, accounts[i].cardCode);
}
Close(hFileHandle, nIoResult);
}
/*****************************************************
* This function is for displaying the scrolling menu *
* for the enterPin function. *
*****************************************************/
void dispInt(int n, int l) {
nxtDisplayCenteredTextLine(l + 2, "%d %d", (n == 0? 9 : n - 1), (n == 9? 0 : n + 1));
nxtDisplayCenteredBigTextLine(l, "%d", n);
}
/*****************************************************
* This function is used for getting the pin number *
* from the user through the NXT input methods and *
* returns it as a single interger value. *
*****************************************************/
int enterPin() {
eraseDisplay();
nxtDisplayTextLine(0, "Please enter pin");
int pin = 0, n = 1;
dispInt(n, 3);
for (int digit = 0; digit < 4; digit++) {
string s = "";
for (int j = 0; j < digit; j++) {
s = s + "*";
}
s = s + "_";
nxtDisplayTextLine(7, s);
while (nNxtButtonPressed != 3) {
if (nNxtButtonPressed == 1) {
PlayImmediateTone(650, 10);
n++;
if (n == 10) {
n = 0;
}
dispInt(n,3);
while (nNxtButtonPressed == 1) {}
} else if (nNxtButtonPressed == 2) {
PlayImmediateTone(650, 10);
n--;
if (n == -1) {
n = 9;
}
dispInt(n,3);
while (nNxtButtonPressed == 2) {}
}
}
PlayImmediateTone(700, 10);
while (nNxtButtonPressed == 3) {}
pin += n* pow(10, 3-digit);
}
nxtDisplayTextLine(7, "****");
wait1Msec(1000);
eraseDisplay();
return pin;
}
/*****************************************************
* This function is used for getting the sequence of *
* colours on a debit card which corresponds to a *
* certain account number and balance and then *
* returns it as a single integer value. *
*****************************************************/
int swipeCard() {
eraseDisplay();
nxtDisplayCenteredTextLine(3, "Please swipe");
nxtDisplayCenteredTextLine(4, "card");
nxtDisplayCenteredTextLine(5, "<------");
int oldColour = -2, newColour, cardCode = 0;
while (!SensorValue[S2]) {}
while (SensorValue[S3]) {}
newColour = SensorValue[S1];
for (int i = 0; i < 3; i++) {
while (newColour == oldColour) {
newColour = SensorValue[S1];
}
wait1Msec(100);
newColour = SensorValue[S1];
cardCode = cardCode + newColour * pow(10, 2-i);
oldColour = newColour;
}
PlayImmediateTone(700, 50);
return cardCode;
}
/*****************************************************
* This function is used for comparing the card code *
* and PIN combination against all other accounts *
* stored in the array and returns the index number *
* of the matching account. *
*****************************************************/
int selectAccount() {
int c = swipeCard();
int p = enterPin();
for (int i = 0; i < 4; i++) {
if (accounts[i].pin == p && accounts[i].cardCode == c) {
return i;
}
}
return -1;
}
/*****************************************************
* This function is used for dispensing/outputting *
* the amount of dollars requested by the user. *
*****************************************************/
void dispense(int n) {
eraseDisplay();
nxtDisplayCenteredTextLine(4, "Dispensing...");
for (int a = n / 20; a > 0; a--) {
nMotorEncoder[motorA] = 0;
motor[motorA] = -15;
while (nMotorEncoder[motorA] > -267){}
motor[motorA] = 0;
wait1Msec(200);
PlayImmediateTone(700, 50);
}
nMotorEncoder[motorA] = 0;
motor[motorA] = 15;
while (nMotorEncoder[motorA] < 40){}
motor[motorA] = 0;
eraseDisplay();
nxtDisplayString(1, "Dispensed:");
nxtDisplayCenteredBigTextLine(4, "$%d", n);
wait1Msec(2500);
eraseDisplay();
}
/*****************************************************
* This function is used for displaying the detailed *
* menu for withdrawing money. It also calls the *
* dispenser function and displays appropiriate *
* messages to the user. *
*****************************************************/
void withdraw(int i) {
bool exit = false;
int amount = 0;
eraseDisplay();
nxtDisplayCenteredBigTextLine(5, "$%d.00", amount);
while (nNxtButtonPressed != -1) {}
while (!exit) {
nxtDisplayTextLine(1, "Enter amount you");
nxtDisplayTextLine(2, "wish to withdraw");
nxtDisplayCenteredBigTextLine(5, "$%d.00", amount);
while (nNxtButtonPressed != 3) {
if (nNxtButtonPressed == 1) {
PlayImmediateTone(650, 10);
amount += 20;
if (amount > stock || amount > accounts[i].balance) {
amount -= 20;
}
while (nNxtButtonPressed == 1){}
nxtDisplayCenteredBigTextLine(5, "$%d.00", amount);
} else if (nNxtButtonPressed == 2) {
PlayImmediateTone(650, 10);
amount -= 20;
if (amount < 0) {
amount = 0;
}
while (nNxtButtonPressed == 2) {}
nxtDisplayCenteredBigTextLine(5, "$%d.00", amount);
}
}
eraseDisplay();
if (accounts[i].balance >= amount && stock >= amount) {
accounts[i].balance -= amount;
stock -= amount;
dispense(amount);
nxtDisplayTextLine(4, "Withdrawal Successful");
wait1Msec(1000);
exit = true;
} else {
nxtDisplayTextLine(6, "Error");
}
}
}
/*****************************************************
* This function is used for security purposes and *
* when called will lockdown the robot. This occurs *
* after 3 consecutive incorrect PINS. The only way *
* deactivate this lockdown is to press the orange *
* button and enter the correct security PIN. *
*****************************************************/
void safetyLockdown() {
nMotorEncoder[motorB] = 0;
bool exit = false;
while (!exit) {
motor[motorB] = -40;
while (nMotorEncoder[motorB] > -90) {}
motor[motorC] = 0;
time1[T1] = 0;
bool state = true;
eraseDisplay();
while (nNxtButtonPressed != 3) {
if (time1[T1] >= 500) {
state=!state;
time1[T1] = 0;
if (state) {
nxtDisplayCenteredBigTextLine(3, "WARNING!");
PlayImmediateTone(900, 50);
} else {
eraseDisplay();
}
}
}
eraseDisplay();
while (nNxtButtonPressed == 3) {}
motor[motorB] = 20;
while (nMotorEncoder[motorB] < 0) {}
motor[motorB] = 0;
exit = (enterPin() == 1234);
}
nxtDisplayCenteredBigTextLine(3, "ATM");
nxtDisplayCenteredBigTextLine(5, "RESTORED!");
wait1Msec(2000);
eraseDisplay();
}
/*****************************************************
* This function is used for displaying the detailed *
* menu for depositing money. *
*****************************************************/
void dispDeposit(int n, int l) {
char c;
if (n == 10) {
c = '.';
nxtDisplayCenteredTextLine(l + 2, "9 D");
} else if (n == 11) {
c = 'D';
nxtDisplayCenteredTextLine(l + 2, ". 0");
} else {
c = n + 48;
nxtDisplayCenteredTextLine(l + 2, "%c %c", (c == '0'? 'D' : n + 47), (c == '9'? '.' : n + 49));
}
nxtDisplayCenteredBigTextLine(l, "%c", c);
}
/*****************************************************
* This function is used for using the detailed *
* menu for withdrawing money. It also turns on the *
* motor to deposit money and displays appropiriate *
* messages to the user. *
*****************************************************/
void deposit(int i) {
nxtDisplayCenteredTextLine(0, "Enter amount");
bool afterDec = false;
float amount = 0, adec = 0;
int n = 0, bdec = 0, amax = 1, bmax = 1;
dispDeposit(n, 5);
nxtDisplayCenteredBigTextLine(2, "$%.2f", amount);
while (nNxtButtonPressed != -1) {}
while (n != 11 && amax <= 2) {
while (nNxtButtonPressed != 3) {
if (nNxtButtonPressed == 1) {
PlayImmediateTone(650, 10);
n++;
if (n == 12) {
n = 0;
}
dispDeposit(n,5);
while (nNxtButtonPressed == 1) {}
} else if (nNxtButtonPressed == 2) {
PlayImmediateTone(650, 10);
n--;
if (n == -1) {
n = 11;
}
dispDeposit(n,5);
while (nNxtButtonPressed == 2) {}
}
}
PlayImmediateTone(700, 10);
while (nNxtButtonPressed == 3) {}
if (n == 10 || bmax > 4) {
afterDec = true;
}
if (n >= 0 && n <= 9){
if (afterDec) {
adec = adec + n/(1.0*pow(10, amax));
amax++;
} else {
bdec = bdec*10 + n;
bmax++;
}
amount = bdec + adec;
}
nxtDisplayCenteredBigTextLine(2, "$%.2f", amount);
}
wait1Msec(500);
PlayImmediateTone(650, 10);
wait1Msec(100);
PlayImmediateTone(800, 10);
accounts[i].balance += amount;
eraseDisplay();
nxtDisplayCenteredTextLine(3, "Please insert");
nxtDisplayCenteredTextLine(4, "money in envelop");
nxtDisplayCenteredTextLine(5, "into the slot");
wait1Msec(5000);
motor[motorC] = -50;
wait1Msec(10000);
motor[S1] = 0;
eraseDisplay();
nxtDisplayCenteredTextLine(4, "Deposit");
nxtDisplayCenteredTextLine(5, "Successful");
wait1Msec(2500);
}
/*****************************************************
* This is the main function. *
*****************************************************/
task main() {
int accInd, nErrors=0;
TFileIOResult fr;
TFileHandle fh;
string fn;
int fs;
FindFirstFile(fh, fr, "accountInfo.txt", fn, fs);
if (fr != 0) {
defaults();
} else {
readIn();
}
Close(fh, fr);
SensorType[S1] = sensorCOLORFULL;
SensorType[S2] = sensorTouch;
SensorType[S3] = sensorTouch;
while (true) {
nxtDisplayCenteredTextLine(1, "Welcome User");
nxtDisplayCenteredTextLine(4, "Press orange");
nxtDisplayCenteredTextLine(5, "button to begin");
while (nNxtButtonPressed != 3) {
if (SensorValue[S3]) {
stock = 300;
}
}
PlayImmediateTone(700, 10);
eraseDisplay();
do {
accInd = selectAccount();
if (accInd == -1) {
eraseDisplay();
nxtDisplayCenteredTextLine(3, "Bad swipe or pin");
nErrors++;
wait1Msec(3000);
}
if (nErrors >= 3) {
safetyLockdown();
} else {
nxtDisplayCenteredTextLine(5, "Try Again");
}
} while (accInd == -1);
bool sameAccount = true;
while (sameAccount) {
eraseDisplay();
nxtDisplayCenteredTextLine(1, "Bal: %.2f", accounts[accInd].balance);
nxtDisplayCenteredTextLine(3, "Choose action:");
nxtDisplayCenteredTextLine(6, "Deposit Withdraw");
while (nNxtButtonPressed != 1 && nNxtButtonPressed != 2) {}
PlayImmediateTone(700, 10);
eraseDisplay();
if (nNxtButtonPressed == 1) {
withdraw(accInd);
} else if (nNxtButtonPressed == 2) {
deposit(accInd);
}
eraseDisplay();
nxtDisplayCenteredTextLine(1, "Another transaction?");
nxtDisplayCenteredTextLine(6, "Yes No");
while (nNxtButtonPressed != 1 && nNxtButtonPressed != 2) {}
PlayImmediateTone(700, 10);
if (nNxtButtonPressed == 1) {
sameAccount = false;
}
while (nNxtButtonPressed != -1) {}
}
writeOut();
eraseDisplay();
}
}