-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sales_data.h
95 lines (77 loc) · 3.17 KB
/
Sales_data.h
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
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <string>
#include <iostream>
#include <functional>
#include "Sales_except.h"
class Sales_data;
std::istream &read(std::istream&, Sales_data&);
class Sales_data {
friend std::istream &read(std::istream&, Sales_data&);
friend std::ostream &print(std::ostream&, const Sales_data&);
friend Sales_data add(const Sales_data&, const Sales_data&);
friend std::ostream &operator<<(std::ostream &, const Sales_data &);
friend std::istream &operator>>(std::istream &, Sales_data &);
friend Sales_data operator+(const Sales_data &, const Sales_data &);
friend Sales_data operator-(const Sales_data &, const Sales_data &);
friend Sales_data operator*(const Sales_data &, const Sales_data &);
friend Sales_data operator/(const Sales_data &, const Sales_data &);
friend bool operator==(const Sales_data &, const Sales_data &);
friend struct std::hash<Sales_data>;
friend std::vector<Sales_data>::iterator first_greater(std::vector<Sales_data> &, double);
friend std::vector<Sales_data>::const_iterator first_greater(const std::vector<Sales_data> &, double);
public:
Sales_data(const std::string &s, unsigned n, double p) : bookNo(s), units_sold(n), revenue(p * n) {
//std::cout << "mainConstructor" << std::endl;
}
Sales_data() : Sales_data("", 0, 0) {
//std::cout << "empty" << std::endl;
}
Sales_data(const std::string &s) : Sales_data(s, 0, 0) {
//std::cout << "string" << std::endl;
}
Sales_data(std::istream &is) : Sales_data() {
read(is, *this);
//std::cout << "istream" << std::endl;
}
Sales_data &combine(const Sales_data&);
std::string isbn() const { return bookNo; }
Sales_data &operator+=(const Sales_data &); // compound-assignment
Sales_data &operator=(const std::string &); // copy-assignment operator
explicit operator std::string() const { return bookNo; }
explicit operator double() const { return revenue; }
static const std::string Sales_data::*bookNo_pointer() { return &Sales_data::bookNo; }
private:
double avg_price() const;
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
std::istream &operator>>(std::istream &, Sales_data &); // input operator
std::ostream &operator<<(std::ostream &, const Sales_data &); // output operator
Sales_data operator+(const Sales_data &, const Sales_data &); // addition operator
Sales_data operator-(const Sales_data &, const Sales_data &); // subtraction operator
Sales_data operator*(const Sales_data &, const Sales_data &); // multiplication operator
Sales_data operator/(const Sales_data &, const Sales_data &); // division operator
Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
// inline and constexpr functions should be defined in the class headers
inline
double Sales_data::avg_price() const {
return units_sold ? revenue / units_sold : 0;
}
namespace std {
template <>
struct hash<Sales_data> {
typedef size_t result_type;
typedef Sales_data argument_type;
size_t operator()(const Sales_data &s) const {
return hash<string>()(s.bookNo) ^
hash<unsigned>()(s.units_sold) ^
hash<double>()(s.revenue);
}
hash() = default;
hash &operator=(const hash &) = default;
};
}
#endif