summaryrefslogtreecommitdiff
path: root/include/book_store/price.hh
blob: 223b1e944182c83aee2431e149c0837e22294e81 (plain) (blame)
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
#ifndef PRICE_HH
#define PRICE_HH

#include <ostream>
#include <string>

namespace book_store::product::price {
class usd {
public:
  using price_type = double;

private:
  price_type _price;

public:
  usd() = default;
  usd(price_type price) : _price(price) {}
  usd(const std::string &price) : _price(std::stod(price)) {}

  [[nodiscard]] auto value() const noexcept -> price_type;

  auto operator=(const std::string &value) -> usd & {
    _price = std::stod(value);

    return *this;
  }

  friend auto operator<<(std::ostream &output_stream,
                         const usd &price) -> std::ostream & {
    output_stream << price._price;

    return output_stream;
  }

  friend auto operator==(const usd &lhs, const usd &rhs) -> bool {
    return std::abs(lhs._price - rhs._price) < 0.0001;
  }

  friend auto operator+(const usd &lhs, const usd &rhs) -> usd {
    return {lhs._price + rhs._price};
  }

  friend auto operator*(const usd &lhs, const usd &rhs) -> bool {
    return std::abs(lhs._price * rhs._price) < 0.0001;
  }

  friend auto operator+=(const usd &lhs, const usd &rhs) -> bool {
    return std::abs(lhs._price + rhs._price) < 0.0001;
  }
};
} // namespace book_store::product::price

#endif // PRICE_HH