-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·212 lines (119 loc) · 4.69 KB
/
main.cpp
File metadata and controls
executable file
·212 lines (119 loc) · 4.69 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
#include <iostream>
#include <fstream>
#include <set>
#include <sstream>
#include <vector>
#include <iomanip>
#include "product.h"
#include "datastore.h"
#include "db_parser.h"
#include "product_parser.h"
#include "util.h"
#include "taobao.h"
#include <map>
#include "review.h"
#include "main_window.h"
#include <QApplication>
using namespace std;
void displayProducts(vector<Product*>& hits, TaoBao& ds);
int main(int argc, char* argv[]) {
if(argc < 2) {
cerr << "Please specify a database file" << endl;
}
TaoBao ds; //TaoBao is China's version of Amazon
// Instantiate the parser
DBParser parser;
// Instantiating the individual product parsers
parser.addProductParser(new ProductBookParser);
parser.addProductParser(new ProductClothingParser);
parser.addProductParser(new ProductMovieParser);
if( parser.parse(argv[1], ds) ) {
cerr << "Error parsing!" << endl;
return 1;
}
/*
1) Computes average rating for all products in database
2) For each review, gets the customer name and adds it to a data structure keeping track of users
and the products they reviewed
*/
for (unsigned int i = 0; i < ds.all_products.size(); i++) { //For each product in the database
string product_name = ds.all_products[i]->getName();
double sum = 0.0;
double counter = 0.0;
double average_rating = 0.0; //By default is 0 if no reviews
string name;
string name2;
map<string, vector<Review*> >::iterator it = ds.reviews_bank.find(product_name);
if (it != ds.reviews_bank.end()) { //If reviews for the current product exist
for (unsigned int j = 0; j < it->second.size(); j++) {
sum += it->second[j]->rating; //Adds the rating from each review to a running sum
counter++;
name = it->second[j]->username;
map<string, User*>::iterator it11 = ds.customers.find(name); //Finds the user who wrote the review
if (it11 != ds.customers.end() ) {
User* current_user_for_review = it11->second;
map<User*, set<Product*> >::iterator it12 = ds.product_user_reviews.find(current_user_for_review); //Finds the set of products that the user has reviewed
if (it12 == ds.product_user_reviews.end() ) { //If the set currently does not exist
set<Product*> cart;
cart.insert(ds.all_products[i]);
ds.product_user_reviews.insert(make_pair(current_user_for_review, cart)); //Makes a new set
}
else {
it12->second.insert(ds.all_products[i]); //Puts product in user's existing product set
}
}
}
if (sum != 0.0) {
average_rating = sum/counter;
}
}
ds.all_products[i]->average_rating = average_rating; //Equals either the average of all ratings or 0
}
QApplication app(argc, argv);
bool not_hacker = false;
MainWindow mainwindow_login(ds, ¬_hacker);
mainwindow_login.show();
app.exec();
if (not_hacker == true) { //User credentials are correct
MainWindow mainwindow(ds);
mainwindow.show();
app.exec();
}
/*
//Order 66 (Deletes all users in the database; Allocated memory)
for(map<string, User*>::iterator it = ds.customers.begin();
it != ds.customers.end();
++it) {
delete it->second;
}
//Deletes all products in the database
for (unsigned int i = 0; i < ds.all_products.size(); i++) {
delete ds.all_products[i];
}
//Deletes all reviews in the database
for (unsigned int i = 0; i < ds.all_reviews.size(); i++) {
delete ds.all_reviews[i];
}
*/
return 0;
}
void displayProducts(vector<Product*>& hits, TaoBao& ds) {
int resultNo = 1;
for(vector<Product*>::iterator it1 = hits.begin(); it1 != hits.end(); ++it1) {
cout << "Hit " << setw(3) << resultNo << endl;
cout << (*it1)->displayString() << endl;
cout << endl;
resultNo++;
string product_name = (*it1)->getName();
map<string, vector<Review*> >::iterator it2 = ds.reviews_bank.find(product_name);
if (it2!= ds.reviews_bank.end()) {
vector<Review*> product_reviews = it2->second;
for (unsigned int i = 0; i < product_reviews.size(); i++) {
cout << product_reviews[i]->displayString() << endl;
}
}
else {
cout << "No reviews yet" << endl;
}
}
}