-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazon.cpp
More file actions
161 lines (148 loc) · 4.85 KB
/
amazon.cpp
File metadata and controls
161 lines (148 loc) · 4.85 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
#include <iostream>
#include <fstream>
#include <set>
#include <sstream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include "product.h"
#include "db_parser.h"
#include "product_parser.h"
#include "util.h"
#include "mydatastore.h"
using namespace std;
struct ProdNameSorter {
bool operator()(Product* p1, Product* p2) {
return (p1->getName() < p2->getName());
}
};
void displayProducts(vector<Product*>& hits);
int main(int argc, char* argv[])
{
if(argc < 2) {
cerr << "Please specify a database file" << endl;
return 1;
}
/****************
* Declare your derived DataStore object here replacing
* DataStore type to your derived type
****************/
MyDataStore ds;
// Instantiate the individual section and product parsers we want
ProductSectionParser* productSectionParser = new ProductSectionParser;
productSectionParser->addProductParser(new ProductBookParser);
productSectionParser->addProductParser(new ProductClothingParser);
productSectionParser->addProductParser(new ProductMovieParser);
UserSectionParser* userSectionParser = new UserSectionParser;
// Instantiate the parser
DBParser parser;
parser.addSectionParser("products", productSectionParser);
parser.addSectionParser("users", userSectionParser);
// Now parse the database to populate the DataStore
if( parser.parse(argv[1], ds) ) {
cerr << "Error parsing!" << endl;
return 1;
}
cout << "=====================================" << endl;
cout << "Menu: " << endl;
cout << " AND term term ... " << endl;
cout << " OR term term ... " << endl;
cout << " ADD username search_hit_number " << endl;
cout << " VIEWCART username " << endl;
cout << " BUYCART username " << endl;
cout << " QUIT new_db_filename " << endl;
cout << "====================================" << endl;
vector<Product*> hits;
bool done = false;
while(!done) {
cout << "\nEnter command: " << endl;
string line;
getline(cin,line);
stringstream ss(line);
string cmd;
if((ss >> cmd)) {
if( cmd == "AND") {
string term;
vector<string> terms;
while(ss >> term) {
term = convToLower(term);
terms.push_back(term);
}
hits = ds.search(terms, 0);
displayProducts(hits);
}
else if ( cmd == "OR" ) {
string term;
vector<string> terms;
while(ss >> term) {
term = convToLower(term);
terms.push_back(term);
}
hits = ds.search(terms, 1);
displayProducts(hits);
}
else if ( cmd == "QUIT") {
string filename;
if(ss >> filename) {
ofstream ofile(filename.c_str());
ds.dump(ofile);
ofile.close();
}
done = true;
}
/* Add support for other commands here */
else if( cmd == "ADD") {
string username;
string hitIndex;
ss >> username;
ss >> hitIndex;
if(ss.fail()){
cout << "Invalid request" << endl;
return 1;
}
ds.addCart(username, hits[stoi(hitIndex)-1]);
}
else if( cmd == "VIEWCART"){
string username;
ss >> username;
vector<Product*> cart = ds.getCart(username);
if(cart.size()>0){
ds.viewCart(cart);
}
}
else if( cmd == "BUYCART"){
string username;
ss >> username;
ds.buyCart(username);
}
else if( cmd == "QUIT"){
string filename;
if(ss >> filename) {
ofstream ofile(filename.c_str());
ds.dump(ofile);
ofile.close();
}
done = true;
}
else {
cout << "Unknown command" << endl;
}
}
}
return 0;
}
void displayProducts(vector<Product*>& hits)
{
int resultNo = 1;
if (hits.begin() == hits.end()) {
cout << "No results found!" << endl;
return;
}
std::sort(hits.begin(), hits.end(), ProdNameSorter());
for(vector<Product*>::iterator it = hits.begin(); it != hits.end(); ++it) {
cout << "Hit " << setw(3) << resultNo << endl;
cout << (*it)->displayString() << endl;
cout << endl;
resultNo++;
}
}