-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystem.cpp
More file actions
988 lines (940 loc) · 26.5 KB
/
System.cpp
File metadata and controls
988 lines (940 loc) · 26.5 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
#include "System.h"
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include "Account.h"
#include "SavingsAccount.h"
#include "CreditAccount.h"
#include "Date.h"
#include <stdexcept>
#include <conio.h>
#include "User.h"
#include <utility>
#include "mainwindow.h"
#include <QTextCodec>
#include <QApplication>
#include "logindialog.h"
#include "QMessageBox"
using namespace std;
System::System()
{
cout << "System initing......" << endl;
DataBaseFile = "DataBase.txt";
CurrentUser = nullptr;
Today = Date(2019,1,1);
logmgr = new LogMaster(ui);
ReadFile();
}
System::~System()
{
string info ;
try
{
CurrentUser = nullptr;
info = SaveData();
}
catch (const exception &e)
{
logmgr -> AddLog(nullptr,"系统",(string("数据保存失败:") + e.what()).c_str(),"warning");
//QMessageBox::warning(nullptr,"数据保存失败",e.what());
}
logmgr -> AddLog(nullptr,"系统",info.c_str(),"information");
//QMessageBox::information(nullptr,"数据保存",info.c_str());
}
System::System(Ui::MainWindow *ui,LogMaster * logmgr)
{
cout << "System initing......" << endl;
DataBaseFile = "DataBase.txt";
CurrentUser = nullptr;
Today = Date(2019,1,1);
this -> ui = ui;
this -> logmgr = logmgr;
string info;
info = ReadFile();
logmgr -> AddLog(nullptr,"系统",info.c_str(),"information",false,true);
//QMessageBox::information(nullptr,"读档",info.c_str());
}
void System::setToday(Date day)
{
Today = day;
}
void System::setToday(int year,int month,int day)
{
Date days(year,month,day);
Today = days;
}
Date System::getToday()
{
return Today;
}
bool System::FindUser(string username, User *&usr)
{
for (map<string, User *>::iterator it = userData.begin(); it != userData.end(); it++)
{
if (it->second->getUsername() == username)
{
usr = it->second;
return true;
}
}
return false;
}
string System::InputPasswd()
{
char c;
string str;
for (int i = 0; (c = _getch()) != 13; )
{
if (c != 8)
{
cout << "*";
str += c;
i++;
}
else
{
if(i != 0)
{
str.erase(i - 1);
i--;
cout << "\b \b";
}
}
}
cout << endl;
return str;
}
//登录
void System::Login(string username, string password)
{
User *usr;
if (FindUser(username, usr))
{
if (usr->LoginCheck(username, password))
{
CurrentUser = usr;
}
else
throw runtime_error("用户名或密码错误");
}
else
throw runtime_error("用户名不存在");
}
void System::CMDLogin()
{
string username, password;
cout <<"******************************"<<endl;
cout << "** 用户名:";
cin >> username;
cout << "** 密码:";
password = InputPasswd();
try
{
Login(username, password);
if(CurrentUser != nullptr)
{
cout << "** " << CurrentUser->getUsername() << " 已登录." << endl;
cout <<"******************************"<<endl;
CurrentUser -> ShowWarningInfo();
}
}
catch (const std::exception &e)
{
std::cerr <<"【登陆错误】: "<< e.what() << '\n';
CMDLogin();
}
}
//注册
void System::Register(string username, string password)
{
User *user = new User(username, password,logmgr);
if(!AddUser(username, user))
throw runtime_error("用户名已被使用,注册失败");
}
//注册账户 格式: date(2019 1 1) type(Credit/Saving) id
void System::CMDRegister()
{
string username;
string password1;
string password2;
cout <<"******************************"<<endl;
cout << "** 用户名:";
cin >> username;
int t = 0;
do
{
if (t > 0)
cout << "两次密码不匹配,请重新输入" << endl;
cout << "** 密码:";
password1 = InputPasswd();
cout << "** 确认密码:";
password2 = InputPasswd();
t++;
} while (password1 != password2);
try
{
Register(username, password1);
cout <<"** "<< username <<" 注册成功"<<endl;
cout <<"******************************"<<endl;
}
catch (const std::exception &e)
{
std::cerr <<"【注册错误】: "<< e.what() << '\n';
CMDRegister();
}
}
//添加账户
bool System::AddUser(string username, User *user)
{
if (userData.count(username) == 0)
{
userData.insert(make_pair(username, user));
return true;
}
return false;
}
//创建帐户
void System::CreateAccount(Date date, string id, double rate, double credit, double annualFee)
{
Account *acc;
if(CurrentUser == nullptr)
throw runtime_error("用户未登录,无法创建帐户");
if (!FindAccount(id, acc))
CurrentUser->CreatAccount(date, id, rate, credit, annualFee);
else
throw runtime_error("用户名已被占用,添加用户失败。");
}
void System::CMDCreateAccount()
{
Date date;
string type;
string id;
double rate;
double credits;
double annualFee;
cin >> date >> type >> id >> rate;
if (type == "Credit")
cin >> credits >> annualFee;
else
{
credits = -1;
annualFee = -1;
}
try
{
CreateAccount(date, id, rate, credits, annualFee);
cout << "** 账户创建完成" << endl;
}
catch (const std::exception &e)
{
std::cerr << "【账户创建错误】:"<< e.what() << '\n';
}
}
//全用户查找Account
bool System::FindAccount(string id, Account *&acc)
{
for (map<string, User *>::iterator it = userData.begin(); it != userData.end(); it++)
{
if (it->second->FindAccount(id, acc))
return true;
else
return false;
}
return false;
}
void System::Logout()
{
if (CurrentUser != nullptr)
{
CurrentUser->AccountLogout();
CurrentUser = nullptr;
}
cout << "** 用户登出成功" << endl;
}
//保存数据
string System::SaveData()
{
ofstream fout;
fout.open(DataBaseFile, ios::out);
if (!fout)
throw runtime_error("文件打开失败");
for (map<string, User *>::iterator itu = userData.begin(); itu != userData.end(); itu++)
{
fout << "UserStart" << endl;
fout << "UserInfoStart" << endl;
fout << "Username= " << itu->second->getUsername() << endl;
fout << "Password= " << itu->second->getPassword() << endl;
fout << "UserInfoEnd" << endl;
fout << "AccountStart" << endl;//账户信息起始标志
for (map<string, Account *>::iterator it = itu->second->userAccount.begin(); it != itu->second->userAccount.end(); it++)
{
if (it->second->getAccountType() == TypeSavings)
{
fout << "SavingsAccountStart" << endl; //储蓄账户数据起始标志
fout << "ID= " << it->second->getId() << endl;
fout << "Balance= " << it->second->getBalance() << endl;
fout << "Rate= " << it->second->getRate() << endl;
fout << "LastRecordedDate= " << it->second->getLastRecordedDate().TransferToString() << endl;
fout << "AccumulationLastDate= " << it->second->accumulation.lastDate.TransferToString() << endl;
fout << "AccumulationValue= " << it->second->accumulation.value << endl;
fout << "AccumulationSum= " << it->second->accumulation.sum << endl;
fout << "LogInfoStart" << endl;
int year = Today.getYear();
int month = Today.getMonth();
if(month <= 3)
{
year-=1;
month = 12-3+month;
}
int sum = 0;
bool LowerBoundArrived = false;
for(multimap<Date,LogInfo> ::reverse_iterator ite = it -> second -> Log.rbegin(); (!LowerBoundArrived || sum < 5)&&(ite != it -> second ->Log.rend());ite ++)
{
if(ite.base() == it -> second -> Log.lower_bound(Date(year,month,1)))
{
LowerBoundArrived = true;
}
sum ++;
fout <<"Log= "<< ite -> second.getDate().TransferToString() <<" " << ite -> second.getAmount() <<" " <<ite -> second.getDetail() << endl;
}
fout << "LogInfoEnd" << endl;
fout << "SavingsAccountEnd" << endl; //储蓄账户数据结束标志
}
if (it->second->getAccountType() == TypeCredit)
{
fout << "CreditAccountStart" << endl; //信用账户数据起始标志
fout << "ID= " << it->second->getId() << endl;
fout << "Balance= " << it->second->getBalance() << endl;
fout << "Rate= " << it->second->getRate() << endl;
fout << "LastRecordedDate= " << it->second->getLastRecordedDate().TransferToString() << endl;
fout << "AccumulationLastDate= " << it->second->accumulation.lastDate.TransferToString() << endl;
fout << "AccumulationValue= " << it->second->accumulation.value << endl;
fout << "AccumulationSum= " << it->second->accumulation.sum << endl;
CreditAccount *tmp = dynamic_cast<CreditAccount *>(it->second);
fout << "Credits= " << tmp->getCredits() << endl;
fout << "AnnualFee= " << tmp->getAnnualFee() << endl;
fout << "LogInfoStart" << endl;
int year = Today.getYear();
int month = Today.getMonth();
if(month <= 3)
{
year-=1;
month = 12-3+month;
}
int sum = 0;
bool LowerBoundArrived = false;
for(multimap<Date,LogInfo> ::reverse_iterator ite = it -> second -> Log.rbegin(); (!LowerBoundArrived || sum < 5)&&(ite != it -> second ->Log.rend());ite ++)
{
if(ite.base() == it -> second -> Log.lower_bound(Date(year,month,1)))
{
LowerBoundArrived = true;
}
sum ++;
fout <<"Log= "<< ite -> second.getDate().TransferToString() <<" " << ite -> second.getAmount() <<" " <<ite -> second.getDetail() << endl;
}
fout << "LogInfoEnd" << endl;
fout << "CreditAccountEnd" << endl; //信用账户数据结束标志
}
}
fout << "AccountEnd" << endl;//帐户信息结束标志
fout << "UserEnd" << endl;
}
fout << "EndFile" << endl; //文件结束标识
//cout << "** 数据保存完成" << endl;
fout.close();
string info = "数据保存完成";
return info;
}
//从文件中读取数据
string System::ReadFile()
{
//cout << "正在从文件中读取数据..." << endl;
fstream fin;
fin.open(DataBaseFile);
if (!fin)
{
//cout << "文件打开错误。如果这是第一次运行程序,请按回车跳过,若不是,请检查文件目录下的DataBase.txt是否正常" << endl;
cin.ignore();
fin.close();
return "文件打开错误。如果这是第一次运行程序,请忽略此消息,若不是,请检查文件目录下的DataBase.txt是否正常";
}
string index;
int SumOfAccount = 0;
int SumOfUser = 0;
while (!fin.eof())
{
fin >> index;
//cout << "1: " << index << endl;
if (index == "UserStart")
{
SumOfUser++;
User *usr;
string username;
string password;
while (index != "UserInfoEnd")
{
fin >> index;
//cout << "6:" << index << endl;
if (index == "Username=")
{
fin >> username;
}
else if (index == "Password=")
{
fin >> password;
}
}
usr = new User(username, password,logmgr);
Account *tmp;
fin >> index;
//cout << "5: " << index << endl;
if (index == "AccountStart")
{
while (index != "AccountEnd")
{
fin >> index;
//cout <<"4:" <<endl;
while (index == "SavingsAccountStart")
{
SumOfAccount++;
//读入学生基本信息
string id = "";
double balance = 0;
double rate = 0;
Date LastRecordedDate(2019, 1, 1);
Date AccumulationLastDate(2019, 1, 1);
double AccumulationValue = 0;
double AccumulationSum = 0;
multimap<Date,LogInfo> log;
while (index != "SavingsAccountEnd")
{
fin >> index;
//cout << "2:" << index << endl;
if (index == "ID=")
{
fin >> id;
}
else if (index == "Balance=")
{
fin >> balance;
}
else if (index == "Rate=")
{
fin >> rate;
}
else if (index == "LastRecordedDate=")
{
fin >> LastRecordedDate;
}
else if (index == "AccumulationLastDate=")
{
fin >> AccumulationLastDate;
}
else if (index == "AccumulationValue=")
{
fin >> AccumulationValue;
}
else if (index == "AccumulationSum=")
{
fin >> AccumulationSum;
}
else if(index == "LogInfoStart")
{
fin >> index;
while(index != "LogInfoEnd")
{
Date date;
double amount;
string detail;
fin >> date >> amount >> detail;
log.insert(make_pair(date,LogInfo(date,amount,detail)));
fin >> index;
}
}
}
tmp = new SavingsAccount(LastRecordedDate, id, rate, usr->getUsername(), balance, AccumulationLastDate,AccumulationValue,AccumulationSum,log,logmgr);
usr->AddAccount(id, tmp);
}
while (index == "CreditAccountStart")
{
SumOfAccount++;
//读入学生基本信息
string id = "";
double balance = 0;
double rate = 0;
Date LastRecordedDate(2019, 1, 1);
Date AccumulationLastDate(2019, 1, 1);
double AccumulationValue = 0;
double AccumulationSum = 0;
double credits = 0;
double annualFee = 0;
multimap<Date,LogInfo> log;
while (index != "CreditAccountEnd")
{
fin >> index;
//cout << "3: " << index << endl;
if (index == "ID=")
{
fin >> id;
}
else if (index == "Balance=")
{
fin >> balance;
}
else if (index == "Rate=")
{
fin >> rate;
}
else if (index == "LastRecordedDate=")
{
fin >> LastRecordedDate;
}
else if (index == "AccumulationLastDate=")
{
fin >> AccumulationLastDate;
}
else if (index == "AccumulationValue=")
{
fin >> AccumulationValue;
}
else if (index == "AccumulationSum=")
{
fin >> AccumulationSum;
}
else if (index == "Credits=")
{
fin >> credits;
}
else if (index == "AnnualFee=")
{
fin >> annualFee;
}
else if(index == "LogInfoStart")
{
fin >> index;
while(index != "LogInfoEnd")
{
Date date;
double amount;
string detail;
fin >> date >> amount >> detail;
log.insert(make_pair(date,LogInfo(date,amount,detail)));
fin >> index;
}
}
}
tmp = new CreditAccount(LastRecordedDate, id, rate, credits, annualFee, usr->getUsername(), balance, AccumulationLastDate, AccumulationValue, AccumulationSum,log,logmgr);
usr->AddAccount(id, tmp);
}
}
}
AddUser(username, usr);
}
}
fin.close();
return to_string(SumOfUser) + " 个用户信息 " + to_string(SumOfAccount) + " 个账户信息读取完成。";
}
void System::CMDUseAccount()
{
string id;
cin >> id;
if (CurrentUser != nullptr)
{
CurrentUser->UseAccount(id);
cout <<"** 欢迎您 " <<id<<" ,请告诉我今天的日期:";
cin >> Today;
CurrentUser->CurrentAccount->Process(Today);
}
else
{
throw runtime_error("未登录,无法选择账户");
}
}
void System::UseAccount(string id)
{
if (CurrentUser != nullptr)
{
CurrentUser->UseAccount(id);
CurrentUser->CurrentAccount->Process(Today);
}
else
{
throw runtime_error("未登录,无法选择账户");
}
}
void System::HelpMe(string cmd)
{
if (cmd == "all" || cmd == "help")
{
cout << "help:查看帮助信息" << endl;
cout << "格式:" << endl;
cout << "help [命令名]" << endl;
cout << "说明:" << endl;
cout << "使用help all 可以查看全部命令帮助信息" << endl;
cout << "样例:" << endl;
cout << "1.help register" << endl;
cout << "2.help all" << endl;
}
if (cmd == "all" || cmd == "new-acc")
{
cout << "new-acc:注册账号。" << endl;
cout << "格式:" << endl;
cout << "信用账户:new-acc [日期] Credit [ID] [利率] [信用额度] [年费]" << endl;
cout << "储蓄账户:new-acc [日期] Savings [ID] [利率]" << endl;
cout << "说明:" << endl;
cout << "[日期]格式:YYYY-MM-DD,例:2019-1-1" << endl;
cout << "[利率]使用浮点形式,不能使用百分数。" << endl;
cout << "样例:" << endl;
cout << "1.new-acc 2019-1-1 Credit 001 0.015 10000 1000" << endl;
cout << "2.new-acc 2019-12-30 Savings LIUYANG 0.015" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all" || cmd == "use-acc")
{
cout << "use-acc:登陆后选择账户。" << endl;
cout << "格式:" << endl;
cout << "login [ID]" << endl;
cout << "样例:" << endl;
cout << "1.use-acc 001" << endl;
cout << "2.use-acc LIUYANG" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all" || cmd == "logout")
{
cout << "logout:登出账号." << endl;
cout << "格式:" << endl;
cout << "logout" << endl;
cout << "样例:" << endl;
cout << "1.logout" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all" || cmd == "deposit")
{
cout << "deposit:实现存款操作。" << endl;
cout << "格式:" << endl;
cout << "deposit [金额]" << endl;
cout << "样例:" << endl;
cout << "1.deposit 5000" << endl;
cout << "2.deposit 10000" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all" || cmd == "withdraw")
{
cout << "withdraw:实现取款操作。" << endl;
cout << "格式:" << endl;
cout << "withdraw [日期] [金额]" << endl;
cout << "样例:" << endl;
cout << "1.withdraw 5000" << endl;
cout << "2.withdraw 10000" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all" || cmd == "show")
{
cout << "show:查看账户信息。" << endl;
cout << "格式:" << endl;
cout << "show" << endl;
cout << "样例:" << endl;
cout << "show" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all" || cmd == "exit")
{
cout << "exit:保存全部操作并退出系统。" << endl;
cout << "格式:" << endl;
cout << "exit" << endl;
cout << "样例:" << endl;
cout << "exit" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all" || cmd == "register")
{
cout << "register:注册账号。" << endl;
cout << "格式:" << endl;
cout << "register" << endl;
cout << "样例:" << endl;
cout << "register" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all" || cmd == "login")
{
cout << "login:注册账号。" << endl;
cout << "格式:" << endl;
cout << "login" << endl;
cout << "样例:" << endl;
cout << "login" << endl;
cout << "------------------------------------------------------------" << endl;
}
if (cmd == "all"|| cmd == "show-log" )
{
cout << "show-log:查询日志。" << endl;
cout << "格式:" << endl;
cout << "show-log [日期] [排序方式]" << endl;
cout << "说明:"<<endl;
cout << "[日期]:输入完整日期,精确到月,如2019-1-1 和2019-1-30都表示2019年1月,不允许输入2019-1"<<endl;
cout << "[排序方式]:2种,time 按时间排序 value按金额从大到小排序"<<endl;
cout << "样例:" << endl;
cout << "show-log 2019-1-1 time 查看2019年1月的日志,按照时间排序" << endl;
cout << "------------------------------------------------------------" << endl;
}
else
{
cout << "command " << cmd << " not fount" << endl;
}
}
void System::Start()
{
cout << "---------------Welcome!-------------------" << endl;
cout << endl;
cout << " ==== ====" << endl;
cout << " \\\\ //" << endl;
cout << " \\\\ //" << endl;
cout << " \\\\ //" << endl;
cout << " \\\\ //" << endl;
cout << " ========= " << endl;
cout << " |||" << endl;
cout << " |||" << endl;
cout << " |||" << endl;
cout << " =====" << endl << endl;
while (MainLoop());
}
//UI界面和Mainloop
bool System::UIStart()
{
if(CurrentUser == nullptr)
{ if(!UILogin())
{
return 0;
}
}
return 0;
}
//可视化登录界面
bool System::UILogin()
{
LoginDialog LoginDlg(this);
if(LoginDlg.exec()==LoginDialog::Accepted)
{
return true;
}
else
{
return false;
}
}
void System::ShowMenu(bool showDetail)
{
if (CurrentUser == nullptr)
{
if (showDetail)
{
cout << "-----------------菜单:----------------------" << endl;
cout << "_________________________________________________________" << endl;
cout << "| 命令 | 说明 " << endl;
cout << "| register |注册账号 " << endl;
cout << "| login |登陆 " << endl;
cout << "| help |查看命令帮助信息。格式:help [命令名] " << endl;
cout << "| exit |保存操作并退出系统。 " << endl;
cout << "————————————————————————————" << endl;
}
cout << "** 您还未登录,请登陆或注册." << endl;
cout << " None@None$";
}
else if (!CurrentUser->isAccountSelected())
{
if (showDetail)
{
cout << "-----------------菜单:----------------------" << endl;
cout << "_________________________________________________________" << endl;
cout << "| 命令 | 说明 " << endl;
cout << "| new-acc |创建账户,格式:new-acc [日期] [账户类型] [id]" << endl;
cout << "| [利率] [信用额度] [年费] Saving账户省略后两项 " << endl;
cout << "| use-acc |选择账户,格式 use-acc [id] " << endl;
cout << "| show-acc |查看所有账户信息 " << endl;
cout << "| logout |登出账号,格式 logout " << endl;
cout << "| help |查看命令帮助信息。格式:help [命令名] " << endl;
cout << "| exit |保存操作并退出系统。 " << endl;
cout << "—————————————————————————————" << endl;
}
cout << "** 你还没有选择账户." << endl;
cout << "None"
<< "@" << CurrentUser->getUsername() << "$";
}
else
{
if (showDetail)
{
cout << "-----------------菜单:----------------------" << endl;
cout << "_________________________________________________________" << endl;
cout << "| 命令 | 说明 " << endl;
cout << "| new-acc |创建账户,格式:new-acc [日期] [账户类型] [id]" << endl;
cout << "| [利率] [信用额度] [年费] Saving账户省略后两项 " << endl;
cout << "| use-acc |选择账户,格式 use-acc [id] " << endl;
cout << "| show-acc |查看所有账户信息 " << endl;
cout << "| logout |登出账号,格式 logout " << endl;
cout << "| logout-acc |登出账户,格式 logout-acc " << endl;
cout << "| deposit |实现存款操作。用法:deposit [金额] " << endl;
cout << "| withdraw |实现取款操作。用法:withdraw [金额] " << endl;
cout << "| show-log |查询某个月的日志。用法:show-log [日期] [排序方式]" <<endl;
cout << "| show |展示账户信息。用法:show [日期] " << endl;
cout << "| help |查看命令帮助信息。格式:help [命令名] " << endl;
cout << "| exit |保存操作并退出系统。 " << endl;
cout << "————————————————————————————" << endl;
}
//cout << "$请输入命令,使用 \"help all\"查看全部命令。" << endl;
cout << CurrentUser->CurrentAccount->getId() << "@" << CurrentUser->getUsername() << "$";
}
}
bool System::MainLoop()
{
static bool ShowDetail = true;
try
{
ShowMenu(ShowDetail);
ShowDetail = false;
string cho;
cin >> cho;
if (cho == "help")
{
string cmd;
cin >> cmd;
HelpMe(cmd);
}
else if (cho == "exit")
{
return false;
}
else
{
if (CurrentUser == nullptr)
{
if (cho == "register")
{
CMDRegister();
}
else if (cho == "login")
{
CMDLogin();
ShowDetail = true;
}
else
cout << "Command " << cho << " Not Fount." << endl;
}
else
{
if (cho == "new-acc")
{
CMDCreateAccount();
}
else if (cho == "use-acc")
{
CMDUseAccount();
ShowDetail = true;
}
else if (cho == "show-acc")
{
CurrentUser->ShowAllAccount();
}
else if (cho == "logout")
{
Logout();
}
else if (CurrentUser->isAccountSelected())
{
if (cho == "logout-acc")
{
CurrentUser->AccountLogout();
}
if (cho == "deposit")
{
try
{
double amount;
cin >> amount;
//cout <<"Debug" << Today << endl;
//cout <<"Debug::存款"<<endl;
CurrentUser->Deposit(Today, amount);
}
catch (const std::exception &e)
{
std::cerr <<"【存款错误】:" << e.what() << '\n';
}
}
else if (cho == "withdraw")
{
try
{
//cout << "哈哈哈"<<endl;
double amount;
cin >> amount;
//cout <<"Debug" << Today << endl;
//cout <<"Debug::取款"<<endl;
CurrentUser->Withdraw(Today, amount);
}
catch (const std::exception &e)
{
std::cerr <<"【取款错误】:"<< e.what() << '\n';
}
}
else if(cho == "show-log")
{
try
{
Date date;
string method;
cin >> date >> method;
if(method == "time")
CurrentUser -> CurrentAccount -> ShowLogTime(date);
else
CurrentUser -> CurrentAccount -> ShowLogAmount(date);
}
catch(const std::exception& e)
{
std::cerr <<"【查询错误】: "<< e.what() << '\n';
}
}
else if (cho == "show")
{
CurrentUser->CurrentAccount->show();
}
}
else
cout << "Command " << cho << " Not Fount." << endl;
}
}
}
catch (const std::exception &e)
{
std::cerr <<"【其他错误】: "<< e.what() << '\n';
return true;
}
return true;
}
User * System::getCurrentUser()
{
return CurrentUser;
}
void System::Deposit(double amount)
{
if(CurrentUser != nullptr)
{
//cout << "哈哈哈"<<endl;
//cout <<"Debug" << Today << endl;
//cout <<"Debug::取款"<<endl;
CurrentUser->Deposit(Today, amount);
}
else
throw("没有登录,不能进行操作");
}
void System::Withdraw(double amount)
{
if(CurrentUser != nullptr)
{
//cout << "哈哈哈"<<endl;
//cout <<"Debug" << Today << endl;
//cout <<"Debug::取款"<<endl;
CurrentUser->Withdraw(Today, amount);
}
else
throw("没有登录,不能进行操作");
}