-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.cpp
executable file
·126 lines (105 loc) · 2.63 KB
/
product.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
#include <sstream>
#include <iomanip>
#include <vector>
#include <map>
#include "review.h"
#include "product.h"
#include "msort.h"
using namespace std;
Product::Product(const std::string category, const std::string name, double price, int qty) :
name_(name),
price_(price),
qty_(qty),
category_(category),
total_ratings_(0),
num_ratings_(0)
{
}
Product::~Product()
{
}
double Product::getPrice() const
{
return price_;
}
std::string Product::getName() const
{
return name_;
}
void Product::subtractQty(int num)
{
qty_ -= num;
}
int Product::getQty() const
{
return qty_;
}
/**
* default implementation...can be overriden in a future
* assignment
*/
bool Product::isMatch(std::vector<std::string>& searchTerms) const
{
return false;
}
void Product::dump(std::ostream& os) const
{
os << category_ << "\n" << name_ << "\n" << price_ << "\n" << qty_ << endl;
}
vector<pair<User*, int>> Product::addReview(Review review) {
vector<pair<User*, int>> returnThis;
for(size_t i = 0; i < reviews_.size(); i++){
returnThis.push_back(pair<User*,int>(reviews_[i].user, reviews_[i].rating));
}
reviews_.push_back(review);
num_ratings_++;
total_ratings_ += review.rating;
return returnThis;
}
map<User*, int> Product::returnAllReviews() {
map<User*, int> returnThis;
for(size_t i = 0; i < reviews_.size(); i++){
returnThis.insert(pair<User*, int>(reviews_[i].user, reviews_[i].rating));
}
return returnThis;
}
void Product::dumpReviews(std::ostream &os) {
for(size_t i = 0; i < reviews_.size(); i++){
Review r = reviews_[i];
os << getName() << "\n" << r.rating << " " << r.username << " " << r.date << " " << r.reviewText << endl;
}
}
double Product::getAvgRating() const{
if(num_ratings_ == 0){
return 0;
}
return (double)(total_ratings_) / (double)(num_ratings_);
}
struct DateComp{
bool operator()(Review a, Review b)
{
if(a.year > b.year){
return true;
}else if(b.year > a.year){
return false;
}else{
if(a.month > b.month){
return true;
}else if(b.month > a.month){
return false;
}else{
if(a.day > b.day){
return true;
}
return false;
}
}
}
};
void Product::printReviews(){
DateComp comp;
mergeSort(reviews_, comp);
for(size_t i = 0; i < reviews_.size(); i++){
cout << reviews_[i].date << " " << reviews_[i].rating << " " << reviews_[i].username << " " << reviews_[i].reviewText << endl;
}
}