diff options
Diffstat (limited to 'source/library.cc')
| -rw-r--r-- | source/library.cc | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/source/library.cc b/source/library.cc new file mode 100644 index 0000000..1ff1f7c --- /dev/null +++ b/source/library.cc @@ -0,0 +1,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 |