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/textbook.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/textbook.cpp')
| -rw-r--r-- | week_1/review_program/textbook.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
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 <iomanip> +#include <iostream> + +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<uint64_t>( + static_cast<double>(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<double>(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 |