-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
188 lines (168 loc) · 5.33 KB
/
controller.cpp
File metadata and controls
188 lines (168 loc) · 5.33 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
#include "controller.h"
#include <fstream>
#include <iostream>
Controller::Controller(Store store): _store{store}, _view{store}{};
double Controller::get_double(std::string prompt) {
while(true) {
try {
std::string s = Dialogs::input(prompt);
if(s == "") break;
std::istringstream iss{s};
double d;
iss >> d;
return d;
} catch(std::runtime_error e) {
std::cerr << "Error: Invalid input: " << e.what() << std::endl;
}
}
throw std::runtime_error{prompt + " was not detected"};
}
int Controller::get_int(std::string prompt) {
while(true) {
try {
std::string s = Dialogs::input(prompt);
if(s == "") break;
std::istringstream iss{s};
int d;
iss >> d;
return d;
} catch(std::runtime_error e) {
std::cerr << "Error: Invalid input: " << e.what() << std::endl;
}
}
throw std::runtime_error{prompt + " was not detected"};
}
void Controller::cli(){
int cmd=1;
while(true){
try {
std::string Message = _view.main_menu();
//Message += "\n\nCommand?";
cmd = get_int(Message);
if (cmd == 0) break;
execute_cmd(cmd);
} catch (std::runtime_error e) {
std::cerr << "Error: Invalid input" << std::endl;
}
}
}
void Controller::execute_cmd(int cmd){
switch (cmd)
{
case 1: add_product(); break;
case 2: list_all_product(true); break;
case 3: add_order(); break;
case 4: list_all_order(); break;
case 5: help(); break;
case 6: save();break;
case 7: load();break;
case 0: break;
default: cmd =6;
break;
}
}
void Controller::add_product(){
std::string name;
double price;
bool cont = true;
while(cont){
try {
name = Dialogs::input("Product name?");
if (name.empty()) throw std::runtime_error{"Unavailable product name"};
price = get_double("Price?");
_store.add_product(Product{name, price});
} catch(std::runtime_error e) {
std::cerr << "Error: " << e.what() << std::endl;
}
std::vector<std::string> buttons ={"Yes","No"};
int conti= Dialogs::question("Do you want continue?","Decide!",buttons);
if (conti ==0) cont =true;
else cont =false;
}
}
std::string Controller::list_all_product(bool state){
std::stringstream iss;
iss << "\nAvailable Products\n";
for(int i=0; i<_store.num_products(); ++i)
{
iss << i << ") " << _store.product(i) << std::endl;
}
std::string message = iss.str();
if(state==true) {
Dialogs::message(message,"All Products");
}
return message;
}
void Controller::add_order(){
/*std::string email;
std::cout << "Enter your email address: ";
std::cin >> email;
int orderid=_store.create_order(email);
bool cont=true;
std::string name;
double price;
int num;
while(cont){
std::cout << "Enter the name of product and price: ";
std::cin >> name >> price;
std::cout << "Enter the quantity: ";
std::cin >> num;
Product_order pro(name,price,num);
_store.add_to_order(orderid, pro);
std::string answer;
std::cout << "Do you want to continue or not (y or n): "<<std::endl;
std::cin >> answer;
if (answer =="y") cont =true;
else cont =false;
}*/
try {
std::string email=Dialogs::input("Customer email address? ");
if (email.empty()) throw std::runtime_error{"Empty email address"};
int order_num = _store.create_order(email);
while(true) {
std::stringstream iss;
iss << "\n\nCurrent Products in Order " << order_num << "\n\n"
<< _store.order(order_num) << "\n" << "\n";
iss<<Controller::list_all_product(false);
iss<<"\n\n"<<"Product number to add (-1 when done)";
int product_num = get_int(iss.str());
if (0 <= product_num && product_num < _store.num_products()) {
int product_quantity = get_int("Quantity?");
if (0 <= product_quantity)
_store.add_to_order(order_num, Product_order{_store.product(product_num), product_quantity});
else
std::cerr << "Invalid quantity: " << product_quantity << std::endl;
} else if (product_num == -1) {
break;
} else {
std::cerr << "No such product number: " << product_num << std::endl;
}
}
} catch (std::runtime_error e) {
std::cerr << "Place Order aborted: " << e.what() << std::endl;
}
}
void Controller::list_all_order(){
std::stringstream iss;
for(int i=0; i<_store.num_orders(); ++i)
{
iss << i << ") " << _store.order(i) << "\n";
}
std::string message = iss.str();
Dialogs::message(message,"All Orders");
}
void Controller::help(){
std::string Message=_view.help();
Dialogs::message(Message,"Help");
}
void Controller::easter_egg(){
_store.add_product(Product("soda",3.44));
}
void Controller::save(){
std::string path = "./database";
_store.save(path);
}
void Controller::load(){
std::string path = "./database";
_store.load(path);
}