-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.cpp
825 lines (804 loc) · 22.3 KB
/
service.cpp
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "card_service.h"
#include "bill_service.h"
#include "global_var.h"
#include "tool.h"
#include "class.h"
#include "service.h"
#include "stat_service.h"
using namespace std;
void CreatCard()
{
string name, password;
double balance;
cout << "========创建卡========" << endl;
// 接受卡号
while (true)
{
cout << "请设置卡号(" << CARD_NAME_MIN << '~' << CARD_NAME_MAX << "位): ";
if (InputCheck_String(name, CARD_NAME_MIN, CARD_NAME_MAX))
{
if (!IsExist(name))
break;
else
cout << "[!] 卡号已被占用" << endl;
}
else
cout << "[!] 长度非法" << endl;
}
// 接受密码
while (true)
{
cout << "请设置密码(" << CARD_PWD_MIN << '~' << CARD_PWD_MAX << "位): ";
if (InputCheck_String(password, CARD_PWD_MIN, CARD_PWD_MAX))
break;
else
cout << "[!] 长度非法" << endl;
}
// 接受首充
while (true)
{
cout << "初始充值(首充20元起步): ¥";
if (InputCheck_Double(balance, 20.0))
break;
else
cout << "[!] 数据非法" << endl;
}
Card new_card = Card(name, password, balance);
Clear();
cout << "========创建成功========" << endl;
new_card.Print();
SaveCard(name, new_card); // 保存卡到内存
Save_Card(); // 保存卡到文件
CreatBill(name, balance); // 保存账单到内存
Save_Bill(); // 保存账单到文件
StatUpdate(0, balance);
Save_Stat();
Pause();
}
void QueryCard(bool is_admin)
{
cout << "========查询卡========" << endl;
if (is_admin)
cout << "[!] 当前为管理员查询模式,密码将会显示" << endl;
string name;
while (true)
{
cout << "输入待查卡号(可输入q终止查询): ";
cin >> name;
if (name == "q" || name.empty())
break;
Clear();
cout << "========查询成功========" << endl;
if (IsExist(name))
{
PrintCard(name, is_admin);
break;
}
else
{
PrintSimilarCard(name);
}
}
Pause();
}
void Price()
{
cout << "========价格========" << endl;
GetPrice().Print();
Pause();
}
void Login()
{
cout << "========上机========" << endl;
string name, password;
cout << "请输入卡号: ";
cin >> name;
cout << "请输入密码: ";
cin >> password;
int ret = CheckLogin(name, password);
if (ret == 0)
{
int type;
// 第一层循环,包夜不成功重选
while (true)
{
cout << "0. 按量计费" << endl
<< "1. 包夜" << endl
<< "2. 包天" << endl;
// 第二层循环,输入不成功重输
while (true)
{
cout << "选择计费方式: ";
if (InputCheck_Int(type, 0, 2))
break;
else
cout << "[!] 输入非法" << endl;
}
Clear();
if (type == 0)
{
cout << "========上机成功========" << endl
<< "上机时间: " << Date2str(time(nullptr)) << endl;
CreatBill(name, type);
break;
}
else if (type == 1)
{
// 获取夜晚范围
pair<int, int> range = GetNightRange();
// 获取time_t格式当前时间
time_t now_time_t = time(nullptr);
// 转换为tm格式当前时间
tm *now_time = localtime(&now_time_t);
if (now_time->tm_hour >= range.first ||
now_time->tm_hour <= range.second)
{
cout << "========上机成功========" << endl
<< "上机时间: " << Date2str(time(nullptr)) << endl;
CreatBill(name, type);
break;
}
else
{
cout << "========上机失败========" << endl
<< "[!] 当前不在包夜时间段内,包夜时间段: " << range.first << '~' << range.second << endl;
}
}
else if (type == 2)
{
Clear();
cout << "========上机成功========" << endl
<< "上机时间: " << Date2str(time(nullptr)) << endl;
CreatBill(name, type);
break;
}
}
Save_Card();
Save_Bill();
}
else if (ret == 1)
{
Clear();
cout << "========上机失败========" << endl
<< "[!] 账号或密码错误" << endl;
}
else if (ret == 2)
{
Clear();
cout << "========上机失败========" << endl
<< "[!] 该账号已经上机" << endl;
}
else if (ret == 3)
{
Clear();
cout << "========上机失败========" << endl
<< "[!] 该卡已注销" << endl;
}
else if (ret == 4)
{
Clear();
cout << "========上机失败========" << endl
<< "[!] 该卡有未结清账单" << endl;
}
Pause();
}
void Logoff()
{
cout << "========下机========" << endl;
string name, password;
cout << "请输入卡号: ";
cin >> name;
cout << "请输入密码: ";
cin >> password;
int ret = CheckLogoff(name, password);
Clear();
if (ret == 0)
{
cout << "========下机成功========" << endl
<< "下机时间: " << Date2str(time(nullptr)) << endl;
auto bill = GetIncompleteBill(name);
if (Charge(name, bill.first))
{
SetPaid(name);
CompleteBill(name);
StatUpdate(4, bill.first, bill.second);
StatDataSave();
cout << "缴费成功!" << endl;
printf("本次消费: ¥%.2lf\n卡余额: ¥%.2lf\n", bill.first, GetBalance(name));
}
else
{
cout << "[!] 缴费失败,余额不足!" << endl;
printf("本次消费: ¥%.2lf\n卡余额: ¥%.2lf\n", bill.first, GetBalance(name));
}
Save_Card();
Save_Bill();
}
else if (ret == 1)
cout << "========下机失败========" << endl
<< "[!] 账号或密码错误" << endl;
else if (ret == 2)
cout << "========下机失败========" << endl
<< "[!] 该账号已经下机" << endl;
else if (ret == 3)
cout << "========下机失败========" << endl
<< "[!] 该卡已注销" << endl;
Pause();
}
void Deposit()
{
cout << "========充值========" << endl;
string name;
cout << "请输入卡号: ";
cin >> name;
if (IsExist(name))
{
if (GetStatus(name) == 0 || GetStatus(name) == 1)
{
double money;
while (true)
{
cout << "当前余额: ¥" << fixed << setprecision(2) << GetBalance(name) << endl
<< "请输入充值金额: ¥";
if (InputCheck_Double(money, 0.01))
break;
else
cout << "[!] 数据非法" << endl;
}
Clear();
cout << "========充值成功========" << endl
<< left << setw(16) << "充前余额: " << "¥" << fixed << setprecision(2) << GetBalance(name) << endl;
CreatBill(name, money);
UpdateBalance(name, money);
cout << left << setw(16) << "充值金额: " << "¥" << fixed << setprecision(2) << money << endl;
if (!IsSettled(name))
{
auto bill = GetIncompleteBill(name);
if (Charge(name, bill.first))
{
SetPaid(name);
CompleteBill(name);
StatUpdate(4, bill.first, bill.second);
StatDataSave();
cout << left << setw(16) << "未结账单扣除: " << "¥" << fixed << setprecision(2) << bill.first << endl;
SetPaid(name);
Save_Card();
}
else
{
cout << "[!] 当前仍有¥" << fixed << setprecision(2) << bill.first << "未结账单,该卡仍不可用" << endl;
}
}
cout << "------------------------" << endl
<< left << setw(16) << "当前余额: " << "¥" << fixed << setprecision(2) << GetBalance(name) << endl;
Save_Bill();
StatUpdate(2, money);
Save_Stat();
}
else if (GetStatus(name) == 2)
{
Clear();
cout << "========充值失败========" << endl
<< "[!] 该卡已注销" << endl;
}
}
else
{
Clear();
cout << "========充值失败========" << endl
<< "[!] 该卡不存在" << endl;
}
Pause();
}
void Refund()
{
cout << "========退费========" << endl;
string name, password;
cout << "请输入卡号: ";
cin >> name;
cout << "请输入密码: ";
cin >> password;
if (IsExist(name))
{
if (!IsSettled(name))
{
Clear();
cout << "========退费失败========" << endl
<< "[!] 账单结清前无法退费" << endl;
}
else if (PasswordCheck(name, password))
{
double money;
cout << "当前余额: " << "¥" << fixed << setprecision(2) << GetBalance(name) << endl;
while (true)
{
cout << "请输入退费金额: ¥";
if (InputCheck_Double(money, 0.01))
break;
else
cout << "[!] 数据非法" << endl;
}
if (money > GetBalance(name))
{
Clear();
cout << "========退费失败========" << endl
<< "[!] 退费不能超过余额" << endl;
}
else
{
Clear();
cout << "========退费成功========" << endl
<< left << setw(16) << "历史余额: " << "¥" << fixed << setprecision(2) << GetBalance(name) << endl
<< left << setw(16) << "退费: " << "¥" << fixed << setprecision(2) << money << endl;
CreatBill(name, -money);
UpdateBalance(name, -money);
cout << "------------------------" << endl
<< left << setw(16) << "当前余额: " << "¥" << fixed << setprecision(2) << GetBalance(name) << endl;
Save_Bill();
StatUpdate(3, money);
Save_Stat();
}
}
else
{
Clear();
cout << "========退费失败========" << endl
<< "[!] 账号或密码错误" << endl;
}
}
else
{
Clear();
cout << "========退费失败========" << endl
<< "[!] 该卡不存在" << endl;
}
Pause();
}
void QueryBill()
{
cout << "========账单========" << endl;
string name;
cout << "请输入卡号: ";
cin >> name;
if (IsExist(name))
{
int select;
cout << "1. 按年/月/日统计" << endl
<< "2. 按时间段统计" << endl
<< "0. 返回" << endl;
while (true)
{
cout << "请选择操作(0~2): ";
if (InputCheck_Int(select, 0, 2))
break;
else
cout << "输入非法" << endl;
}
if (select == 0)
{
return;
}
else if (select == 1)
{
int year = 0, month = 0, day = 0;
while (true)
{
cout << "选择统计范围,可用0表示缺省,缺省写法例如\"2022 0 0\"和\"2022 3 0\"" << endl
<< "请输入(YYYY MM DD): ";
if (cin >> year >> month >> day && year >= 1900 && year <= 2100 &&
month >= 0 && month <= 12 && day >= 0 && day <= 31)
{
break;
}
else
{
cin.clear();
cin.ignore(1024, '\n');
cout << "输入非法" << endl;
}
}
Clear();
cout << "========查询成功========" << endl;
PrintBill(name, year, month, day);
}
else if (select == 2)
{
YearMonthDay start, end;
int year, month, day;
while (true)
{
cout << "请输入开始日期(YYYY MM DD): ";
if (cin >> year >> month >> day && year >= 1900 && year <= 2100 &&
month >= 1 && month <= 12 && day >= 1 && day <= 31)
{
break;
}
else
{
cin.clear();
cin.ignore(1024, '\n');
cout << "输入非法" << endl;
}
}
start = {year, month, day};
while (true)
{
cout << "请输入截止日期(YYYY MM DD): ";
if (cin >> year >> month >> day && year >= 1900 && year <= 2100 &&
month >= 1 && month <= 12 && day >= 1 && day <= 31)
{
break;
}
else
{
cin.clear();
cin.ignore(1024, '\n');
cout << "输入非法" << endl;
}
}
end = {year, month, day};
Clear();
cout << "========查询成功========" << endl;
PrintBill(name, start, end);
}
}
else
{
Clear();
cout << "========查询失败========" << endl
<< "[!] 该卡不存在" << endl;
}
Pause();
}
void Delete()
{
cout << "========注销========" << endl;
string name, password;
cout << "请输入卡号: ";
cin >> name;
cout << "请输入密码: ";
cin >> password;
if (IsExist(name))
{
if (GetStatus(name) == 1)
{
Clear();
cout << "========注销失败========" << endl
<< "[!] 上机时无法注销" << endl;
}
else if (GetStatus(name) == 2)
{
Clear();
cout << "========注销失败========" << endl
<< "[!] 此卡已经注销" << endl;
}
else if (!IsSettled(name))
{
Clear();
cout << "========注销失败========" << endl
<< "[!] 账单结清前无法注销" << endl;
}
else if (PasswordCheck(name, password))
{
cout << "[!] 注销后将无法使用该卡,是否确认注销?(Y/N): ";
bool choice;
while (true)
{
if (InputCheck_YN(choice))
break;
else
cout << "输入非法" << endl;
}
if (choice)
{
Clear();
double refund = GetBalance(name);
cout << "========注销成功========" << endl
<< "余额退费: ¥" << fixed << setprecision(2) << refund << endl;
UpdateBalance(name, -refund);
CreatBill(name, -refund);
DeleteCard(name);
Save_Card();
Save_Bill();
StatUpdate(1, refund);
}
else
{
Clear();
cout << "========注销失败========" << endl
<< "[!] 用户取消操作" << endl;
}
}
else
{
Clear();
cout << "========注销失败========" << endl
<< "[!] 账号或密码错误" << endl;
}
}
else
{
Clear();
cout << "========注销失败========" << endl
<< "[!] 该卡不存在" << endl;
}
Pause();
}
void Password()
{
string name, password;
cout << "========密码重设========" << endl;
cout << "请输入卡号: ";
cin >> name;
cout << "请输入密码: ";
cin >> password;
if (IsExist(name) && PasswordCheck(name, password))
{
string new_password;
while (true)
{
cout << "请输入新密码(" << CARD_PWD_MIN << '~' << CARD_PWD_MAX << "位): ";
if (InputCheck_String(new_password, CARD_PWD_MIN, CARD_PWD_MAX))
break;
else
cout << "[!] 长度非法" << endl;
}
SetPassword(name, new_password);
Clear();
cout << "========重设成功========" << endl
<< "卡号: " << name << endl
<< "密码: " << new_password << endl;
Save_Card();
}
else
{
Clear();
cout << "========重设失败========" << endl
<< "[!] 账号或密码错误" << endl;
}
Pause();
}
void Load_All()
{
bool load;
cout << "========初始化========" << endl;
while (true)
{
cout << "是否读取数据?(Y/N): ";
if (InputCheck_YN(load))
{
if (load)
{
PriceDataLoad();
cout << "成功加载价格信息" << endl;
cout << "成功加载卡信息: " << CardDataLoad() << "条" << endl;
cout << "成功加载账单信息: " << BillDataLoad() << "条" << endl;
cout << "成功加载统计信息: " << StatDataLoad() << "条" << endl;
Pause();
}
break;
}
else
cout << "[!] 输入非法" << endl;
}
}
void Save_All()
{
cout << "========保存========" << endl;
PriceDataSave();
cout << "成功保存价格信息" << endl;
cout << "成功保存卡信息: " << CardDataSave() << "条" << endl;
cout << "成功保存账单信息: " << BillDataSave() << "条" << endl;
cout << "成功保存统计信息: " << StatDataSave() << "条" << endl;
}
void Save_Card()
{
CardDataSave();
}
void Save_Bill()
{
BillDataSave();
}
void Save_Price()
{
PriceDataSave();
}
void Save_Stat()
{
StatDataSave();
}
bool Admin_Login(const string &name, const string &password)
{
// BEGIN 调试用代码,注意删除
// return true;
// END
ifstream AdminData;
AdminData.open("./AdminData.txt", ios::in);
string admin_name, admin_password;
AdminData >> admin_name >> admin_password;
AdminData.close();
if (name == admin_name && password == admin_password)
return true;
else
return false;
}
void Admin_Price()
{
cout << "========修改价格========" << endl;
cout << "当前价格: " << endl;
GetPrice().Print();
cout << "------------------------" << endl;
bool choice;
while (true)
{
cout << "是否修改价格?(Y/N): ";
if (InputCheck_YN(choice))
break;
else
cout << "[!] 输入非法" << endl;
}
if (!choice)
return;
cout << "新价格: " << endl;
double P_HalfHour, P_Night, P_Day;
int NightStart, NightEnd;
cout << "半小时计费: ¥";
while (true)
{
if (InputCheck_Double(P_HalfHour, 0))
break;
else
cout << "数据非法" << endl;
}
cout << "包夜计费: ¥";
while (true)
{
if (InputCheck_Double(P_Night, 0))
break;
else
cout << "数据非法" << endl;
}
cout << "包天计费: ¥";
while (true)
{
if (InputCheck_Double(P_Day, 0))
break;
else
cout << "数据非法" << endl;
}
cout << "包夜时间开始(0~24): ";
while (true)
{
if (InputCheck_Int(NightStart, 0, 24))
break;
else
cout << "数据非法" << endl;
}
cout << "包夜时间结束(0~24): ";
while (true)
{
if (InputCheck_Int(NightEnd, 0, 24))
break;
else
cout << "数据非法" << endl;
}
Clear();
cout << "========修改成功========" << endl
<< "新价格: " << endl;
ChangePrice(P_HalfHour, P_Night, P_Day, NightStart, NightEnd).Print();
Save_Price();
Pause();
}
void Admin_Stat()
{
cout << "========收益统计========" << endl;
int select;
cout << "1. 按年/月/日统计" << endl
<< "2. 按时间段统计" << endl
<< "0. 返回" << endl;
while (true)
{
cout << "请选择操作(0~2): ";
if (InputCheck_Int(select, 0, 2))
break;
else
cout << "输入非法" << endl;
}
if (select == 0)
{
return;
}
else if (select == 1)
{
int year = 0, month = 0, day = 0;
while (true)
{
cout << "选择统计范围,可用0表示缺省,缺省写法例如\"2022 0 0\"和\"2022 3 0\"" << endl
<< "请输入(YYYY MM DD): ";
if (cin >> year >> month >> day && year >= 1900 && year <= 2100 &&
month >= 0 && month <= 12 && day >= 0 && day <= 31)
{
break;
}
else
{
cin.clear();
cin.ignore(1024, '\n');
cout << "输入非法" << endl;
}
}
Clear();
PrintStat(year, month, day);
}
else if (select == 2)
{
YearMonthDay start, end;
int year, month, day;
while (true)
{
cout << "请输入开始日期(YYYY MM DD): ";
if (cin >> year >> month >> day && year >= 1900 && year <= 2100 &&
month >= 1 && month <= 12 && day >= 1 && day <= 31)
{
break;
}
else
{
cin.clear();
cin.ignore(1024, '\n');
cout << "输入非法" << endl;
}
}
start = {year, month, day};
while (true)
{
cout << "请输入截止日期(YYYY MM DD): ";
if (cin >> year >> month >> day && year >= 1900 && year <= 2100 &&
month >= 1 && month <= 12 && day >= 1 && day <= 31)
{
break;
}
else
{
cin.clear();
cin.ignore(1024, '\n');
cout << "输入非法" << endl;
}
}
end = {year, month, day};
Clear();
PrintStat(start, end);
}
Pause();
}
void Admin_Card()
{
cout << "========查询卡========" << endl;
cout << "1. 单卡查询" << endl
<< "2. 输出所有卡" << endl
<< "0. 返回" << endl;
int choice;
while (true)
{
cout << "选择查询模式(0~2): ";
if (InputCheck_Int(choice, 0, 2))
break;
else
cout << "输入非法" << endl;
}
Clear();
if (choice == 1)
{
QueryCard(true);
}
else if (choice == 2)
{
cout << "========查询成功========" << endl;
PrintAllCard();
Pause();
}
}