From 9d2792782da945fbeb8313a7b8e6ae79717c0d82 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 26 Sep 2023 22:00:33 -0700 Subject: fix(review_program): use style guide --- week_1/review_program/build.ninja | 6 +-- week_1/review_program/review_program.cc | 88 -------------------------------- week_1/review_program/review_program.cpp | 88 ++++++++++++++++++++++++++++++++ week_1/review_program/review_program.h | 13 +++++ week_1/review_program/review_program.hh | 13 ----- week_1/review_program/textbook.cc | 88 -------------------------------- week_1/review_program/textbook.cpp | 88 ++++++++++++++++++++++++++++++++ week_1/review_program/textbook.h | 62 ++++++++++++++++++++++ week_1/review_program/textbook.hh | 62 ---------------------- 9 files changed, 254 insertions(+), 254 deletions(-) delete mode 100644 week_1/review_program/review_program.cc create mode 100644 week_1/review_program/review_program.cpp create mode 100644 week_1/review_program/review_program.h delete mode 100644 week_1/review_program/review_program.hh delete mode 100644 week_1/review_program/textbook.cc create mode 100644 week_1/review_program/textbook.cpp create mode 100644 week_1/review_program/textbook.h delete mode 100644 week_1/review_program/textbook.hh diff --git a/week_1/review_program/build.ninja b/week_1/review_program/build.ninja index ad36d8a..a796752 100644 --- a/week_1/review_program/build.ninja +++ b/week_1/review_program/build.ninja @@ -12,10 +12,10 @@ rule link command = $cc $ldflags $in -o $out rule clang_format - command = clang-format -i -style=LLVM $src_dir/*$cc_ext $src_dir/*.hh + command = clang-format -i -style=LLVM $src_dir/*.cpp $src_dir/*.h -build $out_dir/$name.o: compile $src_dir/$name.cc -build $out_dir/textbook.o: compile $src_dir/textbook.cc +build $out_dir/$name.o: compile $src_dir/$name.cpp +build $out_dir/textbook.o: compile $src_dir/textbook.cpp build $out_dir/$name: link $out_dir/$name.o $out_dir/textbook.o diff --git a/week_1/review_program/review_program.cc b/week_1/review_program/review_program.cc deleted file mode 100644 index 61317ad..0000000 --- a/week_1/review_program/review_program.cc +++ /dev/null @@ -1,88 +0,0 @@ -#include "review_program.hh" -#include "textbook.hh" - -#include -#include - -auto main() -> int { - bool run_again = false; - std::vector textbooks; - - // Loop until user is done entering textbooks - do { - review_program::textbook_input_loop_handler(textbooks); - - std::cout << "Enter 1 to do another book, 0 to stop. "; - std::cin >> run_again; - } while (run_again); - - // Print full purchase summary - review_program::full_purchase_summary(textbooks); - - return 0; -} - -namespace review_program { -auto textbook_input(textbook::Textbook &textbook) -> void { - // Obtain and set all non-evaluated values for textbook - textbook.set_code(input_textbook_value("Book")); - textbook.set_single_copy_price(input_textbook_value("Price")); - textbook.set_number_on_hand(input_textbook_value("Number on hand")); - textbook.set_prospective_enrollment( - input_textbook_value("Enrollment")); - textbook.set_required( - input_textbook_value("Required (1 = required, 0 = optional)")); - textbook.set_used(input_textbook_value("Used (1 = used, 0 = new)")); -} - -template -auto input_textbook_value(const std::string &message) -> T { - // Helper for repetitive textbook value prompts - T value = 0; - - std::cout << message << ": "; - std::cin >> value; - - return value; -} - -auto textbook_input_loop_handler(std::vector &textbooks) - -> void { - // 1. Input textbook - // 2. Print textbook - // 3. Evaluate textbook needs - // 4. Print textbook needs - // 5. Add textbook to running count - textbook::Textbook textbook; - - review_program::textbook_input(textbook); - - std::cout << "---" << std::endl; - - textbook.print(); - - std::cout << "---" << std::endl; - - textbook.evaluate_needs(); - textbook.print_needs(); - - std::cout << "---" << std::endl; - - textbooks.push_back(textbook); -} - -auto full_purchase_summary(const std::vector &textbooks) - -> void { - double total_for_all_books = 0.0; - - // Calculate total cost for all books - for (const auto &textbook : textbooks) { - total_for_all_books += textbook.total_cost(); - } - - // Print total cost for all books and expected profits - std::cout << "\n---\nTotal cost for all books: $" << total_for_all_books - << std::endl; - std::cout << "Expected profit: $" << total_for_all_books * 0.2 << std::endl; -} -} // namespace review_program diff --git a/week_1/review_program/review_program.cpp b/week_1/review_program/review_program.cpp new file mode 100644 index 0000000..a634076 --- /dev/null +++ b/week_1/review_program/review_program.cpp @@ -0,0 +1,88 @@ +#include "review_program.h" +#include "textbook.h" + +#include +#include + +auto main() -> int { + bool run_again = false; + std::vector textbooks; + + // Loop until user is done entering textbooks + do { + review_program::textbook_input_loop_handler(textbooks); + + std::cout << "Enter 1 to do another book, 0 to stop. "; + std::cin >> run_again; + } while (run_again); + + // Print full purchase summary + review_program::full_purchase_summary(textbooks); + + return 0; +} + +namespace review_program { +auto textbook_input(textbook::Textbook &textbook) -> void { + // Obtain and set all non-evaluated values for textbook + textbook.set_code(input_textbook_value("Book")); + textbook.set_single_copy_price(input_textbook_value("Price")); + textbook.set_number_on_hand(input_textbook_value("Number on hand")); + textbook.set_prospective_enrollment( + input_textbook_value("Enrollment")); + textbook.set_required( + input_textbook_value("Required (1 = required, 0 = optional)")); + textbook.set_used(input_textbook_value("Used (1 = used, 0 = new)")); +} + +template +auto input_textbook_value(const std::string &message) -> T { + // Helper for repetitive textbook value prompts + T value = 0; + + std::cout << message << ": "; + std::cin >> value; + + return value; +} + +auto textbook_input_loop_handler(std::vector &textbooks) + -> void { + // 1. Input textbook + // 2. Print textbook + // 3. Evaluate textbook needs + // 4. Print textbook needs + // 5. Add textbook to running count + textbook::Textbook textbook; + + review_program::textbook_input(textbook); + + std::cout << "---" << std::endl; + + textbook.print(); + + std::cout << "---" << std::endl; + + textbook.evaluate_needs(); + textbook.print_needs(); + + std::cout << "---" << std::endl; + + textbooks.push_back(textbook); +} + +auto full_purchase_summary(const std::vector &textbooks) + -> void { + double total_for_all_books = 0.0; + + // Calculate total cost for all books + for (const auto &textbook : textbooks) { + total_for_all_books += textbook.total_cost(); + } + + // Print total cost for all books and expected profits + std::cout << "\n---\nTotal cost for all books: $" << total_for_all_books + << std::endl; + std::cout << "Expected profit: $" << total_for_all_books * 0.2 << std::endl; +} +} // namespace review_program diff --git a/week_1/review_program/review_program.h b/week_1/review_program/review_program.h new file mode 100644 index 0000000..45fe940 --- /dev/null +++ b/week_1/review_program/review_program.h @@ -0,0 +1,13 @@ +#ifndef REVIEW_PROGRAM_HH +#define REVIEW_PROGRAM_HH +#include "textbook.h" +#include +#include + +namespace review_program { +auto textbook_input(textbook::Textbook &) -> void; +template auto input_textbook_value(const std::string &) -> T; +auto textbook_input_loop_handler(std::vector &) -> void; +auto full_purchase_summary(const std::vector &) -> void; +} // namespace review_program +#endif diff --git a/week_1/review_program/review_program.hh b/week_1/review_program/review_program.hh deleted file mode 100644 index 41a9dea..0000000 --- a/week_1/review_program/review_program.hh +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef REVIEW_PROGRAM_HH -#define REVIEW_PROGRAM_HH -#include "textbook.hh" -#include -#include - -namespace review_program { -auto textbook_input(textbook::Textbook &) -> void; -template auto input_textbook_value(const std::string &) -> T; -auto textbook_input_loop_handler(std::vector &) -> void; -auto full_purchase_summary(const std::vector &) -> void; -} // namespace review_program -#endif diff --git a/week_1/review_program/textbook.cc b/week_1/review_program/textbook.cc deleted file mode 100644 index c5caffb..0000000 --- a/week_1/review_program/textbook.cc +++ /dev/null @@ -1,88 +0,0 @@ -#include "textbook.hh" - -#include -#include - -namespace textbook { -auto Textbook::code() const -> uint64_t { return this->_code; } -auto Textbook::single_copy_price() const -> double { - return this->_single_copy_price; -} -auto Textbook::number_on_hand() const -> uint64_t { - return this->_number_on_hand; -} -auto Textbook::prospective_enrollment() const -> uint64_t { - return this->_prospective_enrollment; -} -auto Textbook::required() const -> bool { return this->_required; } -auto Textbook::used() const -> bool { return this->_used; } -auto Textbook::amount_to_order() const -> uint64_t { - return this->_amount_to_order; -} -auto Textbook::total_cost() const -> double { return this->_total_cost; } - -auto Textbook::set_code(uint64_t code) -> void { this->_code = code; } -auto Textbook::set_single_copy_price(double single_copy_price) -> void { - this->_single_copy_price = single_copy_price; -} -auto Textbook::set_number_on_hand(uint64_t number_on_hand) -> void { - this->_number_on_hand = number_on_hand; -} -auto Textbook::set_prospective_enrollment(uint64_t prospective_enrollment) - -> void { - this->_prospective_enrollment = prospective_enrollment; -} -auto Textbook::set_required(bool required) -> void { - this->_required = required; -} -auto Textbook::set_used(bool used) -> void { this->_used = used; } - -auto Textbook::print() const -> void { - // Pretty print all values, minus evaluated values - std::cout << "Book: " << this->_code << "\nPrice: $" - << this->_single_copy_price - << "\nInventory: " << this->_number_on_hand - << "\nEnrollment: " << this->_prospective_enrollment - << "\nRequired: " << (this->_required ? "Yes" : "No") - << "\nUsed: " << (this->_used ? "Yes" : "No") << std::endl; -} - -auto Textbook::evaluate_needs() -> void { - /// Calculate the total cost of the books to order based on an input factor - const auto set_amount_to_order = [this](double factor) -> void { - this->_amount_to_order = - static_cast( - static_cast(this->_prospective_enrollment) * factor) - - this->_number_on_hand; - }; - - // Determine amount to order based on given percentages - if (this->_required && !this->_used) { - set_amount_to_order(0.9); - } else if (this->_required && this->_used) { - set_amount_to_order(0.65); - } else if (!this->_required && !this->_used) { - set_amount_to_order(0.4); - } else if (!this->_required && this->_used) { - set_amount_to_order(0.2); - } - - // Calculate total cost - this->_total_cost = - static_cast(this->_amount_to_order) * this->_single_copy_price; -} - -auto Textbook::print_needs() const -> void { - // Pretty print the amount to order and total cost - std::cout << "Need to order: " << this->_amount_to_order << "\nTotal cost: $" - << std::fixed << std::setprecision(2) << this->_total_cost - << std::endl; -} - -auto Textbook::operator==(const Textbook &other) const -> bool { - return this->_code == other._code; -} -auto Textbook::operator!=(const Textbook &other) const -> bool { - return this->_code != other._code; -} -} // namespace textbook diff --git a/week_1/review_program/textbook.cpp b/week_1/review_program/textbook.cpp new file mode 100644 index 0000000..dfca4cd --- /dev/null +++ b/week_1/review_program/textbook.cpp @@ -0,0 +1,88 @@ +#include "textbook.h" + +#include +#include + +namespace textbook { +auto Textbook::code() const -> uint64_t { return this->_code; } +auto Textbook::single_copy_price() const -> double { + return this->_single_copy_price; +} +auto Textbook::number_on_hand() const -> uint64_t { + return this->_number_on_hand; +} +auto Textbook::prospective_enrollment() const -> uint64_t { + return this->_prospective_enrollment; +} +auto Textbook::required() const -> bool { return this->_required; } +auto Textbook::used() const -> bool { return this->_used; } +auto Textbook::amount_to_order() const -> uint64_t { + return this->_amount_to_order; +} +auto Textbook::total_cost() const -> double { return this->_total_cost; } + +auto Textbook::set_code(uint64_t code) -> void { this->_code = code; } +auto Textbook::set_single_copy_price(double single_copy_price) -> void { + this->_single_copy_price = single_copy_price; +} +auto Textbook::set_number_on_hand(uint64_t number_on_hand) -> void { + this->_number_on_hand = number_on_hand; +} +auto Textbook::set_prospective_enrollment(uint64_t prospective_enrollment) + -> void { + this->_prospective_enrollment = prospective_enrollment; +} +auto Textbook::set_required(bool required) -> void { + this->_required = required; +} +auto Textbook::set_used(bool used) -> void { this->_used = used; } + +auto Textbook::print() const -> void { + // Pretty print all values, minus evaluated values + std::cout << "Book: " << this->_code << "\nPrice: $" + << this->_single_copy_price + << "\nInventory: " << this->_number_on_hand + << "\nEnrollment: " << this->_prospective_enrollment + << "\nRequired: " << (this->_required ? "Yes" : "No") + << "\nUsed: " << (this->_used ? "Yes" : "No") << std::endl; +} + +auto Textbook::evaluate_needs() -> void { + /// Calculate the total cost of the books to order based on an input factor + const auto set_amount_to_order = [this](double factor) -> void { + this->_amount_to_order = + static_cast( + static_cast(this->_prospective_enrollment) * factor) - + this->_number_on_hand; + }; + + // Determine amount to order based on given percentages + if (this->_required && !this->_used) { + set_amount_to_order(0.9); + } else if (this->_required && this->_used) { + set_amount_to_order(0.65); + } else if (!this->_required && !this->_used) { + set_amount_to_order(0.4); + } else if (!this->_required && this->_used) { + set_amount_to_order(0.2); + } + + // Calculate total cost + this->_total_cost = + static_cast(this->_amount_to_order) * this->_single_copy_price; +} + +auto Textbook::print_needs() const -> void { + // Pretty print the amount to order and total cost + std::cout << "Need to order: " << this->_amount_to_order << "\nTotal cost: $" + << std::fixed << std::setprecision(2) << this->_total_cost + << std::endl; +} + +auto Textbook::operator==(const Textbook &other) const -> bool { + return this->_code == other._code; +} +auto Textbook::operator!=(const Textbook &other) const -> bool { + return this->_code != other._code; +} +} // namespace textbook diff --git a/week_1/review_program/textbook.h b/week_1/review_program/textbook.h new file mode 100644 index 0000000..879b11d --- /dev/null +++ b/week_1/review_program/textbook.h @@ -0,0 +1,62 @@ +#ifndef TEXTBOOK_HH +#define TEXTBOOK_HH +#include + +namespace textbook { +class Textbook { +private: + // Input values + uint64_t _code; + double _single_copy_price; + uint64_t _number_on_hand; + uint64_t _prospective_enrollment; + bool _required; + bool _used; + // Fix 6 byte misalignment + const char _padding[6] = {0}; + // Evaluated values + uint64_t _amount_to_order = {0}; + double _total_cost = {0}; + +public: + // Constructors + Textbook(uint64_t code, double single_copy_price, uint64_t number_on_hand, + uint64_t prospective_enrollment, bool required, bool used) + : _code(code), _single_copy_price(single_copy_price), + _number_on_hand(number_on_hand), + _prospective_enrollment(prospective_enrollment), _required(required), + _used(used) {} + Textbook() : Textbook(0, 0, 0, 0, false, false) {} + + // Getters + auto code() const -> uint64_t; + auto single_copy_price() const -> double; + auto number_on_hand() const -> uint64_t; + auto prospective_enrollment() const -> uint64_t; + auto required() const -> bool; + auto used() const -> bool; + auto amount_to_order() const -> uint64_t; + auto total_cost() const -> double; + + // Setters + auto set_code(uint64_t) -> void; + auto set_single_copy_price(double) -> void; + auto set_number_on_hand(uint64_t) -> void; + auto set_prospective_enrollment(uint64_t) -> void; + auto set_required(bool) -> void; + auto set_used(bool) -> void; + + // Procedures + auto evaluate_needs() -> void; + + // Printers + auto print() const -> void; + auto print_needs() const -> void; + + // Operator overloads + auto operator==(const Textbook &) const -> bool; + auto operator!=(const Textbook &) const -> bool; +}; +} // namespace textbook + +#endif diff --git a/week_1/review_program/textbook.hh b/week_1/review_program/textbook.hh deleted file mode 100644 index 879b11d..0000000 --- a/week_1/review_program/textbook.hh +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef TEXTBOOK_HH -#define TEXTBOOK_HH -#include - -namespace textbook { -class Textbook { -private: - // Input values - uint64_t _code; - double _single_copy_price; - uint64_t _number_on_hand; - uint64_t _prospective_enrollment; - bool _required; - bool _used; - // Fix 6 byte misalignment - const char _padding[6] = {0}; - // Evaluated values - uint64_t _amount_to_order = {0}; - double _total_cost = {0}; - -public: - // Constructors - Textbook(uint64_t code, double single_copy_price, uint64_t number_on_hand, - uint64_t prospective_enrollment, bool required, bool used) - : _code(code), _single_copy_price(single_copy_price), - _number_on_hand(number_on_hand), - _prospective_enrollment(prospective_enrollment), _required(required), - _used(used) {} - Textbook() : Textbook(0, 0, 0, 0, false, false) {} - - // Getters - auto code() const -> uint64_t; - auto single_copy_price() const -> double; - auto number_on_hand() const -> uint64_t; - auto prospective_enrollment() const -> uint64_t; - auto required() const -> bool; - auto used() const -> bool; - auto amount_to_order() const -> uint64_t; - auto total_cost() const -> double; - - // Setters - auto set_code(uint64_t) -> void; - auto set_single_copy_price(double) -> void; - auto set_number_on_hand(uint64_t) -> void; - auto set_prospective_enrollment(uint64_t) -> void; - auto set_required(bool) -> void; - auto set_used(bool) -> void; - - // Procedures - auto evaluate_needs() -> void; - - // Printers - auto print() const -> void; - auto print_needs() const -> void; - - // Operator overloads - auto operator==(const Textbook &) const -> bool; - auto operator!=(const Textbook &) const -> bool; -}; -} // namespace textbook - -#endif -- cgit v1.2.3