summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-05-26 18:01:25 -0700
committerFuwn <[email protected]>2024-05-26 18:01:25 -0700
commitfa3595d69f817ad1a67b7189ed65dbde1ba9f77d (patch)
treeae85e0ba5f132ce89021cc1e4bcea4e642ffee6c /source
downloadcst_136_assignment_eight-fa3595d69f817ad1a67b7189ed65dbde1ba9f77d.tar.xz
cst_136_assignment_eight-fa3595d69f817ad1a67b7189ed65dbde1ba9f77d.zip
feat: initial commit
Diffstat (limited to 'source')
-rw-r--r--source/book.cc102
-rw-r--r--source/main.cc99
-rw-r--r--source/member.cc23
-rw-r--r--source/person.cc43
-rw-r--r--source/random.cc87
5 files changed, 354 insertions, 0 deletions
diff --git a/source/book.cc b/source/book.cc
new file mode 100644
index 0000000..6b0bd6a
--- /dev/null
+++ b/source/book.cc
@@ -0,0 +1,102 @@
+#include <cstddef>
+#include <ostream>
+#include <string_view>
+#include <utility>
+#include <vector>
+
+#include <book_store/book.hh>
+#include <book_store/person.hh>
+#include <book_store/price.hh>
+
+namespace book_store::product {
+using namespace consumer;
+
+auto book::title() const noexcept -> std::string_view { return this->_title; }
+
+auto book::authors() const noexcept -> std::vector<class person> {
+ return this->_authors;
+}
+
+auto book::publisher() const noexcept -> std::string_view {
+ return this->_publisher;
+}
+
+auto book::isbn() const noexcept -> std::string_view { return this->_isbn; }
+
+auto book::price_usd() const noexcept -> price::usd { return this->_price_usd; }
+
+auto book::copies() const noexcept -> book::size_type { return this->_copies; }
+
+auto book::title(std::string_view title) noexcept -> book & {
+ this->_title = title;
+
+ return *this;
+}
+
+auto book::authors(std::vector<class person> authors) -> book & {
+ this->_authors = std::move(authors);
+
+ return *this;
+}
+
+auto book::author(const class person &author) -> book & {
+ while (this->_authors.size() < 4) {
+ this->_authors.emplace_back(author);
+
+ break;
+ }
+
+ return *this;
+}
+
+auto book::remove_author_by_id(std::size_t author_id) -> book & {
+ this->_authors.erase(
+ this->_authors.begin() +
+ static_cast<std::vector<class person>::difference_type>(author_id));
+
+ return *this;
+}
+
+auto book::publisher(std::string_view publisher) noexcept -> book & {
+ this->_publisher = publisher;
+
+ return *this;
+}
+
+auto book::isbn(std::string_view isbn) noexcept -> book & {
+ this->_isbn = isbn;
+
+ return *this;
+}
+
+auto book::price_usd(price::usd price) noexcept -> book & {
+ this->_price_usd = price;
+
+ return *this;
+}
+
+auto book::copies(book::size_type copies) noexcept -> book & {
+ this->_copies = copies;
+
+ return *this;
+}
+
+auto operator<<(std::ostream &output_stream, const book &book)
+ -> std::ostream & {
+ output_stream << "Title: " << book._title << '\n';
+ output_stream << "Authors: ";
+
+ for (const auto &author : book._authors) {
+ output_stream << author.full_name()
+ << ((&author == &book._authors.back()) ? "" : ", ");
+ }
+
+ output_stream << '\n';
+ output_stream << "Publisher: " << book._publisher << '\n';
+ output_stream << "ISBN: " << book._isbn << '\n';
+ output_stream << "Price: $" << book._price_usd << '\n';
+ output_stream << "Copies: " << book._copies << '\n';
+
+ return output_stream;
+}
+} // namespace book_store::product
diff --git a/source/main.cc b/source/main.cc
new file mode 100644
index 0000000..5d262f7
--- /dev/null
+++ b/source/main.cc
@@ -0,0 +1,99 @@
+#include <algorithm>
+#include <array>
+#include <cstdlib>
+#include <exception>
+#include <iostream>
+#include <random>
+#include <ranges>
+#include <string>
+#include <string_view>
+
+#include <book_store/book.hh>
+#include <book_store/member.hh>
+#include <book_store/random.hh>
+
+auto main() -> int {
+ using namespace book_store;
+
+ std::array<product::book, 100> books;
+ std::array<std::string, 100> book_titles;
+ std::array<consumer::member, 100> members;
+ std::array<std::size_t, 100> member_ids;
+ std::mt19937 random_number_generator(std::random_device{}());
+ utility::random::book_random_engine random(random_number_generator);
+ auto perform = [](std::string_view name, auto action) {
+ std::cout << name << " ...";
+
+ action();
+
+ std::cout << " ok.\n";
+ };
+ auto clear_cerr = []() {
+ if (!std::cerr.good()) {
+ std::cerr.clear();
+ }
+ };
+
+ perform("populating books and book_titles", [&]() {
+ for (auto [index, book] : std::ranges::views::enumerate(books)) {
+ auto random_title = random.title();
+
+ book_titles[static_cast<std::size_t>(index)] = random_title;
+ book =
+ product::book{random_title, random.authors(), random.publisher(),
+ random.isbn(), random.price_usd(), random.copy_count()};
+ }
+ });
+ perform("verifying all books are present", [&]() {
+ for (const auto &title : book_titles) {
+ if (std::ranges::find(book_titles, title) == book_titles.end()) {
+ clear_cerr();
+
+ std::cerr << "error: title not found" << '\n';
+
+ std::terminate();
+ }
+ }
+ });
+ perform("verifying book copy count increment", [&]() {
+ for (int i = 0; i < 100; ++i) {
+ auto &book = books[std::uniform_int_distribution<std::size_t>(
+ 0, books.size() - 1)(random_number_generator)];
+ auto copy_count = book.copies();
+ auto random_copy_count = static_cast<product::book::size_type>(
+ std::uniform_int_distribution<std::size_t>(0, 100)(
+ random_number_generator));
+
+ book.copies(copy_count + random_copy_count);
+
+ if (book.copies() != copy_count + random_copy_count) {
+ clear_cerr();
+
+ std::cerr << "error: invalid copy count after increment" << '\n';
+
+ std::terminate();
+ }
+ }
+ });
+ perform("populating members and member_ids", [&]() {
+ for (auto [index, member] : std::views::enumerate(members)) {
+ auto random_name = random.name();
+ auto random_surname = random.name();
+ auto random_id = random.id();
+
+ member_ids[static_cast<std::size_t>(index)] = random_id;
+ member = consumer::member{random_name, random_surname, random_id};
+ }
+ });
+ perform("verifying all members are present", [&]() {
+ for (auto member_id : member_ids) {
+ if (std::ranges::find(member_ids, member_id) == member_ids.end()) {
+ std::cerr << "error: id not found" << '\n';
+
+ std::terminate();
+ }
+ }
+ });
+
+ return 0;
+}
diff --git a/source/member.cc b/source/member.cc
new file mode 100644
index 0000000..0e8a41c
--- /dev/null
+++ b/source/member.cc
@@ -0,0 +1,23 @@
+#include <book_store/book.hh>
+#include <book_store/member.hh>
+#include <book_store/price.hh>
+
+namespace book_store::consumer {
+using namespace product;
+
+auto member::books_bought() const noexcept -> book::size_type {
+ return this->_books_bought;
+}
+
+auto member::amount_spent() const noexcept -> price::usd {
+ return this->_amount_spent;
+}
+
+auto member::books_bought(book::size_type books_bought) noexcept -> void {
+ this->_books_bought = books_bought;
+}
+
+auto member::amount_spent(price::usd amount_spent) noexcept -> void {
+ this->_amount_spent = amount_spent;
+}
+} // namespace book_store::consumer
diff --git a/source/person.cc b/source/person.cc
new file mode 100644
index 0000000..e80e6df
--- /dev/null
+++ b/source/person.cc
@@ -0,0 +1,43 @@
+#include <cstddef>
+#include <string>
+#include <string_view>
+
+#include <book_store/person.hh>
+
+namespace book_store::consumer {
+auto person::last_name() const noexcept -> std::string_view {
+ return this->_last_name;
+}
+
+auto person::first_name() const noexcept -> std::string_view {
+ return this->_first_name;
+}
+
+auto person::full_name(bool last_first) const noexcept -> std::string {
+ if (last_first) {
+ return this->_last_name + ", " + this->_first_name;
+ }
+
+ return this->_first_name + " " + this->_last_name;
+}
+
+auto person::id() const noexcept -> std::size_t { return this->_id; }
+
+auto person::last_name(std::string_view last_name) noexcept -> person & {
+ this->_last_name = last_name;
+
+ return *this;
+}
+
+auto person::first_name(std::string_view first_name) noexcept -> person & {
+ this->_first_name = first_name;
+
+ return *this;
+}
+
+auto person::id(std::size_t person_id) noexcept -> person & {
+ this->_id = person_id;
+
+ return *this;
+}
+} // namespace book_store::consumer
diff --git a/source/random.cc b/source/random.cc
new file mode 100644
index 0000000..d237b9f
--- /dev/null
+++ b/source/random.cc
@@ -0,0 +1,87 @@
+#include <cstddef>
+#include <random>
+#include <string>
+#include <vector>
+
+#include <book_store/book.hh>
+#include <book_store/person.hh>
+#include <book_store/random.hh>
+
+namespace book_store::utility::random {
+auto book_random_engine::title() -> std::string {
+ static std::uniform_int_distribution<> distrubtion{0, 49};
+ std::string book_name;
+
+ for (int i = 0; i < 3; ++i) {
+ book_name += book_title_parts[static_cast<std::size_t>(
+ distrubtion(this->_random_number_generator))];
+ book_name += ' ';
+ }
+
+ book_name.pop_back();
+
+ return book_name;
+}
+
+auto book_random_engine::name() -> std::string {
+ static std::uniform_int_distribution<> distrubtion{0, 49};
+
+ return std::string(names[static_cast<std::size_t>(
+ distrubtion(this->_random_number_generator))]);
+}
+
+auto book_random_engine::author() -> consumer::person {
+ return consumer::person{this->name(), this->name(), this->id()};
+}
+
+auto book_random_engine::authors() -> std::vector<consumer::person> {
+ std::vector<consumer::person> authors;
+ static std::uniform_int_distribution<> distrubtion{1, 4};
+ auto author_count = distrubtion(this->_random_number_generator);
+
+ authors.reserve(static_cast<std::size_t>(author_count));
+
+ for (int i = 0; i < author_count; ++i) {
+ authors.push_back(this->author());
+ }
+
+ return authors;
+}
+
+auto book_random_engine::publisher() -> std::string {
+ static std::uniform_int_distribution<> distrubtion{0, 9};
+
+ return std::string(publishers[static_cast<std::size_t>(
+ distrubtion(this->_random_number_generator))]);
+}
+
+auto book_random_engine::isbn() -> std::string {
+ static std::uniform_int_distribution<> distrubtion{0, 9};
+ std::string isbn;
+
+ for (int i = 0; i < 13; ++i) {
+ isbn += std::to_string(distrubtion(this->_random_number_generator));
+ }
+
+ return isbn;
+}
+
+auto book_random_engine::price_usd() -> double {
+ static std::uniform_real_distribution<> distrubtion{0.0, 100.0};
+
+ return distrubtion(this->_random_number_generator);
+}
+
+auto book_random_engine::copy_count() -> product::book::size_type {
+ static std::uniform_int_distribution<> distrubtion{0, 10000};
+
+ return static_cast<product::book::size_type::book_count_type>(
+ distrubtion(this->_random_number_generator));
+}
+
+auto book_random_engine::id() -> std::size_t {
+ static std::uniform_int_distribution<> distrubtion{0, 1000000};
+
+ return static_cast<std::size_t>(distrubtion(this->_random_number_generator));
+}
+} // namespace book_store::utility::random