summaryrefslogtreecommitdiff
path: root/source/library.cc
blob: 1ff1f7cbdaf622dd04034e7067c94ff222656c0b (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
#include <cstddef>
#include <iostream>
#include <random>
#include <string>

#include <book_store/book.hh>
#include <book_store/customer.hh>
#include <book_store/library.hh>
#include <book_store/random.hh>
#include <book_store/store.hh>
#include <book_store/utility.hh>

namespace book_store::library {
auto purchase_handler(book_store::store &store) -> void {
  using namespace book_store;
  using namespace book_store::utility;

  std::cout << "You may now purchase books. Press enter to exit.";

  for (;;) {
    std::string isbn;
    std::size_t customer_id;
    std::size_t copy_count;

    isbn =
        prompt("\n\nEnter the ISBN of the book you would like to purchase: ");

    if (isbn.empty()) {
      break;
    }

    customer_id = std::stoul(prompt("Enter your ID: "));
    copy_count =
        std::stoul(prompt("Enter the number of copies you would like to "
                          "purchase: "));

    auto book = store.find_book_by_isbn(isbn);

    if (!book.has_value()) {
      clear_cerr();

      std::cerr << "\nBook not found.";

      continue;
    }

    auto person = store.find_person_by_id(customer_id);

    if (!person.has_value()) {
      clear_cerr();

      std::cerr << "\nPerson not found.";

      continue;
    }

    auto &book_reference = book.value();
    auto purchase_book = store.purchase_book(customer_id, isbn, copy_count);

    if (purchase_book.has_value()) {
      clear_cerr();

      std::cerr << '\n' << purchase_book.value().what();

      continue;
    }

    std::cout << "\nPurchase successful.\nRemaining copies: "
              << book_reference.get().copies();
  }
}

auto populate_books(book_store::store &store,
                    std::size_t library_size) -> void {
  utility::random::book_random_engine random(
      std::mt19937(std::random_device{}()));

  for (std::size_t i = 0; i < library_size; ++i) {
    store.append_book(product::book{random.title(), random.authors(),
                                    random.publisher(), random.isbn(),
                                    random.price_usd(), random.copy_count()});
  }
}

auto populate_consumers(book_store::store &store) -> void {
  using namespace book_store;
  using namespace book_store::utility;

  std::string first_name;
  std::string last_name;
  std::size_t person_id;
  std::string member_type;

  std::cout << "Enter a person's information. Press enter for the first name "
               "field to complete data entry.\n\n";

  for (;;) {
    if (store.people_size() == store.people_max_size()) {
      clear_cerr();

      std::cerr << "Maximum number of people reached.\n";

      break;
    }

    first_name = prompt("First Name: ");

    if (first_name.empty()) {
      break;
    }

    last_name = prompt("Last Name: ");
    person_id = std::stoul(prompt("ID: "));
    member_type = prompt("Member Type (1 = person, 2 = member): ");

    if (member_type == "1") {
      store.append_customer(
          consumer::customer(first_name, last_name, person_id, false));
    } else if (member_type == "2") {
      store.append_customer(
          consumer::customer(first_name, last_name, person_id, true));
    } else {
      clear_cerr();

      std::cerr << "Invalid member type. Try again.\n";
    }

    std::cout << '\n';
  }
}

auto manage(book_store::store &store) -> void {
  using namespace book_store::utility;

  std::cout << "\nYou may now search for books by title, author, purchase by "
               "ISBN, view a person's transactions by ID, or tick a year "
               "forward. Press enter to "
               "exit.\n\n";

  for (;;) {
    std::string search_type;
    std::string search_term;

    search_type =
        prompt("Operation (title, author, purchase, transactions, tick): ");

    if (search_type.empty()) {
      break;
    }

    if (search_type == "title") {
      search_term = prompt("\nEnter the title: ");

      for (const auto &book : store.find_books_by_title(search_term)) {
        std::cout << book << '\n';
      }
    } else if (search_type == "author") {
      search_term = prompt("\nEnter the author: ");

      for (const auto &book : store.find_books_by_author(search_term)) {
        std::cout << book << '\n';
      }
    } else if (search_type == "purchase") {
      purchase_handler(store);
    } else if (search_type == "tick") {
      store.tick_year();
    } else if (search_type == "transactions") {
      for (const auto &book : store.transactions_by_id(
               std::stoul(prompt("Enter the person's ID: ")))) {
        std::cout << '\n' << book << "\n";
      }
    } else {
      clear_cerr();

      std::cerr << "Invalid search type. Try again.\n\n";
    }
  }
}
} // namespace book_store::library