-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSalesData.hpp
200 lines (147 loc) · 4.23 KB
/
SalesData.hpp
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
//
// Created by martin on 28/05/2022.
//
#include <bits/stdc++.h>
using namespace std;
class SalesData
{
public:
// constructors
SalesData() : SalesData ("", 0, 0) {}
SalesData (string book_number) : SalesData (std::move (book_number), 0, 0) {}
SalesData (string book_number, unsigned units_sold, double price)
: m_book_no (std::move (book_number)), m_units_sold (units_sold), m_revenue (price * units_sold)
{
}
SalesData (const char *ISBN) : m_book_no (ISBN) {}
explicit SalesData (istream &is) : SalesData()
{
cout << "istream constructor used" << endl;
read (is, *this);
}
// conversion operators
explicit operator string() const { return isbn(); }
// methods
SalesData &combine (const SalesData &rhs)
{
m_units_sold += rhs.units_sold();
m_revenue += rhs.revenue();
return *this;
}
// getters and setters
[[nodiscard]] const string &isbn() const { return m_book_no; }
void set_isbn (const string &book_no) { m_book_no = book_no; }
[[nodiscard]] unsigned int units_sold() const { return m_units_sold; }
void set_units_sold (unsigned int units_sold) { m_units_sold = units_sold; }
[[nodiscard]] double revenue() const { return m_revenue; }
void set_revenue (double revenue) { m_revenue = revenue; }
SalesData &operator+= (const SalesData &other)
{
if (isbn() != other.isbn())
{
throw runtime_error ("can't add different books");
}
*this = *this + other;
return *this;
}
SalesData &operator+ (const string &ISBN)
{
set_isbn (ISBN);
return *this;
}
SalesData operator+ (const SalesData &other)
{
if (this->isbn() != other.isbn())
{
throw runtime_error ("can't add different books");
}
set_isbn (other.isbn());
set_units_sold (units_sold() + other.units_sold());
set_revenue (revenue() + other.units_sold());
return *this;
}
friend bool operator== (const SalesData &lhs, const SalesData &rhs);
friend bool operator!= (const SalesData &lhs, const SalesData &rhs);
private:
[[nodiscard]] double avg_price() const
{
if (units_sold())
{
return revenue() / units_sold();
}
else
{
return 0;
}
}
std::string m_book_no;
unsigned m_units_sold {0};
double m_revenue {0.0};
// friends
friend SalesData add (const SalesData &lhs, const SalesData &rhs);
friend istream &read (istream &is, SalesData &item);
friend ostream &print (ostream &os, const SalesData &item);
};
bool operator== (const SalesData &lhs, const SalesData &rhs)
{
return lhs.m_book_no == rhs.m_book_no && lhs.m_units_sold == rhs.m_units_sold && lhs.m_revenue == rhs.m_revenue;
}
bool operator!= (const SalesData &lhs, const SalesData &rhs) { return ! (rhs == lhs); }
istream &operator>> (istream &is, SalesData &data)
{
char c1;
char c2;
string isbn;
int units_sold;
double revenue;
is >> c1 >> isbn >> units_sold >> revenue >> c2;
if (c1 != '{' || c2 != '}' || ! isalnum (isbn[0]))
{
throw runtime_error ("bad input. format needed { ISBN UNITS REVENUE }");
return is;
}
data.set_isbn (isbn);
data.set_units_sold (units_sold);
data.set_revenue (revenue);
return is;
}
ostream &operator<< (ostream &os, const SalesData &data)
{
os << "isbn: " << data.isbn() << " units_sold: " << data.units_sold() << " revenue: " << data.revenue();
return os;
}
/***
DEFINITIONS
*/
SalesData add (const SalesData &lhs, const SalesData &rhs)
{
SalesData sum = lhs;
sum.combine (rhs);
return sum;
}
istream &read (istream &is, SalesData &item)
{
double price {0};
is >> item.m_book_no >> item.m_units_sold >> price;
item.m_revenue = item.m_units_sold * price;
return is;
}
ostream &print (ostream &os, const SalesData &item)
{
os << item.isbn() << " " << item.units_sold() << " " << item.revenue() << " " << item.avg_price();
return os;
}
// hash function for unordered multiset.
namespace std
{
template <> struct hash<SalesData>
{
typedef size_t result_type;
typedef SalesData argument_type;
size_t operator() (const SalesData &s) const;
};
size_t hash<SalesData>::operator() (const SalesData &s) const
{
return hash<string>() (s.isbn()) ^ hash<unsigned>() (s.units_sold()) ^ hash<double>() (s.revenue());
}
} // namespace std