This repository has been archived by the owner on Jun 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionallity.h
762 lines (678 loc) · 19.5 KB
/
functionallity.h
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*! Functionallity
* --------------
* @author Wisnu Adi Nurcahyo
* @contact <wisnu@nurcahyo.me>
* @license MIT License
*/
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
/* Parse
-----
@input input String
@input/output command String
arguments <String>
@return Void
Example
-------
input = "delete c 2"
parse(input, command, arguments)
Return
------
command => "delete"
arguments => <"c", "2">
===================================================================*/
void parse(string input, string &command, vector <string> &arguments)
{
if (input.length() > 0)
{
vector <string> tokens, tail;
istringstream iss(input);
copy(istream_iterator <string> (iss),
istream_iterator <string> (),
back_inserter(tokens));
command = tokens.front();
if (tokens.size() < 2) tail = vector <string> ();
else tail = vector <string> (tokens.begin() + 1, tokens.end());
arguments = tail;
}
else command = "";
}
/* isNumeric
---------
@input input String
@return Boolean
Example
-------
bool test = isNumeric("123a")
tset = isNumeric("1998")
Return
------
test => false
tset => true
===================================================================*/
bool isNumeric(string input)
{
unsigned long int i = 0, len = input.length();
bool numeric = true;
if (len > 0)
{
while (numeric && i < len)
{
if (not isdigit(input[i])) numeric = false;
i++;
}
}
return numeric;
}
/* returnValue
-----------
@input input Auto
@return Void
===================================================================*/
void returnValue(auto input)
{
cout << endl << " => " << input << endl << endl;
}
/* returnString
------------
@input input String
@return Void
===================================================================*/
void returnString(string input)
{
cout << endl << " => \"" << input << "\"" << endl << endl;
}
/* returnMsg
---------
@input input String
@return Void
===================================================================*/
void returnMsg(string input)
{
cout << endl << input << endl << endl;
}
/* checkNumThenAssign
------------------
@input input String
@input/output val Int
@return Void
Example
-------
input = "1234"
checkNumThenAssign(input, val)
tupni = "321k"
checkNumThenAssign(tupni, lav)
Return
------
val => 1234
lav =>
===================================================================*/
void checkNumThenAssign(string input, auto &val)
{
if (isNumeric(input)) val = stol(input);
else returnMsg(" Please insert a positive number!");
}
/* isWords
-------
@input input String
@return Boolean
Example
-------
bool test = isWords("name")
tset = isWords("al44")
Return
------
test => true
tset => false
===================================================================*/
bool isWords(string input)
{
unsigned long int i = 0, len = input.length();
bool words = true;
if (len > 0)
{
while (words && i < len)
{
if (isdigit(input[i])) words = false;
i++;
}
}
return words;
}
/* checkWordsThenAssign
--------------------
@input input String
@input/output val String
@return Void
Example
-------
input = "720p"
checkWordsThenAssign(input, val)
tupni = "love"
checkWordsThenAssign(tupni, lav)
Return
------
val =>
lav => "love"
===================================================================*/
void checkWordsThenAssign(string input, string &val)
{
if (isWords(input)) val = input;
else returnMsg(" Please insert a words!");
}
/* joinString
----------
@input input <String>
@return String
Example
-------
string join = joinString(<"one", "two">)
Return
------
join => "one two"
===================================================================*/
string joinString(vector <string> input)
{
stringstream ss;
for (size_t i = 0; i < input.size(); i++)
{
if (i > 0) ss << " ";
ss << input.at(i);
}
return ss.str();
}
/* showHelp
--------
@return Void
===================================================================*/
void showHelp()
{
cout << endl
<< " Bankir is a CLI based banking system management." << endl
<< " Keywords: \"c\" stand for Customer, \"a\" stand for Account." << endl
<< " \"cid\" stand for Customer ID." << endl
<< " \"aid\" stand for Account ID." << endl
<< endl
<< " Commands" << endl
<< " help Display this message" << endl
<< " info Display all customer and their account" << endl
<< " insert [c|a] Insert new data" << endl
<< " delete [c|a] [cid|aid] Delete data by their ID" << endl
<< " update [c|a] [cid|aid] Update data by their ID" << endl
<< " show [c|a] [cid|aid] Show data by their ID" << endl
<< " link <aid> <cid> Link Account to a Customer by ID" << endl
<< " unlink <aid> <cid> Unlink Account from a Customer by ID" << endl
<< " all [c|a] Show all data based on parameter" << endl
<< " maxa Show account with the highest balance" << endl
<< " twos Show all accounts that linked to two customers" << endl
<< " exit Close this program" << endl
<< endl
<< " Examples" << endl
<< " insert c" << endl
<< " This will takes you to the Insert Customer sub-program." << endl
<< endl
<< " delete a 2" << endl
<< " This will remove Account with ID 2." << endl
<< endl;
}
void showInfo(List L)
{
showAllList(L);
}
void attrCustomer()
{
cout << endl
<< " Attributes" << endl
<< " ----------" << endl
<< " id Integer Auto" << endl
<< " age Integer Required" << endl
<< " name String Required" << endl
<< endl
<< " Commands" << endl
<< " --------" << endl
<< " attr Display this" << endl
<< " save Save data" << endl
<< " back Close this interface" << endl
<< endl;
}
/* doInsertCustomer
----------------
@input/output LC customerList
L List
@return Void
===================================================================*/
void doInsertCustomer(customerList &LC, List &L)
{
unsigned long int id;
unsigned int age = 0;
string name (""), input, attr, val;
vector <string> args;
addrCustomer P;
infoTypeCustomer x;
address Q;
infotype y;
attrCustomer();
if (isEmpty(L)) id = 1;
else id = info(last(L)).id + 1;
cout << "[insert| c ]>> ";
while (getline(cin, input) && input != "back")
{
parse(input, attr, args);
if (attr != "" && attr != "save")
{
if (args.size() < 1)
{
if (attr == "attr") attrCustomer();
else if (attr == "id") returnValue(id);
else if (attr == "age") returnValue(age);
else if (attr == "name") returnString(name);
else returnMsg(" Attribute didn't exist!");
}
else
{
val = args.at(0);
if (attr == "attr") attrCustomer();
else if (attr == "id") returnValue(id);
else if (attr == "age")
{
checkNumThenAssign(val, age);
if (age < 17) returnMsg(" Age at least 17!");
else returnValue(age);
}
else if (attr == "name")
{
val = joinString(args);
checkWordsThenAssign(val, name);
returnString(name);
}
else returnMsg(" Attribute didn't exist!");
}
}
else if (attr == "save")
{
if (age < 17 || name == "")
returnMsg(" Please fill out all required data!");
else
{
x.id = id;
x.age = age;
x.name = name;
P = allocateCustomer(x);
insertLastCustomer(LC, P);
y.id = id;
y.customer = P;
Q = allocate(y);
insertLast(L, Q);
returnMsg(" Saved! Temporary data removed!");
id = info(last(L)).id + 1;
age = 0;
name = "";
}
}
cout << "[insert| c ]>> ";
}
}
void attrAccount()
{
cout << endl
<< " Attributes" << endl
<< " ----------" << endl
<< " id Integer Auto" << endl
<< " balance Integer Not Required" << endl
<< endl
<< " Commands" << endl
<< " --------" << endl
<< " attr Display this" << endl
<< " save Save data" << endl
<< " back Close this interface" << endl
<< endl;
}
/* doInsertAccount
---------------
@input/output LA accountList
@return Void
===================================================================*/
void doInsertAccount(accountList &LA)
{
unsigned long int id;
signed long int balance = 0;
string input, attr, val;
vector <string> args;
addrAccount P;
infoTypeAccount x;
attrAccount();
if (isEmptyAccount(LA)) id = 1;
else id = info(last(LA)).id + 1;
cout << "[insert| a ]>> ";
while (getline(cin, input) && input != "back")
{
parse(input, attr, args);
if (attr != "" && attr != "save")
{
if (args.size() < 1)
{
if (attr == "attr") attrAccount();
else if (attr == "id") returnValue(id);
else if (attr == "balance") returnValue(balance);
else returnMsg(" Attribute didn't exist!");
}
else
{
val = args.at(0);
if (attr == "attr") attrAccount();
else if (attr == "id") returnValue(id);
else if (attr == "balance")
{
checkNumThenAssign(val, balance);
returnValue(balance);
}
else returnMsg(" Attribute didn't exist!");
}
}
else if (attr == "save")
{
x.id = id;
x.balance = balance;
P = allocateAccount(x);
insertLastAccount(LA, P);
returnMsg(" Saved! Temporary data removed!");
id = info(last(LA)).id + 1;
balance = 0;
}
cout << "[insert| a ]>> ";
}
}
/* doInsert
--------
@input args <String>
@input/output LC customerList
LA accountList
L List
@return Void
===================================================================*/
void doInsert(vector <string> args, customerList &LC, accountList &LA, List &L)
{
if (args.size() < 1) returnMsg(" Arguments needed!");
else
{
string opt = args.at(0);
if (opt == "c") doInsertCustomer(LC, L);
else if (opt == "a") doInsertAccount(LA);
else returnMsg(" Please provides \"c\" or \"a\"!");
}
cout << endl;
}
/* doLinkList
----------
@input args <String>
@input/output LA accountList
L List
@return Void
===================================================================*/
void doLinkList(vector <string> args, accountList &LA, List &L)
{
if (args.size() < 2) returnMsg(" Need two arguments!");
else
{
if (isNumeric(args.at(0)) && isNumeric(args.at(1)))
{
unsigned long int aid = stol(args.at(0)),
cid = stol(args.at(1));
addrAccount A = findAccountByID(LA, aid);
if (found (A))
{
vector <unsigned long int>::iterator linked;
linked = find(info(A).cid.begin(), info(A).cid.end(), cid);
if (linked == info(A).cid.end())
{
if (info(A).cid.size() < 2)
{
address R = findListByID(L, cid);
if (found (R))
{
info(A).cid.insert(info(A).cid.end(), cid);
info(R).account.insert(info(R).account.end(), A);
returnMsg(" Linked successfuly!");
}
else returnMsg(" Customer not found!");
}
else returnMsg(" Maximum Account link reached! Max: 2.");
}
else returnMsg(" Selected Account has been linked before!");
}
else returnMsg(" Account not found!");
}
else returnMsg(" Arguments must be an ID!");
}
}
/* doUnlinkList
------------
@input args <String>
@input/output LA accountList
L List
@return Void
===================================================================*/
void doUnlinkList(vector <string> args, accountList &LA, List &L)
{
if (args.size() < 2) returnMsg(" Need two arguments!");
else
{
if (isNumeric(args.at(0)) && isNumeric(args.at(1)))
{
unsigned long int aid = stol(args.at(0)),
cid = stol(args.at(1));
addrAccount A = findAccountByID(LA, aid);
if (found (A))
{
vector <unsigned long int>::iterator linked;
linked = find(info(A).cid.begin(), info(A).cid.end(), cid);
if (linked != info(A).cid.end())
{
address R = findListByID(L, cid);
if (found (R))
{
info(A).cid.erase(
remove(
info(A).cid.begin(), info(A).cid.end(), cid
),
info(A).cid.end()
);
info(R).account.erase(
remove(
info(R).account.begin(), info(R).account.end(), A
),
info(R).account.end()
);
returnMsg(" Unlinked successfuly!");
}
else returnMsg(" Customer not found!");
}
else returnMsg(" Account ID didn't found!\n Perhaps indeed not linked.");
}
else returnMsg(" Account not found!");
}
else returnMsg(" Arguments must be an ID!");
}
}
void deleteListByID(List &L, customerList &LC, unsigned long int id,
address &A, addrCustomer &AC)
{
A = findListByID(L, id);
if (found (A))
{
vector <addrAccount> account = info(A).account;
for (int i = 0; i < account.size(); i++)
info(account.at(i)).cid.erase(
remove(
info(account.at(i)).cid.begin(), info(account.at(i)).cid.end(), id
),
info(account.at(i)).cid.end()
);
account.clear();
AC = info(A).customer;
info(A).customer = Nil;
deleteCustomerByAddress(LC, AC);
deleteListByAddress(L, A);
returnMsg(" Customer has been deleted!");
}
else returnMsg(" Customer ID didn't exist!");
}
void deleteAccountByID(List &L, accountList &LA, unsigned long int id,
addrAccount &AA)
{
AA = findAccountByID(LA, id);
if (found (AA))
{
vector <unsigned long int> cid = info(AA).cid;
address A;
for (int i = 0; i < cid.size(); i++)
{
A = findListByID(L, cid.at(i));
info(A).account.erase(
remove(
info(A).account.begin(), info(A).account.end(), AA
),
info(A).account.end()
);
}
info(AA).cid.clear();
deleteAccountByAddress(LA, AA);
returnMsg(" Account has been deleted!");
}
else returnMsg(" Account ID didn't exist!");
}
/* doDelete
--------
@input args <String>
@input/output LC customerList
LA accountList
L List
@return Void
===================================================================*/
void doDelete(vector <string> args, customerList &LC, accountList &LA, List &L,
addrCustomer &AC, addrAccount &AA, address &A)
{
if (args.size() < 2) returnMsg(" Need two arguments!");
else
{
if (!isNumeric(args.at(1))) returnMsg(" Second argument must be an ID!");
else
{
string opt = args.at(0);
unsigned long int id = stol(args.at(1));
if (opt == "c") deleteListByID(L, LC, id, A, AC);
else if (opt == "a") deleteAccountByID(L, LA, id, AA);
else returnMsg(" Please provides \"c\" or \"a\"!");
}
}
}
void doUpdate()
{
cout << "Do update here..." << endl;
}
void showCustomer(addrCustomer P, List L)
{
cout << endl
<< " Customer ID : " << info(P).id << endl
<< " Age : " << info(P).age << endl
<< " Name : " << info(P).name << endl;
int n, i;
address A = findListByID(L, info(P).id);
n = account(A).size();
for (i = 0; i < n; i++)
{
cout << " Account ID : " << info(account(A).at(i)).id << endl
<< " Balance : " << info(account(A).at(i)).balance << endl;
}
cout << endl;
}
void doShow(vector <string> args, customerList LC, accountList LA, List L)
{
if (args.size() < 2) returnMsg(" Need two arguments!");
else
{
if (!isNumeric(args.at(1))) returnMsg(" Second argument must be an ID!");
else
{
string opt = args.at(0);
unsigned long int id = stol(args.at(1));
if (opt == "c")
{
addrCustomer AC = findCustomerByID(LC, id);
if (found (AC)) showCustomer(AC, L);
else returnMsg(" Customer ID didn't exist!");
}
else if (opt == "a")
{
addrAccount AA = findAccountByID(LA, id);
if (found (AA)) showAccount(AA);
else returnMsg(" Account ID didn't exist!");
}
else returnMsg(" Please provides \"c\" or \"a\"!");
}
}
}
void doAll(vector <string> args, customerList LC, accountList LA)
{
if (args.size() < 1) returnMsg(" Need one arguments!");
else
{
string opt = args.at(0);
if (opt == "c") showAllCustomer(LC);
else if (opt == "a") showAllAccount(LA, LC);
else returnMsg(" Please provides \"c\" or \"a\"!");
}
}
void doFindMaxBalance(accountList LA, customerList LC)
{
addrCustomer AC;
addrAccount AA = findMaxBalance(LA);
int n, i;
if (found (AA))
{
cout << endl
<< " Account ID : " << info(AA).id << endl
<< " Balance : " << info(AA).balance << endl;
n = info(AA).cid.size();
if (n > 0) cout << " Linked to:" << endl;
for (i = 0; i < n; i++)
{
AC = findCustomerByID(LC, info(AA).cid.at(i));
cout << " - [" << info(AC).id << "] "
<< info(AC).name << endl;
}
cout << endl;
}
else returnMsg(" Account is empty!");
}
void doTwos(accountList LA, customerList LC)
{
addrAccount AA;
addrCustomer AC;
int n, i;
AA = first(LA);
while (found (AA))
{
n = info(AA).cid.size();
if (n == 2)
{
cout << endl
<< " Account ID : " << info(AA).id << endl
<< " Balance : " << info(AA).balance << endl
<< " Linked to:" << endl;
for (i = 0; i < n; i++)
{
AC = findCustomerByID(LC, info(AA).cid.at(i));
cout << " - [" << info(AC).id << "] "
<< info(AC).name << endl;
}
cout << endl;
}
AA = next(AA);
}
}