-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1236 lines (1049 loc) · 45.2 KB
/
Copy pathmain.cpp
File metadata and controls
1236 lines (1049 loc) · 45.2 KB
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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string>
#include <iostream>
#include <unordered_set>
#include <fstream>
#include <ctime>
#include <vector>
#include <algorithm>
#include <cctype>
#include <regex>
#include <stdexcept>
/* - CUSTOMER - */
class Customer{
private:
std::string ID;
std::string name, surname;
/*
used_credentials is an unordered set used to avoid to have the same name-surname for two different Customers.
*/
static std::unordered_set<std::string> used_credentials;
std::string create_ID_customer(std::string _name, std::string _surname){
std::string _ID = build_ID(_name, _surname);
// check credentials uniqueness
check_credentials_uniqueness( _name, _surname );
// add to set
add_to_used_credentials(_name, _surname);
return _ID;
}
public:
/*
NOTE: method needed to re-build used_credentials from file.
*/
static void clear_used_credentials() {
used_credentials.clear();
}
// static to be used in load_from_file() too
static std::string build_credentials(std::string _name, std::string _surname){
return _name + "#" + _surname;
}
// static to be used in load_from_file() too
static void check_credentials_uniqueness(std::string _name, std::string _surname){
if ( used_credentials.find( build_credentials(_name, _surname) ) != used_credentials.end() ){
throw std::runtime_error("Combination of name and surname already existing. Please try a new one.");
}
}
void add_to_used_credentials(std::string _name, std::string _surname){
used_credentials.insert( build_credentials(_name, _surname) );
}
void remove_from_used_credentials(std::string _name, std::string _surname) {
used_credentials.erase( build_credentials(_name, _surname) );
}
std::string build_ID(std::string _name, std::string _surname){
time_t tmstp;
std::string readable_time = std::to_string(time(&tmstp));
return build_credentials(_name, _surname) + "#" + readable_time;
}
/*
Optional bool and _ID are needed to deal with Customer creation from file; in this case ID mustn't be created but passed.
*/
Customer(std::string _name, std::string _surname, bool from_file=false, std::string _ID = ""){
if (from_file == false) {
ID = create_ID_customer(_name, _surname);
}
else {
ID = _ID; // from file, you need to keep the same value, not create a new one.
}
name = _name;
surname = _surname;
}
//std::string get_ID(){
const std::string& get_ID() const {
return ID;
}
//std::string get_name(){
const std::string& get_name() const {
return name;
}
void set_name(std::string _name){
name = _name;
}
//std::string get_surname(){
const std::string& get_surname() const {
return surname;
}
void set_surname(std::string _surname){
surname = _surname;
}
void print_customer() const {
std::cout << "ID: " << ID << ", Name: " << name << ", Surname: " << surname << "\n" << std::endl;
}
};
std::unordered_set<std::string> Customer::used_credentials;
/* - INTERACTION - */
class Interaction{
protected:
std::string customer_ID; // name+surname+timestamp
std::string title; // info to recognise the interaction
static std::unordered_set<std::string> used_titles;
public:
/*
NOTE: method needed to re-build used_titles from file.
*/
static void clear_used_titles() {
used_titles.clear();
}
// static to be used in load_from_file() too
static void check_title_uniqueness(std::string _title){
if ( used_titles.find( _title ) != used_titles.end() ){
throw std::runtime_error("Title already existing. Please try a new one.");
}
}
void add_to_used_titles(std::string _title){
used_titles.insert(_title);
}
void remove_from_used_titles(std::string _title){
used_titles.erase(_title);
}
Interaction(std::string _customer_ID, std::string _title, bool from_file=false){
// check title, not if from file
if (!from_file) {
check_title_uniqueness(_title);
}
// if ok proceed
customer_ID = _customer_ID;
title = _title;
// add to set
used_titles.insert(title);
}
Interaction(){
customer_ID = "_customer_ID";
title = "_title";
}
// get customer_ID method
const std::string& get_customer_ID() const {
return customer_ID;
}
// NO set customer_ID method to keep consistent the data
// get/set title methods
const std::string& get_title() const {
return title;
}
/*
set_title functions:
- check title uniqueness
- remove the old title from used_titles
- update the title
- add the new title to used_titles
*/
void set_title(std::string _title){
check_title_uniqueness(_title);
remove_from_used_titles(title);
title = _title;
add_to_used_titles(title);
}
};
std::unordered_set<std::string> Interaction::used_titles;
/* - APPOINTMENT - */
class Appointment : public Interaction{
private:
std::string date;
public:
Appointment(std::string _customer_ID, std::string _title, std::string _date, bool _from_file = false)
: Interaction(_customer_ID, _title, _from_file), date(_date) {}
// get/set date methods
const std::string& get_date() const {
return date;
}
void set_date(std::string _date){
date = _date;
}
void print_appointment() const {
std::cout << "Title: " << title << ", Date: " << date << "\n" << std::endl;
}
};
/* - CONTRACT - */
class Contract : public Interaction{
private:
std::string contract_path;
public:
Contract(std::string _customer_ID, std::string _title, std::string _contract_path, bool _from_file = false)
: Interaction(_customer_ID, _title, _from_file), contract_path(_contract_path) {}
// get/set contract_path methods
const std::string& get_contract_path() const {
return contract_path;
}
void set_contract_path(std::string _contract_path){
contract_path = _contract_path;
}
void print_contract() const {
std::cout << "Title: " << title << ", Contract path: " << contract_path << "\n" << std::endl;
}
};
/* - UTILITY acquire from user FUNCTIONS - */
struct Person{
std::string customer_name;
std::string customer_surname;
};
// cannot be empty message + minimum typed accepted (ex. '-' or '12/12/12')
void cannot_be_empty(const std::string& what, const std::string& minimum_typed) {
std::cout << what << " cannot be empty. Minimum typed acceped is ' " << minimum_typed << " '" << std::endl;
}
Person acquire_name_surname(){
Person p;
while (true) {
std::cout << "Enter customer name: " << std::endl;
std::getline(std::cin, p.customer_name);
if (!p.customer_name.empty())
break;
cannot_be_empty("Name","-");
}
while (true) {
std::cout << "Enter customer surname: " << std::endl;
std::getline(std::cin, p.customer_surname);
if (!p.customer_surname.empty())
break;
cannot_be_empty("Surname","-");
}
return p;
}
std::string acquire_title(std:: string action_message="Enter interaction title: "){
std::string t;
while (true) {
std::cout << action_message << std::endl;
getline(std::cin, t);
if (!t.empty())
return t;
cannot_be_empty("Title","-");
}
}
/*
Function to validate date. It checks for:
- valid format
- valid days, month, years (ex. you cannot type 37 as day-date)
- leap year (28 february's days or more)
It returns a string containing a valid date.
*/
std::string validate_date(const std::string& date) {
/*
Regex to check for:
- format DD/MM/YY
- valid input (if user types a something like "hello" it will not be accepted).
*/
static const std::regex date_regex(
R"(^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/([0-9]{2})$)"
);
if (!regex_match(date, date_regex)) {
throw std::runtime_error("Invalid date format. Use DD/MM/YY.");
}
int day = stoi(date.substr(0, 2));
int month = stoi(date.substr(3, 2));
int yy = stoi(date.substr(6, 2));
int days_in_month[] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
// standard disambiguation to check leap year
int year = (yy <= 69) ? (2000 + yy) : (1900 + yy);
bool is_leap =
(year % 4 == 0 && year % 100 != 0) ||
(year % 400 == 0);
if (month == 2 && is_leap) {
days_in_month[1] = 29;
}
// check real day
if (day > days_in_month[month - 1]) {
throw std::runtime_error("Invalid date: day exceeds days in month.");
}
return date;
}
std::string acquire_date(std::string action_message = "Enter interaction date (DD/MM/YY): ") {
std::string d;
while (true) {
std::cout << action_message << std::endl;
std::getline(std::cin, d);
if (d.empty()) {
cannot_be_empty("Date", "12/12/12");
continue;
}
try {
return validate_date(d);
}
catch (const std::runtime_error& e) {
std::cout << "Invalid date: " << e.what() << std::endl;
}
}
}
std::string acquire_contract_path(std:: string action_message="Enter interaction contract path: "){
std::string cp;
while (true) {
std::cout << action_message << std::endl;
getline(std::cin, cp);
if (!cp.empty())
return cp;
cannot_be_empty("Contract path","-");
}
}
/*
Function to set and check a limit of attempts to user input; it avoids infinite loop in extreme cases.
*/
bool attempt_counter(int& _counter) {
++_counter;
return _counter <= 3;
}
/* - END - UTILITY acquire from user FUNCTIONS - */
/* - CustomerContainer - */
class CustomerContainer{
public:
/* - CUSTOMERS - */
std::vector<Customer> customers_list;
std::vector<Appointment> appointments_list;
std::vector<Contract> contracts_list;
// view all customers
std::vector<Customer> get_customers_list(){
return customers_list;
}
// print ID, name and surname of each customer.
void print_all_customers(){
const std::vector<Customer>& all_customers = get_customers_list();
if (all_customers.empty()) {
std::cout << "No customer exists yet.\n" << std::endl;
return;
}
for (size_t i = 0; i < all_customers.size(); i++){
all_customers[i].print_customer();
}
}
// create a new customer - NOTE it does not just create but it also asks for name and surname.
void create_customer(){
int counter = 0;
while(true){
if (!attempt_counter(counter)) {
std::cout << "Too many attempts.\n";
break;
}
try{
Person p = acquire_name_surname();
Customer new_customer(p.customer_name, p.customer_surname);
customers_list.push_back(new_customer);
std::cout << "New customer with name " << p.customer_name << " and surname " << p.customer_surname << " has been succesfully created.\n" << std::endl;
break;
}
catch (const std::runtime_error& e) {
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
}
// get a specific customer
/* Asks name and surname through console and returns the related Customer */
Customer* get_customer_by_name_surname(){
Person p = acquire_name_surname();
for (size_t i = 0; i < customers_list.size(); i++){
if (customers_list[i].get_name() == p.customer_name && customers_list[i].get_surname() == p.customer_surname){
return &customers_list[i];
}
}
// if no customer found
std::cout << "No customer with name " << p.customer_name << " and surname " << p.customer_surname << " has been found.\n" << std::endl;
return nullptr;
}
// edit a customer - NOTE it does not just edit but it also asks for name and surname.
void edit_customer(){
std::cout << "Enter the customer you want to edit details:" << std::endl;
Customer* c = get_customer_by_name_surname();
if (!c) {
// addictional console message (a first "not found" message. is already in get_customer_by_name_surname()).
std::cout << "Impossible to edit a not found customer.\n" << std::endl;
return;
}
int counter = 0;
while(true){
if (!attempt_counter(counter)) {
std::cout << "Too many attempts.\n";
break;
}
try{
Person customer_new_details = acquire_name_surname();
// check uniqeness
// (NOTE: the customer ID does NOT update itself! this check only avoids to confirm new name-surname if already existing)
c->check_credentials_uniqueness( customer_new_details.customer_name, customer_new_details.customer_surname );
c->remove_from_used_credentials(c->get_name(), c->get_surname());
c->set_name(customer_new_details.customer_name);
c->set_surname(customer_new_details.customer_surname);
c->add_to_used_credentials(c->get_name(), c->get_surname());
std::cout << "New name is: " << c->get_name() << " and new surname is: " << c->get_surname() << "\n" << std::endl;
break;
}
catch (const std::runtime_error& e) {
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
}
/*
Function to delete interactions related to a Customer once this customer is deleted.
*/
void delete_cascade_interactions(std::string customer_ID){
// appointments
for (auto it = appointments_list.begin(); it != appointments_list.end(); ) {
if (it->get_customer_ID() == customer_ID) {
// remove from used_titles
it->remove_from_used_titles(it->get_title());
// remove from appointments_list and delete object
it = appointments_list.erase(it);
} else {
++it;
}
}
// contracts
for (auto it = contracts_list.begin(); it != contracts_list.end(); ) {
if (it->get_customer_ID() == customer_ID) {
// remove from used_titles
it->remove_from_used_titles(it->get_title());
// remove from appointments_list and delete object
it = contracts_list.erase(it);
} else {
++it;
}
}
}
// delete a customer - NOTE it does not just delete but it also asks for name and surname.
void delete_customer(){
std::cout << "Enter the customer you want to delete details:" << std::endl;
Customer* c = get_customer_by_name_surname();
if (!c) {
// addictional console message (a first "not found" message. is already in get_customer_by_name_surname()).
std::cout << "Impossible to delete a not found customer.\n" << std::endl;
return;
}
std::string typed_confirmation;
std::cout << "Are you sure you want to proceed to delete this customer ("<< c->get_name() << " " << c->get_surname() <<")? [Y/N]\n Type Y to proceed or N to stop it." << std::endl;
getline(std::cin, typed_confirmation);
if (typed_confirmation != "Y" && typed_confirmation != "y"){
std::cout << "Delection stopped." << std::endl;
return;
}
try {
// save info before deleting object
std::string name = c->get_name();
std::string surname = c->get_surname();
std::string customer_ID = c->get_ID();
// delete interactions with that customer_id
delete_cascade_interactions(customer_ID);
// remove from used
c->remove_from_used_credentials(name, surname);
//remove element
auto it = find_if(customers_list.begin(), customers_list.end(),
[c](const Customer& elem) {
return elem.get_ID() == c->get_ID();
});
if (it != customers_list.end()) {
customers_list.erase(it);
std::cout << "Customer "<< name << " " << surname <<" succesfully deleted.\n" << std::endl;
}
}
catch (const std::runtime_error& e){
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
/* - INTERACTIONS - */
/* - Appointment - */
void create_appointment(std::string _customer_ID){
int counter = 0;
while(true){
if (!attempt_counter(counter)) {
std::cout << "Too many attempts.\n";
break;
}
try{
// acquire info
std::string title, date;
title = acquire_title("Enter a title for this Appointment: ");
date = acquire_date("Enter a date for this Appointment (standard format: DD/MM/YY) : ");
Appointment new_appointment(_customer_ID, title, date);
appointments_list.push_back(new_appointment);
std::cout << "New appointment with title " << title << " and date " << date << " has been succesfully created.\n" << std::endl;
break;
}
catch (const std::runtime_error& e){
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
};
/* Asks title through console and returns the related Appointment */
Appointment* get_appointment_by_title(){
std::string title = acquire_title();
for (size_t i = 0; i < appointments_list.size(); i++){
if (appointments_list[i].get_title() == title){
return &appointments_list[i];
}
}
// if no customer found
std::cout << "No appointment with title " << title << " has been found.\n" << std::endl;
return nullptr;
}
// edit a appointment - NOTE it does not just edit but it also asks for title.
void edit_appointment(Appointment* a){
if (!a) {
// addictional console message (a first "not found" message. is already in get_appointment_by_title()).
std::cout << "Impossible to edit a not found appointment.\n" << std::endl;
return;
}
int counter = 0;
while(true){
if (!attempt_counter(counter)) {
std::cout << "Too many attempts.\n";
break;
}
try{
std::string appointment_new_title = acquire_title();
std::string appointment_new_date = acquire_date();
a->set_title(appointment_new_title);
a->set_date(appointment_new_date);
std::cout << "New title is: " << a->get_title() << " and new date is: " << a->get_date() << "\n" << std::endl;
break;
}
catch (const std::runtime_error& e){
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
}
// delete a appointment - NOTE it does not just delete but it also asks for title.
void delete_appointment(Appointment* a){
if (!a) {
// addictional console message (a first "not found" message. is already in get_appointment_by_title()).
std::cout << "Impossible to delete a not found appointment.\n" << std::endl;
return;
}
std::string typed_confirmation;
std::cout << "Are you sure you want to proceed to delete this appointment ( "<< a->get_title() << " )? [Y/N]\n Type Y to proceed or N to stop it." << std::endl;
getline(std::cin, typed_confirmation);
if (typed_confirmation != "Y" && typed_confirmation != "y"){
std::cout << "Delection stopped." << std::endl;
return;
}
try{
// save info before deleting object
std::string title = a->get_title();
// remove from used
a->remove_from_used_titles(title);
//remove element
auto it = find_if(appointments_list.begin(), appointments_list.end(),
[a](const Appointment& elem) {
return elem.get_title() == a->get_title() && elem.get_customer_ID() == a->get_customer_ID();
});
if (it != appointments_list.end()) {
appointments_list.erase(it);
std::cout << "Appointment " << title << " successfully deleted.\n" << std::endl;
}
}
catch (const std::runtime_error& e){
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
/* - Contract - */
void create_contract(std::string _customer_ID){
int counter = 0;
while(true){
if (!attempt_counter(counter)) {
std::cout << "Too many attempts.\n";
break;
}
try{
// acquire info
std::string title, contract_path;
title = acquire_title("Enter a title for this Contract: ");
contract_path = acquire_contract_path("Enter a contract path for this Contract: ");
Contract new_contract(_customer_ID, title, contract_path);
contracts_list.push_back(new_contract);
std::cout << "New contract with title " << title << " and contract path " << contract_path << " has been succesfully created.\n" << std::endl;
break;
}
catch (const std::runtime_error& e){
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
};
/* Asks title through console and returns the related Contract */
Contract* get_contract_by_title(){
std::string title = acquire_title();
for (size_t i = 0; i < contracts_list.size(); i++){
if (contracts_list[i].get_title() == title){
return &contracts_list[i];
}
}
// if no customer found
std::cout << "No contract with title " << title << " has been found.\n" << std::endl;
return nullptr;
}
// edit a contract - NOTE it does not just edit but it also asks for title.
void edit_contract(Contract* c){
if (!c) {
// addictional console message (a first "not found" message. is already in get_contract_by_title()).
std::cout << "Impossible to edit a not found contract.\n" << std::endl;
return;
}
int counter = 0;
while(true){
if (!attempt_counter(counter)) {
std::cout << "Too many attempts.\n";
break;
}
try{
std::string contract_new_title = acquire_title();
std::string contract_new_contract_path = acquire_contract_path();
c->set_title(contract_new_title);
c->set_contract_path(contract_new_contract_path);
std::cout << "New title is: " << c->get_title() << " and new contract path is: " << c->get_contract_path() << "\n" << std::endl;
break;
}
catch (const std::runtime_error& e){
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
}
// delete a contract - NOTE it does not just delete but it also asks for title.
void delete_contract(Contract* c){
if (!c) {
// addictional console message (a first "not found" message. is already in get_contract_by_title()).
std::cout << "Impossible to delete a not found contract.\n" << std::endl;
return;
}
std::string typed_confirmation;
std::cout << "Are you sure you want to proceed to delete this contract ( "<< c->get_title() << " )? [Y/N]\n Type Y to proceed or N to stop it." << std::endl;
getline(std::cin, typed_confirmation);
if (typed_confirmation != "Y" && typed_confirmation != "y"){
std::cout << "Delection stopped." << std::endl;
return;
}
try{
// save info before deleting object
std::string title = c->get_title();
// remove from used
c->remove_from_used_titles(title);
//remove element
auto it = find_if(contracts_list.begin(), contracts_list.end(),
[c](const Contract& elem) {
return elem.get_title() == c->get_title() && elem.get_customer_ID() == c->get_customer_ID();
});
if (it != contracts_list.end()) {
contracts_list.erase(it);
std::cout << "Contract " << title << " successfully deleted.\n" << std::endl;
}
}
catch (const std::runtime_error& e){
std::cout << "Error: " << e.what() << std::endl;
// try again
}
}
};
/* - FILE - */
std::string const file_name = "file.csv";
std::string const file_customers_header = "---- CUSTOMERS ---- : customer_id,name,surname";
std::string const file_appointments_header = "---- APPOINTMENTS ---- : customer_id,title,date";
std::string const file_contracts_header = "---- CONTRACTS ---- : customer_id,title,contract_path";
// - UTILITY FILE
/*
Function to avoid mishandling between csv comma and value comma.
If std::string contains comma this functions it will double it otherwise nothing happens.
Ex. "hello,world" >became> "hello,,world" while "no comma here" >became> "no comma here".
*/
std::string manage_escape_comma(const std::string& input){
/*
Quick trick of double comma, it should avoid any realistic typing.
If comma in std::string it will be doubled otherwise nothing happens.
This allows to check when rebuilding Objects to not have mistaken csv comma with value comma or other way.
*/
std::string result;
for (char c : input) {
if (c == ',')
result += ",,";
else
result += c;
}
return result;
}
/*
Reverse for std::string manage_escape_comma(const std::string& input) function BUT:
- splits ONLY if there's a single comma,
- if double comma it reduces it to a single one.
Ex. "a,b,c" >became> ["a","b","c"] while "a,,b,c,d" >became> ["a,b","c","d"]
*/
std::vector<std::string> manage_unescape_single_comma(const std::string& line) {
std::vector<std::string> fields;
std::string current;
for (size_t i = 0; i < line.size(); i++) {
if (line[i] == ',') {
if (i + 1 < line.size() && line[i + 1] == ',') {
// escaped comma
current += ',';
i++; // skip second comma
} else {
// real separator
fields.push_back(current);
current.clear();
}
} else {
current += line[i];
}
}
fields.push_back(current); // last field
return fields;
}
// - end - UTILITY FILE
/*
Function to save on a csv file all customers, appointments and contracts.
- it opens a file (or creates it if not existing) and manage to close it ones the job is finished
- then it writes all the customers, appointments and contracts.
*/
void save_to_file(CustomerContainer& _main_CustomerContainer){
std::ofstream file( file_name, std::ios::out );
if (!file) {
std::cerr << "Error opening the file.";
return;
}
if (_main_CustomerContainer.customers_list.size() > 0) {
file << file_customers_header << "\n";
for (const Customer& c : _main_CustomerContainer.customers_list) {
file << manage_escape_comma( c.get_ID() ) << "," << manage_escape_comma( c.get_name() ) << "," << manage_escape_comma( c.get_surname() ) << '\n';
}
file << "\n"; // readability
}
if (_main_CustomerContainer.appointments_list.size() > 0) {
file << file_appointments_header << "\n";
for (const Appointment& a : _main_CustomerContainer.appointments_list) {
file << manage_escape_comma( a.get_customer_ID() ) << "," << manage_escape_comma( a.get_title() ) << "," << manage_escape_comma( a.get_date() ) << '\n';
}
file << "\n"; // readability
}
if (_main_CustomerContainer.contracts_list.size() > 0) {
file << file_contracts_header << "\n";
for (const Contract& c : _main_CustomerContainer.contracts_list) {
file << manage_escape_comma( c.get_customer_ID() ) << "," << manage_escape_comma( c.get_title() ) << "," << manage_escape_comma( c.get_contract_path() ) << '\n';
}
}
std::cout << "File saved.\n" << std::endl;
}
/*
Function to load data from file:
- clears used_credentials and used_titles
- clears lists (customers, appointments, contracts)
- open file (and manage to close it)
- (line by line) re-writes each customer, appointment and contract
- (line by line) fills used_credentials and used_titles
- (line by line) fills lists (customers, appointments, contracts).
*/
void load_from_file(CustomerContainer& _main_CustomerContainer){
// clear used_credentials and titles
Customer::clear_used_credentials();
Interaction::clear_used_titles();
// clear lists (customers, appointments, contracts)
_main_CustomerContainer.customers_list.clear();
_main_CustomerContainer.appointments_list.clear();
_main_CustomerContainer.contracts_list.clear();
enum class Section {default_section, customers_section, appointments_section, contracts_section};
std::ifstream file( file_name );
if (!file.is_open()) {
std::cerr << "File not found.\n";
return;
}
Section current_section = Section::default_section;
std::string line;
while (getline(file, line)) {
if (line.empty()) continue;
if (line == file_customers_header) {
current_section = Section::customers_section;
continue;
}
if (line == file_appointments_header) {
current_section = Section::appointments_section;
continue;
}
if (line == file_contracts_header) {
current_section = Section::contracts_section;
continue;
}
if (current_section == Section::customers_section) {
std::vector<std::string> fields_from_line = manage_unescape_single_comma(line);
// check to have a correct amount of fields
if (fields_from_line.size() != 3) {
std::cout << "\nCorrupted data for line: " << line << "\n" << std::endl;
continue;
}
try {
// check for duplicates
Customer::check_credentials_uniqueness(fields_from_line[1],fields_from_line[2]);
// build customer
Customer c(fields_from_line[1], fields_from_line[2], true, fields_from_line[0]);
// push into used_credentials
c.add_to_used_credentials(fields_from_line[1], fields_from_line[2]);
// push to customers_list
_main_CustomerContainer.customers_list.push_back(c);
}
catch (const std::runtime_error& e) {
std::cout << ". Error: " << e.what() << "\nSkipping line: " << line << std::endl;
continue;
}
continue;
}
if (current_section == Section::appointments_section) {
std::vector<std::string> fields_from_line = manage_unescape_single_comma(line);
// check to have a correct amount of fields
if (fields_from_line.size() != 3) {
std::cout << "\nCorrupted data for line: " << line << "\n" << std::endl;
continue;
}
try {
// check for duplicates
Appointment::check_title_uniqueness(fields_from_line[1]);
// check for valid date
validate_date(fields_from_line[2]);
// build appointment
Appointment a(fields_from_line[0], fields_from_line[1], fields_from_line[2], true);
// push to appointments_list
_main_CustomerContainer.appointments_list.push_back(a);
}
catch (const std::runtime_error& e) {
std::cout << ". Error: " << e.what() << "\nSkipping line: " << line << std::endl;
continue;
}
continue;
}
if (current_section == Section::contracts_section) {
std::vector<std::string> fields_from_line = manage_unescape_single_comma(line);
// check to have a correct amount of fields
if (fields_from_line.size() != 3) {
std::cout << "\nCorrupted data for line: " << line << "\n" << std::endl;
continue;
}
try {
// check for duplicates
Contract::check_title_uniqueness(fields_from_line[1]);
// build contract
Contract c(fields_from_line[0], fields_from_line[1], fields_from_line[2], true);
// push to contracts_list
_main_CustomerContainer.contracts_list.push_back(c);