-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
192 lines (155 loc) · 5.41 KB
/
main.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
/*
Contains:
* Base loop
* Sign up
* Login
*/
#include "Views.cpp"
bool login()
{
clear_cmd_();//clear cmd
//Options menu to get logined user type menu.
OptionsMenu user_type_menu;
user_type_menu.add_option("Customer");
user_type_menu.add_option("Admin");
user_type_menu.add_option("Back to main menu", pre_menu);
if(!user_type_menu.run(false))
return true; //Back to main menu
auto user_type{user_type_menu.get_choosed_option()};
auto error_msg{std::string{""}};
while (true)
{
clear_cmd_();//clear cmd
std::cout<<error_msg;
error_msg="";
//Inputs menu to get login info.
InputsMenu menu;
menu.add_input("Name");
menu.add_input("Password", '$');
menu.run(false);
if(user_type=="Customer")
{
Customer customer;
LoginInfo login_info{menu.get_answers()[0], menu.get_answers()[1]};
if(customer.login(login_info))
while(customer_view(customer));//Customer view loop.
else
{
error_msg+= "Wrong username or password\n\n";
continue;//relogin
}
}
else if(user_type=="Admin")
{
Admin admin;
LoginInfo login_info{menu.get_answers()[0], menu.get_answers()[1]};
if(admin.login(login_info))
while(admin_view(admin));//Admin view loop.
else
{
//return true;//return to base loop (base menu).
error_msg+= "Wrong username or password\n\n";
continue;//relogin
}
}
return true;//when the user's view loop end return to base loop (base menu).
}
}
bool sign_up()
{
clear_cmd_();//clear cmd
/*Options menu to get logined user type menu.*/
OptionsMenu user_type_menu;
user_type_menu.add_option("Customer");
user_type_menu.add_option("Admin");
user_type_menu.add_option("Back to main menu", pre_menu);
if(!user_type_menu.run(false))
return true; //Back to main menu
auto user_type{user_type_menu.get_choosed_option()};
auto error_msg{std::string{""}};
//Sign up loop
while (true)
{
clear_cmd_();//clear cmd
std::cout<<error_msg;
error_msg="";//reset error message
/*Inputs menu to get login info.*/
InputsMenu menu;
menu.add_input("Name", '\n');
menu.add_input("Password", '$');
menu.run(false);
//InputsMenu already check for empty.
/*if(menu.get_answers()[1]=="")
{
error_msg+= "Password can't be empty.\n\n";
continue;
}*/
LoginInfo login_info{menu.get_answers()[0], menu.get_answers()[1]};
if(user_type=="Customer")
{
/*Get payment methods info from customer (at least one payment to sign up)*/
auto payment_methods{Data::get_payment_methods()};
int methods_signed{0};
///Create empty methods info with same order as payment_methods
std::vector<PaymentInfo> methods_info;
for (int i = 0; i < (int)payment_methods.size(); i++)
methods_info.push_back(PaymentInfo{});
//Payments info loop.(break then continue when finish with error_msg)
//(break only when successfully finished)
//(else will return false)
bool is_error{false};
while (true)
{
OptionsMenu menu{"\n Add payment info :\n"};
for (int i = 0; i < (int)payment_methods.size(); i++)
menu.add_option(payment_methods[i]);
if(methods_signed>0)
menu.add_option("Finish");
menu.add_option("Back to main menu", pre_menu);
if(!menu.run())
return true; // Back to base(main) menu.
if(menu.get_choosed_option()=="Finish")
break; //Successfully signed up
/*ask payment info*/
int idx{menu.get_choosed_number()-1}; //-1 to make it zero based.
methods_info[idx]= ask_payment_info(); //get info from user
methods_signed+=1;
// Future update:
/// Add check payment info methods in the payments APIs and interface then check it here.
if(methods_info[idx].is_empty())
{
//back to the beginning of sign up
error_msg+="Invalid Payment Info\n\n";
is_error=true;
break;
}
}
if(is_error)
continue;
if(!Customer::register_(login_info, methods_info))
{
error_msg+= "Name already taken\n\n";
continue;
}
}
else if(user_type=="Admin")
{
if(!Admin::register_(login_info))
{
error_msg+= "Name already taken\n\n";
continue;
}
}
return true;//Pass all checks :)
}
}
int main()
{
//Base menu.
OptionsMenu menu;
menu.add_option("Login", login);
menu.add_option("Sign up", sign_up);
menu.add_option("Exit", pre_menu);
while(menu.run());//Base app loop.
return 0;
}