summaryrefslogtreecommitdiff
path: root/source/store.cc
blob: 528e35875b011e07e38ea14ded8bbececd2be233 (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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <algorithm>
#include <cctype>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include <book_store/book.hh>
#include <book_store/book_count.hh>
#include <book_store/customer.hh>
#include <book_store/price.hh>
#include <book_store/purchase_error.hh>
#include <book_store/store.hh>

namespace book_store {
auto store::append_book(const product::book &book) -> void {
  if (this->books_max_size() != 0 &&
      this->_books.size() < this->books_max_size()) {
    this->_books.push_back(book);
  }
}

auto store::append_customer(const customer_type &person) -> void {
  if (this->people_max_size() != 0 &&
      this->_customers.size() < this->people_max_size()) {
    this->_customers.push_back(person);
  }
}

auto store::purchase_book(size_type person_id, std::string_view isbn,
                          product::book_count count)
    -> std::optional<purchase_error> {
  auto person = this->find_person_by_id(person_id);

  if (!person) {
    return purchase_error(
        purchase_error::purchase_error_type::person_not_found);
  }

  auto book = this->find_book_by_isbn(isbn);

  if (!book) {
    return purchase_error(purchase_error::purchase_error_type::book_not_found);
  }

  if (book->get().copies() < count) {
    return purchase_error(
        purchase_error::purchase_error_type::not_enough_stock);
  }

  auto &customer = *person;
  auto book_price = book.value().get().price_usd();

  if (customer.is_member()) {
    auto discount = 1.0 - (this->_membership_discount_percent * 0.01);

    book_price =
        static_cast<product::price::usd::price_type>(book_price * discount);

    if (customer.books_bought() % 11 == 0) {
      auto transaction_count = 0;
      auto transaction_total = 0.0;

      for (auto &transaction : this->_transactions) {
        if (transaction.first == person_id) {
          transaction_total += transaction.second.price_usd();
          transaction_count += 1;
        }
      }

      if (transaction_count >= 10) {
        book_price = (transaction_total / transaction_count) * discount;
      }

      customer.amount_spent(0);
    }
  }

  customer.books_bought(customer.books_bought() + count);
  // There's some fun casting going on here. It's all safe, though ... probably.
  customer.amount_spent(customer.amount_spent() +
                        static_cast<product::price::usd::price_type>(
                            (static_cast<product::book_count::book_count_type>(
                                 book_price.value()) *
                             count.value())));
  book.value().get().copies(book.value().get().copies() - count);
  this->_transactions.emplace_back(
      person_id, product::book{book.value().get().title().data(),
                               book.value().get().authors(),
                               book.value().get().publisher().data(),
                               book.value().get().isbn().data(), book_price,
                               product::book_count(count)});

  return std::nullopt;
}

auto store::tick_year() -> void {
  for (auto &customer : this->_customers) {
    customer.amount_spent(customer.amount_spent() + this->_membership_fee);
  }
}

auto store::membership_fee(product::price::usd fee) -> void {
  this->_membership_fee = fee;
}

auto store::membership_discount_percent(discount_percent_type discount_percent)
    -> void {
  this->_membership_discount_percent = discount_percent;
}

auto store::books_max_size() const noexcept -> size_type {
  return this->_books_max_size;
}

auto store::people_max_size() const noexcept -> size_type {
  return this->_customers_max_size;
}

auto store::books_size() const noexcept -> size_type {
  return this->_books.size();
}

auto store::people_size() const noexcept -> size_type {
  return this->_customers.size();
}

auto store::membership_fee() const noexcept -> product::price::usd {
  return this->_membership_fee;
}

auto store::membership_discount_percent() const noexcept
    -> discount_percent_type {
  return this->_membership_discount_percent;
}

auto store::find_person_by_id(size_type person_id)
    -> std::optional<customer_type> {
  for (auto &person : this->_customers) {
    if (person.id() == person_id) {
      return person;
    }
  }

  return std::nullopt;
}

auto store::find_book_by_isbn(std::string_view isbn)
    -> std::optional<std::reference_wrapper<product::book>> {
  for (auto &book : this->_books) {
    std::string book_isbn = book.isbn().data();
    std::string search_isbn = isbn.data();

    std::transform(
        book_isbn.begin(), book_isbn.end(), book_isbn.begin(),
        [](unsigned char character) { return std::tolower(character); });
    std::transform(
        search_isbn.begin(), search_isbn.end(), search_isbn.begin(),
        [](unsigned char character) { return std::tolower(character); });

    if (book_isbn == search_isbn) {
      return book;
    }
  }

  return std::nullopt;
}

auto store::find_books_by_title(std::string_view title)
    -> std::vector<product::book> {
  std::vector<product::book> books;

  for (auto &book : this->_books) {
    std::string book_title = book.title().data();
    std::string search_title = title.data();

    std::transform(
        book_title.begin(), book_title.end(), book_title.begin(),
        [](unsigned char character) { return std::tolower(character); });
    std::transform(
        search_title.begin(), search_title.end(), search_title.begin(),
        [](unsigned char character) { return std::tolower(character); });

    if (book_title.find(search_title) != std::string::npos) {
      books.push_back(book);
    }
  }

  return books;
}

auto store::find_books_by_author(std::string_view name)
    -> std::vector<product::book> {
  std::vector<product::book> books;

  for (auto &book : this->_books) {
    for (auto &author : book.authors()) {
      std::string author_name = author.full_name();
      std::string search_name = name.data();

      std::transform(
          author_name.begin(), author_name.end(), author_name.begin(),
          [](unsigned char character) { return std::tolower(character); });
      std::transform(
          search_name.begin(), search_name.end(), search_name.begin(),
          [](unsigned char character) { return std::tolower(character); });

      if (author_name.find(search_name) != std::string::npos) {
        books.push_back(book);
      }
    }
  }

  return books;
}

auto store::transactions_by_id(size_type person_id)
    -> std::vector<product::book> {
  std::vector<product::book> books;

  for (auto &transaction : this->_transactions) {
    if (transaction.first == person_id) {
      books.push_back(transaction.second);
    }
  }

  return books;
}

auto store::books() -> books_type & { return this->_books; }

auto store::people() -> customer_container & { return this->_customers; }
} // namespace book_store