diff options
| author | Fuwn <[email protected]> | 2023-09-26 22:00:33 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-26 22:00:33 -0700 |
| commit | 9d2792782da945fbeb8313a7b8e6ae79717c0d82 (patch) | |
| tree | f584a26331dbbe7fe39dea61284ffccb95026f33 /week_1/review_program/review_program.cpp | |
| parent | fix(review_program): c++14 (diff) | |
| download | cs260-9d2792782da945fbeb8313a7b8e6ae79717c0d82.tar.xz cs260-9d2792782da945fbeb8313a7b8e6ae79717c0d82.zip | |
fix(review_program): use style guide
Diffstat (limited to 'week_1/review_program/review_program.cpp')
| -rw-r--r-- | week_1/review_program/review_program.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
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 <cstdint> +#include <iostream> + +auto main() -> int { + bool run_again = false; + std::vector<textbook::Textbook> 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<uint64_t>("Book")); + textbook.set_single_copy_price(input_textbook_value<double>("Price")); + textbook.set_number_on_hand(input_textbook_value<uint64_t>("Number on hand")); + textbook.set_prospective_enrollment( + input_textbook_value<uint64_t>("Enrollment")); + textbook.set_required( + input_textbook_value<bool>("Required (1 = required, 0 = optional)")); + textbook.set_used(input_textbook_value<bool>("Used (1 = used, 0 = new)")); +} + +template <typename T> +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<textbook::Textbook> &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<textbook::Textbook> &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 |