-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_window.cpp
executable file
·1004 lines (566 loc) · 28.1 KB
/
main_window.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "main_window.h"
#include "util.h" //To access the convToLower member function
using namespace std;
MainWindow::MainWindow() {
}
MainWindow::MainWindow(TaoBao& datastore) { //For instantiating the main QT window
//Gets reference/pointer to datastore object instantiated in main
ds = &datastore;
//Assigns the account holder to login information
user = ds->current_user;
//Overall layout of GUI
overall_layout = new QVBoxLayout();
//Greeting layout
greeting_layout = new QHBoxLayout();
overall_layout->addLayout(greeting_layout);
//Greeting message
greeting = new QLabel("Welcome to TaoBao, China's biggest ecommerce merchant");
greeting_layout->addWidget(greeting);
//Main products and search layout
search_interface = new QHBoxLayout();
overall_layout->addLayout(search_interface);
//Product recommendation layout
product_rec_layout = new QVBoxLayout();
search_interface->addLayout(product_rec_layout);
//Product recommendation label
product_rec_label = new QLabel("Product recommendation");
product_rec_layout->addWidget(product_rec_label);
//Recommended product to account holder
top_product = ds->product_recommendation(user);
//List containing the product recommendation
product_rec_list = new QListWidget();
product_rec_layout->addWidget(product_rec_list);
product_rec_list->addItem(QString::fromStdString(top_product->getName()));
//Add product recommendation to cart button
product_rec_button = new QPushButton("Add recommendation to cart");
product_rec_layout->addWidget(product_rec_button);
//Adds product recommendation to cart
connect(product_rec_button, SIGNAL(clicked()), this, SLOT(add_product_rec_to_cart()));
//List of product matches layout
product_matches_layout = new QVBoxLayout();
search_interface->addLayout(product_matches_layout);
//Product matches label
product_matches_label = new QLabel("Your search returned");
product_matches_layout->addWidget(product_matches_label);
//List of product matches
product_matches_list = new QListWidget();
product_matches_layout->addWidget(product_matches_list);
//Displays the information of the current product selected
connect(product_matches_list, SIGNAL(currentRowChanged(int)), this, SLOT(display_product_information(int)));
//Current product information layout
product_layout = new QVBoxLayout();
search_interface->addLayout(product_layout);
//Current product information string
display_product_info = new QLabel(" ");
product_layout->addWidget(display_product_info);
//Floats the string to the top
product_layout->addStretch();
//Main reviews layout
review_interface = new QHBoxLayout();
overall_layout->addLayout(review_interface);
//Current review layout
selected_review_layout = new QVBoxLayout();
review_interface->addLayout(selected_review_layout);
//Current review label
selected_review_label = new QLabel("Review information");
selected_review_layout->addWidget(selected_review_label);
//List of reviews for current product
list_of_reviews = new QListWidget();
review_interface->addWidget(list_of_reviews);
//Gets the reviews for the current product selected
connect(product_matches_list, SIGNAL(currentRowChanged(int)), this, SLOT(get_reviews_for_product()));
//Current review string
selected_review_info = new QLabel("");
selected_review_layout->addWidget(selected_review_info);
//Floats the string to the top
selected_review_layout->addStretch();
//Displays the information of the current review selected
connect(list_of_reviews, SIGNAL(currentRowChanged(int)), this, SLOT(display_selected_review(int)));
//Add new review layout
overall_add_review_layout = new QVBoxLayout();
review_interface->addLayout(overall_add_review_layout);
//Date input layout
date_layout = new QHBoxLayout();
overall_add_review_layout->addLayout(date_layout);
//Date input label
date_format = new QLabel("Enter date in format yyyy-mm-dd");
date_layout->addWidget(date_format);
//Date input box
date_input = new QLineEdit();
date_layout->addWidget(date_input);
//Rating input layout
rating_layout = new QHBoxLayout();
overall_add_review_layout->addLayout(rating_layout);
//Rating input label
rating_format = new QLabel("Enter rating 1-5");
rating_layout->addWidget(rating_format);
//Rating input box
rating_input = new QLineEdit();
rating_layout->addWidget(rating_input);
//Review text input layout
review_text_layout = new QHBoxLayout();
overall_add_review_layout->addLayout(review_text_layout);
//Review text input label
review_text_label = new QLabel("Enter review here");
review_text_layout->addWidget(review_text_label);
//Review text input box
review_text_input = new QTextEdit();
review_text_layout->addWidget(review_text_input);
//Review button
add_review_button = new QPushButton("Add new review");
overall_add_review_layout->addWidget(add_review_button);
//Adds a new review to the database
connect(add_review_button, SIGNAL(clicked()), this, SLOT(add_new_review()));
//Main search layout
search_layout = new QVBoxLayout();
search_interface->addLayout(search_layout);
//Adds an empty dialog box for a more pleasing interface
search_layout->addSpacing(50);
//Search words input box
search_entries = new QLineEdit();
search_layout->addWidget(search_entries);
//Search words input button
search_button = new QPushButton("Search");
search_layout->addWidget(search_button);
//Searches through the TaoBao object for products that fulfill the search requirements
connect(search_button, SIGNAL(clicked()), this, SLOT(search_taobao()));
//And/Or buttons layout
and_or_layout = new QHBoxLayout();
search_layout->addLayout(and_or_layout);
//And button
and_button = new QRadioButton("AND");
and_or_layout->addWidget(and_button);
//Adds the pre-requisite of the "and" keyword condition
connect(and_button, SIGNAL(clicked()), this, SLOT(and_search()));
//Or button
or_button = new QRadioButton("OR");
and_or_layout->addWidget(or_button);
//Adds the pre-requisite of the "or" keyword condition
connect(or_button, SIGNAL(clicked()), this, SLOT(or_search()));
//Sorting searches results
sort_search_layout = new QHBoxLayout();
search_layout->addLayout(sort_search_layout);
//Sorting searches label
sort_label = new QLabel("Choose to sort:");
sort_search_layout->addWidget(sort_label);
//Sort searches by alphabet button
alphabet = new QPushButton("Alphabetically");
sort_search_layout->addWidget(alphabet);
//Sorts current searches alphabetically
connect(alphabet, SIGNAL(clicked()), this, SLOT(sort_alphabetically()));
//Sort searches by rating button
rating = new QPushButton("By rating (highest to lowest)");
sort_search_layout->addWidget(rating);
//Sorts current searches by average rating from highest to lowest
connect(rating, SIGNAL(clicked()), this, SLOT(sort_by_average_rating()));
//Cart button options layout
cart_options_layout = new QHBoxLayout();
search_layout->addLayout(cart_options_layout);
//Add to user cart button
add_cart_button = new QPushButton("Add Cart");
cart_options_layout->addWidget(add_cart_button);
//Adds current product to the current user's cart
connect(add_cart_button, SIGNAL(clicked()), this, SLOT(add_user_cart()));
//View user cart button
view_cart_button = new QPushButton("View Cart");
cart_options_layout->addWidget(view_cart_button);
//Instantiates a new QT main window for the view cart window
connect(view_cart_button, SIGNAL(clicked()), this, SLOT(view_user_cart()));
//Save to database button
save_database = new QPushButton("Save database to file");
search_layout->addWidget(save_database);
//Saves the current database to the given output file
connect(save_database, SIGNAL(clicked()), this, SLOT(save_to_file()));
//Output file input box
output_file = new QLineEdit();
search_layout->addWidget(output_file);
//Exit store layouts
exit_store_layout = new QHBoxLayout();
search_layout->addLayout(exit_store_layout);
//Exit store button
exit_store_button = new QPushButton("Exit Store");
exit_store_layout->addWidget(exit_store_button);
//Exits back to main where QT main window was first shown/executed
connect(exit_store_button, SIGNAL(clicked()), this, SLOT(close()));
//No search preferences to begin with (neither "and" nor "or")
and_ = false;
or_ = false;
//Changes QT main window size
resize(QDesktopWidget().availableGeometry(this).size() * 0.9);
//Sets the overall layout of QT main window
setLayout(overall_layout);
}
MainWindow::MainWindow(TaoBao& passed_datastore, string user) {
ds = &passed_datastore;
customer_name = user;
overall_layout = new QVBoxLayout(); //Instantiates the overall layout for this QT window
string customer = "This is " + customer_name + "'s cart";
customer_cart = new QLabel(QString::fromStdString(customer));
overall_layout->addWidget(customer_cart);
list_of_cart_products = new QListWidget();
overall_layout->addWidget(list_of_cart_products); //Adds the cart list to the overall layout
buy_cart_button = new QPushButton("Buy cart");
overall_layout->addWidget(buy_cart_button); //Adds the buy cart button to the overall layout
connect(buy_cart_button, SIGNAL(clicked()), this, SLOT(buy_user_cart())); //Calls buy cart function when buy cart button is pressed
close_cart_window_button = new QPushButton("Close");
overall_layout->addWidget(close_cart_window_button); //Adds the exit cart button to the overall layout
connect(close_cart_window_button, SIGNAL(clicked()), this, SLOT(close())); //Calls the close window function when the exit cart button is pressed
remove_product_button = new QPushButton("Remove Item");
overall_layout->addWidget(remove_product_button); //Adds the remove product button to the overall layout
connect(remove_product_button, SIGNAL(clicked()), this, SLOT(remove_from_user_cart())); //Calls the remove from user cart function when remove product button is pressed
//Instantiates a copy of the user's cart from the database to fill the list up with
map<string, User*>::iterator it1 = passed_datastore.customers.find(customer_name);
if (it1 != passed_datastore.customers.end() ) {
User* client = it1->second; //Finds corresponding User* for customer
map<User*, queue<Product*> >::iterator it2 = passed_datastore.users_cart.find(client);
if (it2 != passed_datastore.users_cart.end() ) { //One already exists for each customer who presses add cart
queue<Product*> temp_cart = it2->second; //Makes a copy of the customer's cart
while (temp_cart.size() > 0) { //Don't use for loops for queues (since you're poping)!
string product_info2;
Product* item;
item = temp_cart.front();
product_info2 = item->getName();
list_of_cart_products->addItem(QString::fromStdString(product_info2)); //Adds item to list of user's cart
cart_products.push_back(item); //Pushes product* into a new temp array;
temp_cart.pop();
}
}
}
setLayout(overall_layout);
}
MainWindow::MainWindow(TaoBao& datastore, bool* not_hacker) {
ds = &datastore;
not_hack = not_hacker;
login_layout = new QVBoxLayout();
login_greeting_layout = new QHBoxLayout();
login_layout->addLayout(login_greeting_layout);
login_greeting = new QLabel("Welcome to TaoBao, China's largest online shopping marketplace");
login_greeting_layout->addWidget(login_greeting);
request_layout = new QHBoxLayout();
login_layout->addLayout(request_layout);
request = new QLabel("Please enter your login information");
request_layout->addWidget(request);
user_input_layout = new QHBoxLayout();
login_layout->addLayout(user_input_layout);
user_layout = new QVBoxLayout();
user_input_layout->addLayout(user_layout);
user_label = new QLabel("Username");
user_layout->addWidget(user_label);
username = new QLineEdit();
user_layout->addWidget(username);
password_layout = new QVBoxLayout();
user_input_layout->addLayout(password_layout);
password_label = new QLabel("Password");
password_layout->addWidget(password_label);
password = new QLineEdit();
password_layout->addWidget(password);
buttons_layout = new QHBoxLayout();
login_layout->addLayout(buttons_layout);
login_button = new QPushButton("Login");
buttons_layout->addWidget(login_button);
connect(login_button, SIGNAL(clicked()), this, SLOT(verify_user()));
quit_button = new QPushButton("Quit");
buttons_layout->addWidget(quit_button);
connect(quit_button, SIGNAL(clicked()), this, SLOT(close()));
//new_user_button = new QPushButton("New User");
//buttons_layout->addWidget(new_user_button);
setLayout(login_layout);
}
MainWindow::~MainWindow() {
}
void MainWindow::and_search() {
and_ = true;
or_ = false;
}
void MainWindow::or_search() {
and_ = false;
or_ = true;
}
void MainWindow::search_taobao() {
string user_input = search_entries->text().toStdString(); //Gets a string version of the search input
if (user_input.size() == 0) {
return; //Returns if no words entered
}
if (product_matches.size() > 0) {
while (product_matches.size() > 0) {
product_matches.pop_back(); //Erases all products in product matches vector
}
product_matches_list->clear(); //Clears all items in list of product matches from search
display_product_info->setText("");
}
if (reviews_for_current_product.size() > 0) {
while (reviews_for_current_product.size() > 0) {
reviews_for_current_product.pop_back(); //Erases all reviews in temporary review vector
}
list_of_reviews->clear(); //Clears all items in lis of review matches for previously selected product
selected_review_info->setText("");
}
vector<string> all_search_terms; //To store a vector of the individual search terms
vector<Product*> product_results; //Delete later; Use good only
stringstream ss;
ss << user_input;
string temp;
while (ss >> temp) {
temp = convToLower(temp); //Makes search term case-insensitive
all_search_terms.push_back(temp); //Pushes it to the vector of search terms
}
if (and_ == true && or_ == false) { //AND search
int type = 0;
product_results = ds->search(all_search_terms, type); //Returns all products that match the search requirements
for (unsigned int i = 0; i < product_results.size(); i++) {
product_matches.push_back(product_results[i]); //Adds product matches into separate vector for this class
product_matches_list->addItem(QString::fromStdString(product_matches[i]->getName())); //Adds product name as an item to the product list
}
}
if (and_ == false && or_ == true) { //OR search
int type = 1;
product_results = ds->search(all_search_terms, type); //Returns all products that match the search requirements
for (unsigned int i = 0; i < product_results.size(); i++) {
product_matches.push_back(product_results[i]); //Adds product matches into separate vector for this class
product_matches_list->addItem(QString::fromStdString(product_results[i]->getName())); //Adds product name as an item to the product lis
}
}
search_entries->setText(""); //Clears search entry input box to allow for next search
}
void MainWindow::display_product_information(int productIndex) {
if (productIndex == -1) {
return; //Return if no products in product list or no products currently selected
}
QString productText; //Instantiates a Qstring to hold the string of product information
productText += QString::fromStdString(product_matches[productIndex]->displayString()); //Gets the product information from the vector of product matches
display_product_info->setText(productText); //Sets the Qlabel to hold the product information QString
}
void MainWindow::get_reviews_for_product() { //Make something for no reviews
if (product_matches_list->currentRow() == -1) {
return; //Return if no products in product list or no products currently selected
}
if (reviews_for_current_product.size() > 0) {
while (reviews_for_current_product.size() > 0) {
reviews_for_current_product.pop_back(); //Erases all reviews in temporary review vector
}
list_of_reviews->clear(); //Clears all items in list of review matches for previously selected product
selected_review_info->setText(""); //Clears the QLabel holding the string for the current review
}
QListWidgetItem* current_product = product_matches_list->currentItem(); //Gets the currently selected product item from product list
string product = current_product->text().toStdString(); //Gets the name of product
map<string, vector<Review*> >::iterator it = ds->reviews_bank.find(product); //Sets iterator to vector of reviews in the database for current product
if (it != ds->reviews_bank.end()) {
for (unsigned int i = 0; i < it->second.size(); i++) {
reviews_for_current_product.push_back(it->second[i]);
}
ReviewDate comp1; //Instantiates a functor that sorts reviews by their date data member
mergeSort(reviews_for_current_product, comp1, 0, reviews_for_current_product.size()-1); //Calls mergesort along with the review functor on the temporary review vector
for (unsigned int j = 0; j < reviews_for_current_product.size(); j++) {
list_of_reviews->addItem(QString::fromStdString(reviews_for_current_product[j]->date)); //Adds the reviews to the review list according by date (sorted from most to least recent)
}
}
}
void MainWindow::display_selected_review(int reviewIndex) {
if (list_of_reviews->currentRow() == -1) {
return; //Return if no reviews in review list or no reviews currently selected
}
QString reviewText; //Instantiates a Qstring to hold the review string
reviewText = QString::fromStdString(reviews_for_current_product[reviewIndex]->displayString()); //Gets the review from the vector of reviews for current product
selected_review_info->setText(reviewText); //Sets the Qlabel to hold the review QString
}
void MainWindow::add_new_review() {
string name = user->getName();
if (date_input->text().isEmpty() || rating_input->text().isEmpty() || review_text_input->toPlainText().isEmpty()) {
return; // Return if the date, rating, or text input field is left blank
}
if (product_matches_list->currentRow() == -1) {
return; //Return if no product currently selected
}
string date = date_input->text().toStdString(); //Retrieves string from date input box
int year;
char trash1;
int month;
char trash2;
int day;
stringstream ss1;
ss1 << date;
ss1 >> year;
ss1 >> trash1;
ss1 >> month;
ss1 >> trash2;
ss1 >> day;
if (year < 1900 || year > 2015) {
date_input->setText("");
return;
}
if (month < 1 || month > 12) {
date_input->setText("");
return;
}
if (day < 1 || day > 31) {
date_input->setText("");
return;
}
string rating = rating_input->text().toStdString(); //Retrieves rating from rating input box
int score;
stringstream ss2;
ss2 << rating;
ss2 >> score; //Converts the rating into an integer
if (score < 1 || score > 5) {
rating_input->setText("");
return;
}
string review = review_text_input->toPlainText().toStdString(); //Retrieves review text from review text input box
if (review.size() == 0) {
review_text_input->setPlainText("");
return;
}
QListWidgetItem* current_product = product_matches_list->currentItem(); //Gets the currently selected product item
string product = current_product->text().toStdString(); //Gets the name of product from the product item
Review* fresh = new Review(product, score, name, date, review); //Dynamically allocates/instantiates a new review object
ds->addReview(fresh); //Adds new review to the database
ds->all_reviews.push_back(fresh); //Pushes the new review into the database's vector of all reviews
date_input->setText("");
rating_input->setText("");
review_text_input->setPlainText("");
get_reviews_for_product(); //Calls the function to add the review to the list of mergesorted reviews
}
void MainWindow::add_product_rec_to_cart() {
if (product_rec_list->count() < 1) {
return; //Returns if no products in the product list
}
vector<Product*> rec_good;
rec_good.push_back(top_product);
string username = user->getName();
ds->add_to_cart(username, 1, rec_good); //Adds the product to the user's cart
}
void MainWindow::add_user_cart() {
if (product_matches_list->count() < 1) {
return; //Returns if no products in the product recommendation list
}
string username = user->getName();
int current_row = product_matches_list->currentRow(); //Gets the index of the currently selected product
ds->add_to_cart(username, current_row+1, product_matches); //Adds the product to the user's cart
}
void MainWindow::view_user_cart() {
string username = user->getName();
MainWindow* cart = new MainWindow(*ds, username); //Instantiates a new QT window for user's cart
cart->show(); //Shows QT window
}
void MainWindow::buy_user_cart() {
if (list_of_cart_products->count() < 1) {
return; //Return if there is nothing in the user's cart
}
map<string, User*>::iterator it1 = ds->customers.find(customer_name); //Finds user's object in database
ds->buy_cart(customer_name); //Buys cart for user
while (cart_products.size() > 0) {
cart_products.pop_back(); //Deletes all products in user's temporary cart
}
list_of_cart_products->clear(); //Clears the product list of all items
map<User*, queue<Product*> >::iterator it2 = ds->users_cart.find(it1->second); //Gets user's cart from database
queue<Product*> temp2 = it2->second; //Copy of user's cart
while (temp2.size() > 0) {
list_of_cart_products->addItem(QString::fromStdString(temp2.front()->getName())); //Adds products left in user's cart to the list of user's cart products
cart_products.push_back(temp2.front()); //Pushes products to user's temporary cart
temp2.pop();
}
}
void MainWindow::remove_from_user_cart() {
if (list_of_cart_products->currentRow() == -1) {
return; //Return if nothing selected from list of user's cart products
}
int item = list_of_cart_products->currentRow(); //Gets the productIndex of item user wants to remove
cart_products.erase(cart_products.begin() + item); //Removes the product from the user's temporary cart
map<string, User*>::iterator it1 = ds->customers.find(customer_name);
if (it1 != ds->customers.end() ) {
User* client = it1->second;
map<User*, queue<Product*> >::iterator it2 = ds->users_cart.find(client);
if (it2 != ds->users_cart.end() ) {
while (it2->second.size() > 0) {
it2->second.pop(); //Deletes everything from user's cart
}
for (unsigned int i = 0; i < cart_products.size(); i++) {
it2->second.push(cart_products[i]); //Adds everything back to user's cart except for the removed product
}
}
}
delete list_of_cart_products->currentItem(); //Removes item from the list of user's cart products
}
void MainWindow::sort_by_average_rating() {
vector<Product*> temp_product_matches;
temp_product_matches = product_matches; //Copies products that match the search requirements
PointerProductRating comp1; //Instantiates a functor that sorts products by their average rating
mergeSort(temp_product_matches, comp1, 0, temp_product_matches.size()-1); //Calls mergesort on the temporary product vector
product_matches_list->clear(); //Clears all items from the list of product matches
product_matches = temp_product_matches; //Assigns mergesorted vector to vector holding product matches
for (unsigned int i = 0; i < product_matches.size(); i++) {
product_matches_list->addItem(QString::fromStdString(product_matches[i]->getName())); //Adds (in order) product to the list of product matches
}
}
void MainWindow::sort_alphabetically() {
vector<Product*> temp_product_matches;
temp_product_matches = product_matches; //Copies products that match the search requirements
PointerProductABC comp1; //Instantiates a functor that sorts products alphabetically by their name
mergeSort(temp_product_matches, comp1, 0, temp_product_matches.size()-1); //Calls mergesort on the temporary product vector
product_matches_list->clear(); //Clears all items from the list of product matches
product_matches = temp_product_matches; //Assigns mergesorted vector to vector holding product matches
for (unsigned int i = 0; i < product_matches.size(); i++) {
product_matches_list->addItem(QString::fromStdString(product_matches[i]->getName())); //Adds (in order) product to the list of product matches
}
}
void MainWindow::save_to_file() { //Do error checking for ofile
if (output_file->text().isEmpty()) {
return; //Returns if output file input box is empty
}
string file = output_file->text().toStdString();
ofstream ofile (file.c_str()); //Instantiates an output file stream
if( ofile.fail() ) { //Cannot open output file
cout << "Couldn't open file" << endl;
return;
}
ds->dump(ofile); //Dumps database into output file
ofile.close(); //Closes output file
}
/*
Additions
*/
void MainWindow::verify_user() {
string username_input = username->text().toStdString();
string password_input = password->text().toStdString();
if (username_input.size() == 0 || password_input.size() == 0 || password_input.size() > 8) {
return; //Returns if no words entered
}
for (unsigned int i = 0; i < password_input.size(); i++) {
if (iswspace(password_input[i]) ) {
incorrect_layout = new QHBoxLayout();
login_layout->addLayout(incorrect_layout);
incorrect = new QLabel("Invalid username or password. Please try again");
incorrect_layout->addWidget(incorrect);
username->setText("");
password->setText("");
return;
}
}
string user_password = ds->hash_password(password_input);
map<string, User*>::iterator it = ds->customers.find(username_input);
if (it != ds->customers.end() ) {
User* client = it->second;
string actual_password = client->getPassword();
if (user_password == actual_password) {
*not_hack = true; //Allows for main window
ds->current_user = client;
this->close();
}
else {
incorrect_layout = new QHBoxLayout();
login_layout->addLayout(incorrect_layout);
incorrect = new QLabel("Invalid username or password. Please try again");
incorrect_layout->addWidget(incorrect);
username->setText("");
password->setText("");
}
}
else {
incorrect_layout = new QHBoxLayout();
login_layout->addLayout(incorrect_layout);
incorrect = new QLabel("Invalid username or password. Please try again");
incorrect_layout->addWidget(incorrect);
username->setText("");
password->setText("");