-
Notifications
You must be signed in to change notification settings - Fork 14
/
Sales_data.h
83 lines (65 loc) · 2.53 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
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <iostream>
#include <string>
#include <unordered_set>
#include <tuple>
#include <vector>
#include <stdexcept>
struct Sales_data;
Sales_data operator+(const Sales_data & sa, const Sales_data & sb);
std::istream & operator>>(std::istream & is, Sales_data & sa);
std::ostream & operator<<(std::ostream & os, const Sales_data & sa);
bool operator==(const Sales_data &lhs, const Sales_data &rhs);
bool operator!=(const Sales_data &lhs, const Sales_data &rhs);
bool compareIsbn(const Sales_data & lhs, const Sales_data & rhs);
template <typename T> class std::hash;
class Sales_data
{
friend class std::hash<Sales_data>;
friend Sales_data operator+(const Sales_data & sa, const Sales_data & sb);
friend std::istream & operator>>(std::istream & is, Sales_data & sa);
friend std::ostream & operator<<(std::ostream & os, const Sales_data & sa);
friend bool operator==(const Sales_data &lhs, const Sales_data &rhs);
public:
Sales_data() noexcept = default;
Sales_data(const std::string &s) noexcept : ISBN(s) { }
Sales_data(const std::string &s, int a, double t) noexcept : ISBN(s), amount(a), totalPrice(t * a) { }
Sales_data(std::istream &is) noexcept { is >> *this; }
std::string isbn() const { return ISBN; }
Sales_data & operator+=(const Sales_data & sa);
Sales_data & operator=(const std::string & s);
explicit operator double() const { return totalPrice; }
explicit operator std::string() const { return isbn(); }
private:
std::string ISBN;
double totalPrice = 0.0;
int amount = 0;
inline double avg_price() const;
};
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;
};
}
typedef std::tuple<std::vector<Sales_data>::size_type, std::vector<Sales_data>::const_iterator, std::vector<Sales_data>::const_iterator> matches;
std::vector<matches> findBook(const std::vector<std::vector<Sales_data>> &files, const std::string & book);
void reportResults(std::istream & in, std::ostream & os, const std::vector<std::vector<Sales_data>> &files);
class out_of_stock : public std::runtime_error
{
public:
explicit out_of_stock(const std::string &s) : std::runtime_error(s) { }
};
class isbn_mismatch : public std::logic_error
{
public:
explicit isbn_mismatch(const std::string &s) : std::logic_error(s) { }
isbn_mismatch(const std::string &s, const std::string & lhs, const std::string & rhs) : std::logic_error(s), left(lhs), right(rhs) { }
const std::string left, right;
};
#endif