This repository has been archived by the owner on Feb 16, 2019. It is now read-only.
forked from hadisfr/ratatouille
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
97 lines (89 loc) · 2.92 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "system.h"
#include "util.h"
#define NEW_SUPER -1
#define ERRSTR "ERR"
using namespace std;
void readDatabase(System &system, string customersFileName, string restaurantsFileName){
string line;
ifstream restaurantsFileStr(restaurantsFileName, ifstream::in);
int restaurantId = NEW_SUPER;
while(getline(restaurantsFileStr, line)){
if(line == ""){
restaurantId = NEW_SUPER;
}
else{
vector <string> inputParameters = parseString(line, ',');
if(restaurantId == NEW_SUPER){
restaurantId = atoi(inputParameters[1].c_str());
system.addRestaurant(inputParameters[0], restaurantId, inputParameters[2]);
}
else{
system.addFood(restaurantId, atoi(inputParameters[0].c_str()), inputParameters[1], Food::stringToType(inputParameters[2]), atoi(inputParameters[3].c_str()));
}
}
}
restaurantsFileStr.close();
ifstream customersFileStr(customersFileName, ifstream::in);
while(getline(customersFileStr, line)){
vector <string> inputParameters = parseString(line, ',');
system.addCustomer(inputParameters[0], atol(inputParameters[1].c_str()), inputParameters.size() > 2 ? inputParameters[2] : "");
}
customersFileStr.close();
}
void systemIo(istream &istr, ostream &ostr, System &system){
string line;
while(getline(istr, line)){
vector <string> inputParameters = parseString(line, ' ');
if(inputParameters[0] == "menu"){
ostr << system.getMenu(atoi(inputParameters[1].c_str()));
}
else if(inputParameters[0] == "restaurants"){
ostr << system.getRestaurantsDetail(inputParameters.size() > 1 ? atol(inputParameters[2].c_str()) : 0);
}
else if(inputParameters[0] == "list"){
if(inputParameters[1] == "near"){
ostr << system.listFoods(atol(inputParameters[2].c_str()));
}
else if(inputParameters[1] == "type"){
ostr << system.listFoods(Food::stringToType(inputParameters[2]));
}
else ostr << ERRSTR << endl;
}
else if(inputParameters[0] == "order"){
long customerId = atol(inputParameters[1].c_str());
system.newOrder(customerId);
while(getline(istr, line) && line != "$"){
inputParameters = parseString(line, ' ');
string personalizations = "";
for(int i = 2; i < inputParameters.size(); i++){
if(i > 2)
personalizations += string(" ");
personalizations += inputParameters[i];
}
system.addToOrder(customerId, atol(inputParameters[0].c_str()), atoi(inputParameters[1].c_str()), personalizations);
}
ostr << system.getOrderReport(customerId);
}
else if(inputParameters[0] == "bill"){
if(inputParameters[1] == "all"){
ostr << system.getAllBills(atol(inputParameters[2].c_str()));
}
else{
ostr << system.getLastBill(atol(inputParameters[1].c_str()));
}
}
else
ostr << ERRSTR << endl;
// ostr << endl;
}
}
int main(){
System system;
readDatabase(system, "customers.txt", "restaurants.txt");
systemIo(cin, cout, system);
return 0;
}