-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource1.cpp
More file actions
2101 lines (1587 loc) · 62.2 KB
/
Copy pathSource1.cpp
File metadata and controls
2101 lines (1587 loc) · 62.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 <algorithm> // For std::sort
#include <cctype> // For digit check
#include <chrono> // For timing
#include <cmath> // For C math functions
#include <cstdint> // For specific data type
#include <ctime> // For time related functions
#include <iomanip> // For setw etc
#include <iostream> // For print and user input
#include <limits> // For areyousure()
#include <regex> // For std::regex pattern (limit user to redeem only shown vector gifts) *This will increase the compile time*
#include <sstream> // For dataStream
#include <string> // For std::string data type
// #include <string_view> // For std::string_view (已移除)
#include <thread> // For std::thread
#include <vector> // For std::vector
#include <Windows.h> // Also for timing and clear
/* 此功能爲開發人員選項 請勿開啟 啟用時請移除最前方的兩個斜號 */
// #define DEBUG_ENABLED
/*********************************************************/
/* Thank you my hero Windows.h */
void SetConsoleColor(WORD color) // XD
{
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsoleOutput, color);
}
bool areYouSure(const std::string& clear, const std::string& type, const std::string check, const std::string customerID) // use for [3] edit customer , [4] enter customer view and [5] show transaction history
{
char select{ 0 };
std::string selectStr{};
if (check == "check")
{
std::cin.clear();
std::cin.ignore(10000, '\n');
}
do
{
if (type == "exit")
{
std::cout << "Are you sure you want to ";
SetConsoleColor(12);
std::cout << "leave";
SetConsoleColor(7);
std::cout << "? (Y/N): ";
}
else if(type == "delete")
{
SetConsoleColor(12);
std::cout << "Are you sure you want to delete " << customerID << " 's Data? (Y/N): ";
SetConsoleColor(7);
}
else
{
std::cout << "Are you sure you want to ";
SetConsoleColor(10);
std::cout << "continue";
SetConsoleColor(7);
std::cout << "? (Y/N): ";
}
std::getline(std::cin, selectStr);
if (clear == "clear")
system("cls");
if (selectStr.length() == 1 && (selectStr[0] == 'Y' || selectStr[0] == 'y' || selectStr[0] == 'N' || selectStr[0] == 'n'))
{
select = selectStr[0]; // Accept the valid input
}
else
{
select = 0; // Reset select to continue the loop
}
} while (select != 'Y' && select != 'y' && select != 'N' && select != 'n');
if (select == 'N' || select == 'n')
{
if (clear == "clear")
system("cls");
return false;
}
if (clear == "clear")
system("cls");
return true;
}
char getUserInput(bool dataLoaded)
{
char userInput{ 0 }; // 初始化 userInput 數值 (賦予 0 可以避免未定義的風險) 謹記此爲 char 並非 int
std::string inputBuffer{};
bool DEBUG_MODE = false;
#ifdef DEBUG_ENABLED
DEBUG_MODE = true;
#endif
do
{
SetConsoleColor(14);
std::cout << "Welcome! Customer" << std::endl; // 歡迎信息 (待更改)
SetConsoleColor(7);
std::cout << "*** Main Menu ***" << '\n';
std::cout << "[1] Load Starting Data" << '\n';
std::cout << "[2] Show Records" << '\n';
std::cout << "[3] Edit Customers" << '\n';
std::cout << "[4] Enter Customer View" << '\n';
std::cout << "[5] Show Transaction History" << '\n';
std::cout << "[6] Credits and Exit" << '\n';
if (DEBUG_MODE)
{
std::cout << std::endl;
std::cout << "*** Creative Mode ***" << '\n';
std::cout << "[7] Find Initial Data" << '\n';
std::cout << "[8] Delete Customer Data" << '\n';
std::cout << "[9] Edit CC Point" << '\n';
std::cout << "*****************" << '\n';
std::cout << "Option (0 - 9): ";
}
else
{
std::cout << "*****************" << '\n';
std::cout << "Option (1 - 6): ";
}
std::cin >> inputBuffer;
if (inputBuffer.length() == 1)
{
userInput = inputBuffer[0];
}
else
{
userInput = 0;
}
system("cls");
#ifndef DEBUG_ENABLED // Debug 模式時跳過檢查
if (!dataLoaded && userInput != '1' && userInput != '6') // 規定用戶必須輸入 [1] 或 [6],否則 [2] - [5] 將無法使用
{
system("cls");
std::cout << "Please Load the Starting Data First" << '\n';
Sleep(1500);
system("cls");
userInput = '0'; // 將 userInput 設為無效值以便繼續進行 loop
}
#endif
} while (userInput < '1' || (userInput > '6' && !(DEBUG_MODE && userInput <= '9')));
return userInput; // 返回用戶輸入
}
#ifndef DEBUG_ENABLED // Debug 模式時跳過進度條
void progressBar(int progress, int total) // 進度條函數
{
int barWidth = 50;
int filledWidth = static_cast<int>(barWidth * progress / total);
std::cout << '[';
for (int i{ 0 }; i < barWidth; ++i)
{
if (i < filledWidth) std::cout << '=';
else if (i == filledWidth) std::cout << '>';
else std::cout << ' ';
}
std::cout << ']' << ' ' << std::setw(3);
if (((progress * 100) / total) == 100)
SetConsoleColor(10);
else
SetConsoleColor(12);
std::cout << (progress * 100 / total) << "%\r";
std::cout.flush();
SetConsoleColor(7);
}
#endif
class GiftRecord // struct, 用來儲存 gift record 的數據 因應功課要求變成 class
{
public:
std::string giftID{};
std::string giftDescription{};
int giftPrice{};
int giftPointsRequired{};
// 避免 giftDescription 超過 100 char which is impossible so this is just to satisfy 要求 and wont be use XD
void setGiftDescription(const std::string& description)
{
if (description.length() > 100)
{
giftDescription = description.substr(0, 100);
}
else
{
giftDescription = description;
}
}
};
struct InitialCustomerRecord // 儲存第一次的數據 以便用戶在 [5] 時查閱數據
{
std::string initialCustomerID{};
int initialPointsBalance{};
};
struct CustomerRecord // struct, 用來儲存 customer record 的數據
{
std::string customerID{};
char customerRank{};
int pointsBalance{};
};
struct TransactionRecord // struct, 用來儲存 transaction record 的數據
{
std::string customerID{};
std::string type{};
std::string details{};
int pointsChange{};
double extraMoney{};
};
std::vector<GiftRecord> giftRecords; // 儲存禮品記錄的 vector
std::vector<InitialCustomerRecord> initialCustomerRecords; // 儲存初始禮品記錄數據的 vector
std::vector<CustomerRecord> customerRecords; // 儲存用戶記錄的 vector
std::vector<TransactionRecord> transactionHistory; // 儲存各種事件記錄的 vector
void storeGiftData() // 加載初始數據
{
#ifdef DEBUG_ENABLED
std::cout << "GiftData Loaded" << '\n';
#endif
/* Push back 是用來把數據放在/加入到 vector 的最後值 */
giftRecords.push_back({ "A01", "LG Internet TV", 3900, 19000 });
giftRecords.push_back({ "A02", "Pioneer Hifi Set", 2400, 11500 });
giftRecords.push_back({ "A03", "Sony DVD Player", 400, 2000 });
giftRecords.push_back({ "B01", "Healthy Air Fryer", 1500, 7300 });
giftRecords.push_back({ "B02", "Tefal Microwave Oven", 480, 2400 });
giftRecords.push_back({ "B03", "Famous Coffee Maker", 1800, 8800 });
giftRecords.push_back({ "B04", "Smart Rice Cooker", 600, 2900 });
giftRecords.push_back({ "B05", "TechCook Toaster Oven", 450, 2250 });
giftRecords.push_back({ "C01", "Wellcome $50 Coupon", 50, 250 });
giftRecords.push_back({ "C02", "Mannings $50 Coupon", 50, 250 });
giftRecords.push_back({ "C03", "Carol Restaurant $100 Coupon", 100, 500 });
giftRecords.push_back({ "C04", "Shell $200 Coupon", 200, 960 });
giftRecords.push_back({ "D01", "Clever Headset", 350, 1750 });
giftRecords.push_back({ "D02", "HP Optic Mouse", 250, 1250 });
giftRecords.push_back({ "D03", "Stylish Bluetooth Speaker", 800, 3900 });
}
void storeCustomerData() // 加載用戶數據
{
#ifdef DEBUG_ENABLED
std::cout << "CustomerData Loaded" << '\n';
#endif
customerRecords.push_back({ "Tommy2015", 'B', 8500 });
customerRecords.push_back({ "DavidChan", 'B', 22800 });
customerRecords.push_back({ "Luna123", 'B', 650 });
customerRecords.push_back({ "TigerMan", 'B', 14000 });
customerRecords.push_back({ "Max5678", 'S', 2580 });
customerRecords.push_back({ "Neo2000", 'S', 8000 });
customerRecords.push_back({ "CCTang", 'S', 33554 });
customerRecords.push_back({ "EchoWong", 'G', 8650 });
customerRecords.push_back({ "Rubychow", 'G', 28000 });
customerRecords.push_back({ "Ivy2023", 'G', 12340 });
}
void storeInitialCustomerData() // 加載用戶數據 由於直接複製 vector (vector.assign) 會出現錯誤
{
#ifdef DEBUG_ENABLED
std::cout << "initialCustomerData Loaded" << '\n';
#endif
initialCustomerRecords.push_back({ "Tommy2015", 8500 });
initialCustomerRecords.push_back({ "DavidChan", 22800 });
initialCustomerRecords.push_back({ "Luna123", 650 });
initialCustomerRecords.push_back({ "TigerMan", 14000 });
initialCustomerRecords.push_back({ "Max5678", 2580 });
initialCustomerRecords.push_back({ "Neo2000", 8000 });
initialCustomerRecords.push_back({ "CCTang", 33554 });
initialCustomerRecords.push_back({ "EchoWong", 8650 });
initialCustomerRecords.push_back({ "Rubychow", 28000 });
initialCustomerRecords.push_back({ "Ivy2023", 12340 });
}
void addCustomer(const std::string& id, char rank, int points) // 增加用戶數據的函數
{
#ifdef DEBUG_ENABLED
std::cout << "addCustomer Function Called" << '\n';
Sleep(2000);
#endif
customerRecords.push_back({ id, rank, points });
initialCustomerRecords.push_back({ id, points }); // 如果新增客戶時也新增到初始客戶數據 vector 否則初始數據將不會包含新用戶
}
void deleteCustomer(const std::string& id) // 刪除用戶數據的函數
{
#ifdef DEBUG_ENABLED
std::cout << "deleteCustomer Function Called" << '\n';
Sleep(3000);
#endif
for (auto it{ customerRecords.begin() }; it != customerRecords.end();) // 尋找數據
{
if (it->customerID == id)
{
customerRecords.erase(it);
break; // 如果找到則結束 loop
}
else
{
++it; // 找不到則繼續
}
}
}
bool findCustomer(const std::string id) // 尋找用戶並返回是否存在用戶 use for [3] edit customer , [4] enter customer view and [5] show transaction history
{
bool found{ false };
for (const auto& customer : customerRecords) // For each loop (god i love for each loop)
{
if (customer.customerID == id)
{
found = true;
break;
}
}
return found;
}
bool isLeapYear(int year) // 檢查是否 leap year 的函數
{
#ifdef DEBUG_ENABLED
std::cout << "isLeapYear Function Called" << '\n';
Sleep(3000);
#endif
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
return true;
}
else
{
return false;
}
}
bool isValidDate(int day, int month, int year, const std::tm& currentTime) // 檢查是否合乎格式的日期
{
#ifdef DEBUG_ENABLED
std::cout << "isValidDate Function Called" << '\n';
Sleep(3000);
#endif
int currentYear{ currentTime.tm_year + 1900 };
int currentMonth{ currentTime.tm_mon + 1 };
int currentDay{ currentTime.tm_mday };
// 檢查是否爲未來
if (year <= 1970)
{
return false;
}
if (year > currentYear || (year == currentYear && month > currentMonth) || (year == currentYear && month == currentMonth && day > currentDay))
{
#ifdef DEBUG_ENABLED
std::cout << "Future Check Failed" << '\n';
Sleep(3500);
#endif
return false;
}
// 檢查日期有效性
if (month < 1 || month > 12 || day < 1) // 檢查是否超出範圍
{
#ifdef DEBUG_ENABLED
std::cout << "Month Check Failed" << '\n';
Sleep(3500);
#endif
return false;
}
// 2月份的麻煩嘢
int daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeapYear(year))
{
daysInMonth[1] = 29;
}
if (day > daysInMonth[month - 1])
{
#ifdef DEBUG_ENABLED
std::cout << "daysInMonth Check Failed" << '\n';
Sleep(3500);
#endif
return false;
}
return true;
}
long daysDifference(const std::tm& creationTime, const std::tm& currentTime) // 計算與今天相差的日數
{
// Convert std::tm to std::time_t
std::time_t creationTimeEpoch = std::mktime(const_cast<std::tm*>(&creationTime));
std::time_t currentTimeEpoch = std::mktime(const_cast<std::tm*>(¤tTime));
// Removed cuz this wont happen since we added the exact same thing in valid date check
/* if (creationTimeEpoch == static_cast<std::time_t>(-1) || currentTimeEpoch == static_cast<std::time_t>(-1))
{
std::cerr << "mktime error: year before 1900 is not accepted." << std::endl;
Sleep(2000);
system("cls");
return -1;
} */
double diffSeconds = difftime(currentTimeEpoch, creationTimeEpoch);
long diffDays = static_cast<long>(diffSeconds / (60 * 60 * 24));
return diffDays;
}
long calculateDaysSinceCreation(int day, int month, int year) // 也是計算與今天相差的日數
{
// Initialize the creation time structure
std::tm creationTime = {};
creationTime.tm_year = year - 1900; // tm_year is years since 1900
creationTime.tm_mon = month - 1; // tm_mon is 0-11
creationTime.tm_mday = day;
// Get the current time as std::tm
std::time_t now = std::time(nullptr); //NULLPTR = 空指針
std::tm currentTime;
localtime_s(¤tTime, &now);
// Calculate the difference in days
return daysDifference(creationTime, currentTime);
}
char calculateRank(long days) // 根據日子返回用戶應該獲得的等級 Rank
{
#ifdef DEBUG_ENABLED
std::cout << "calculateRank Function Called" << '\n';
std::cout << "ReceivedData (Days): " << days << '\n';
Sleep(3000);
#endif
char userRank{ 0 };
if (days >= 365) // 大過或等於一年
{
userRank = 'G';
}
else if (days >= 180) // 大過或等於半年
{
userRank = 'S';
}
else // 其他情況 (小於半年)
{
userRank = 'B';
}
// #ifdef DEBUG_ENABLED
SetConsoleColor(14);
std::cout << "User Rank is: " << userRank << '\n';
SetConsoleColor(7);
Sleep(2000);
// #endif
return userRank;
}
void errorInput() // 如果用戶輸入非數值
{
#ifdef DEBUG_ENABLED
std::cout << "errorInput Function Called" << '\n';
Sleep(2000);
#endif
SetConsoleColor(12);
std::cout << "Invalid input. Please enter again: ";
SetConsoleColor(7);
std::cin.clear();
std::cin.ignore(1000000, '\n');
}
void updateCustomer(const std::string& id)
{
#ifdef DEBUG_ENABLED
std::cout << "updateCustomer Function Called" << '\n';
#endif
if (findCustomer(id)) // 如果 ID 和已有數據重複則刪除數據
{
bool sure{ areYouSure("dont clear", "delete", "dont check", id) };
if (sure)
{
deleteCustomer(id);
SetConsoleColor(10);
std::cout << "Customer " << id << " has been removed.\n";
SetConsoleColor(7);
Sleep(2000);
system("cls");
}
else
{
std::cout << "Action Canceled. Returning to menu." << '\n';
Sleep(2000);
system("cls");
}
}
else // 如果沒有則添加數據
{
std::cout << "Enter the points balance for the new customer: ";
int points{ 0 };
int retries{ 3 };
while ((!(std::cin >> points)) || points < 0) // 防止用戶輸入非數字或負數
{
retries--;
std::cout << "You have " << retries << " retries left." << '\n';
errorInput();
if (retries <= 0)
{
SetConsoleColor(12);
std::cout << "Run out of attempts. Returning to main menu." << '\n';
SetConsoleColor(7);
Sleep(2000);
system("cls");
return;
}
}
SetConsoleColor(10);
std::cout << "You have input a number: " << points << '\n';
SetConsoleColor(7);
std::cin.clear();
std::cin.ignore(1000000, '\n');
// Ask for date
Retries: // goto
std::cout << "Enter your account creation date";
SetConsoleColor(10);
std::cout << " (DD/MM/YYYY)";
SetConsoleColor(7);
std::cout << ": ";
std::string creationDate{};
std::getline(std::cin >> std::ws, creationDate); // Bro who use std::cin to a string and std::ws best thing
Sleep(500);
int day{ 0 };
int month{ 0 };
int year{ 0 };
// 獲取日期
std::istringstream dateStream(creationDate);
char slash{};
if (dateStream >> day >> slash >> month >> slash >> year && slash == '/') // 將 string 轉爲數字
{
std::time_t now = std::time(nullptr); // nullptr cool
std::tm localTime;
localtime_s(&localTime, &now); // Use localtime_s for thread safety
if (isValidDate(day, month, year, localTime))
{
long daysSinceCreation = calculateDaysSinceCreation(day, month, year);
if (daysSinceCreation >= 0) // Check that daysDifference didn't return an error (EXTRA SAFETY XD)
{
addCustomer(id, calculateRank(daysSinceCreation), points);
SetConsoleColor(10);
std::cout << "Customer " << id << " has been added.\n";
SetConsoleColor(7);
std::cout << "Action Completed. Returning to menu." << '\n';
Sleep(2000);
system("cls");
}
}
else
{
SetConsoleColor(12);
std::cout << "Invalid date! It is either you have input an invalid date or it is before or equals to 1970." << '\n';
SetConsoleColor(7);
retries--;
if (retries <= 0)
{
SetConsoleColor(12);
std::cout << "Run out of attempts. Returning to main menu." << '\n';
SetConsoleColor(7);
Sleep(2000);
system("cls");
return;
}
std::cout << "You have " << retries << " retries left." << '\n';
Sleep(2000);
goto Retries; // 使用 goto (not recommended but it is so convenient)
}
}
else
{
SetConsoleColor(12);
std::cout << "Invalid input for date format. Please retry." << '\n';
SetConsoleColor(7);
retries--;
if (retries <= 0)
{
SetConsoleColor(12);
std::cout << "Run out of attempts. Returning to main menu." << '\n';
SetConsoleColor(7);
Sleep(2000);
system("cls");
return;
}
std::cout << "You have " << retries << " retries left." << '\n';
Sleep(2000);
goto Retries; // 使用 goto (not recommended but it is so convenient)
}
}
}
void printGiftRecord() // print gift records
{
SetConsoleColor(10);
std::cout << "***** Gift Records *****" << '\n';
SetConsoleColor(7);
std::cout << std::setfill('=') << std::setw(72) << "=" << std::setfill(' ') << std::endl;
std::cout << std::left; // 設置左對齊 (其實無用)
std::cout << std::setw(10) << "Gift ID"
<< std::setw(30) << "Gift Description"
<< std::setw(15) << "Price (HKD)"
<< std::setw(30) << "Points Required" << '\n';
std::cout << std::setfill('=') << std::setw(72) << '=' << std::setfill(' ') << std::endl; // 打印分隔線
for (auto& giftRecord : giftRecords) // 打印所有禮品記錄 using for each loop
{
std::cout << std::setw(10) << giftRecord.giftID
<< std::setw(30) << giftRecord.giftDescription
<< std::setw(18) << giftRecord.giftPrice
<< std::setw(22) << giftRecord.giftPointsRequired << std::left << '\n';
}
std::cout << std::setfill('=') << std::setw(72) << '=' << std::setfill(' ') << std::endl << '\n';
}
void printCustomerRecord() // print customer records
{
SetConsoleColor(10);
std::cout << "***** Customer Records *****" << '\n';
std::cout << "Record has been printed by the order of number > capital > smaller" << '\n';
SetConsoleColor(7);
std::cout << std::setfill('=') << std::setw(90) << '=' << std::setfill(' ') << std::endl;
std::cout << std::setw(50) << "CustomerID" << '\t'
<< std::setw(15) << "Rank" << '\t'
<< std::setw(20) << "Points Balance" << '\n';
std::cout << std::setfill('=') << std::setw(90) << '=' << std::setfill(' ') << std::endl;
// Sorting customerRecords by customerID
std::sort(customerRecords.begin(), customerRecords.end(), [](const CustomerRecord& a, const CustomerRecord& b)
{
return a.customerID < b.customerID;
});
for (auto& customerRecord : customerRecords) // 打印所有顧客記錄 using for each loop
{
std::cout << std::setw(50) << customerRecord.customerID << '\t'
<< std::setw(15) << customerRecord.customerRank << '\t'
<< std::setw(20) << ((customerRecord.pointsBalance < 0) ? '\0' : customerRecord.pointsBalance) << '\n';
}
std::cout << std::setfill('=') << std::setw(90) << '=' << std::setfill(' ') << '\n';
std::cout << std::endl;
}
void printRecords() // Cancer code don't bother (Not cancer anymore :D)
{
system("cls");
#ifdef DEBUG_ENABLED
std::cout << "printRecords Function Called" << '\n';
#endif
std::cout << std::endl;
printGiftRecord();
printCustomerRecord();
}
auto findUserData(const std::string& customerID, const std::string& dataType, int messageOption) // 尋找用戶數據
{
#ifdef DEBUG_ENABLED
std::cout << "findUserData Function Called" << '\n';
Sleep(2000);
#endif
for (auto& customerRecord : customerRecords)
{
if (findCustomer(customerID) == 1 && customerRecord.customerID == customerID)
{
if (dataType == "point")
{
if (messageOption == 1)
{
SetConsoleColor(14);
std::cout << "Customer " << customerID << " currently have " << ((customerRecord.pointsBalance < 0) ? '\0' : customerRecord.pointsBalance) << " points." << '\n';
SetConsoleColor(7);
}
return customerRecord.pointsBalance;
}
if (dataType == "rank")
{
if (messageOption == 1)
{
SetConsoleColor(14);
std::cout << "Customer " << customerID << " currently is at Rank " << customerRecord.customerRank << '\n';
SetConsoleColor(7);
if (customerRecord.customerRank == 'G')
{
SetConsoleColor(10);
std::cout << "You can have 10% off discount for redeeming gifts!" << '\n';
SetConsoleColor(7);
}
else if (customerRecord.customerRank == 'S')
{
SetConsoleColor(10);
std::cout << "You can have 5% off discount for redeeming gifts!" << '\n';
SetConsoleColor(7);
}
else
{
SetConsoleColor(12);
std::cout << "You don't have any discount on your rank! Level Up to receive extra advantages!" << '\n';
SetConsoleColor(7);
}
std::cout << std::endl;
}
return static_cast<int>(customerRecord.customerRank); // 將 char 變爲 int 否則 auto data type 將無法運行
}
}
}
if (messageOption == 1)
{
SetConsoleColor(10);
std::cout << "Action Completed." << '\n';
SetConsoleColor(7);
std::cout << "Backing main menu." << '\n';
}
Sleep(2000);
return 0;
}
auto findInitialData(const std::string& customerID)
{
#ifdef DEBUG_ENABLED
std::cout << "Function findInitialData Called" << '\n';
#endif
for (auto& initialCustomerRecord : initialCustomerRecords)
{
if (initialCustomerRecord.initialCustomerID == customerID)
{
return initialCustomerRecord.initialPointsBalance;
}
}
SetConsoleColor(12);
std::cout << "Customer " << customerID << " not found in initial records." << '\n';
SetConsoleColor(7);
return 0;
}
std::vector<GiftRecord> getAvailableGiftsForRedemption(const std::string& customerID, double discount = 1.0)
{
// 過濾並排序禮品記錄
std::vector<GiftRecord> availableGifts;
int customerPoints{ findUserData(customerID, "point", 0) };
// 先將所有禮品記錄添加到臨時vector中
for (const auto& gift : giftRecords)
{
if (gift.giftPointsRequired * discount <= customerPoints)
{
availableGifts.push_back(gift);
}
}
// 根據所需積分對可用禮品進行排序
std::sort(availableGifts.begin(), availableGifts.end(), [](const GiftRecord& a, const GiftRecord& b)
{
return a.giftPointsRequired < b.giftPointsRequired;
});
return availableGifts;
}
void earnCCPoint(const std::string& customerID, double moneyAmount)
{
#ifdef DEBUG_ENABLED
std::cout << "earnCCPoint Function Called" << '\n';
Sleep(2000);
#endif
int newPoints = static_cast<int>(floor(moneyAmount / 250));
double extraMoney{};
for (auto& extraMoneys : transactionHistory)
{
if (findCustomer(customerID) == 1 && extraMoneys.customerID == customerID)
{
extraMoney = extraMoneys.extraMoney;
}
}
for (auto& ccPoints : customerRecords)
{
if (findCustomer(customerID) == 1 && ccPoints.customerID == customerID)
{
if (ccPoints.pointsBalance < 0)
ccPoints.pointsBalance = 0;
ccPoints.pointsBalance += newPoints;
// #ifdef DEBUG_ENABLED
SetConsoleColor(10);
std::cout << "Your Current Points is: " << ccPoints.pointsBalance << '\n';
// #endif
std::cout << "Customer " << customerID << " ccPoints has been updated!" << '\n';
SetConsoleColor(7);
Sleep(1500);
transactionHistory.push_back({ customerID, "Earn CC Points", std::to_string(newPoints), newPoints, extraMoney });
return;
}
}
std::cout << "Error 420: User Not Found." << '\n'; // Impossible since we already have a findCustomer function
}
void reduceCCPoint(const std::string& customerID, int newPoints) // 減分 兌換禮品時使用
{
for (auto& ccPoints : customerRecords)
{
if (findCustomer(customerID) == 1 && ccPoints.customerID == customerID)
{
ccPoints.pointsBalance -= newPoints;
#ifdef DEBUG_ENABLED
std::cout << "Current Points: " << ccPoints.pointsBalance << '\n';
#endif
SetConsoleColor(10);
std::cout << "Customer " << customerID << " ccPoints has been updated!" << '\n';
SetConsoleColor(7);
}
}
return;
}
void printByCategory(std::string username, char giftCategory, double discount);
void redeemGift(CustomerRecord& customer, double discount, char allowedCategory, std::string customerID) // 兌換禮品的函數
{
int attempts{ 3 };
const int maxAttempts{ 0 };
bool success{ 0 };
while (attempts + 2 > maxAttempts && !success)
{
if (attempts <= maxAttempts)
{
SetConsoleColor(12);
std::cout << "Failed to redeem gift after " << maxAttempts << " attempts. Returning to customer menu." << '\n';
SetConsoleColor(7);
Sleep(2500);
break;
}
std::cout << "Enter the giftID that you want to redeem ";