/* textbook.cpp * Summary: Implementation file for the Textbook class * Author: Zoltan Szabatin * Created: April 2nd, 2024 */ #include "textbook.h" #include #include namespace wk1_reviewProgram_zSzabatin { // Getters uint64_t Textbook::GetCode() const { return this->code_; } double Textbook::GetSingleCopyPrice() const { return this->single_copy_price_; } uint64_t Textbook::GetNumberOnHand() const { return this->number_on_hand_; } uint64_t Textbook::GetProspectiveEnrollment() const { return this->prospective_enrollment_; } // uint64_t Textbook::GetProspectiveEnrollment() const bool Textbook::GetIsRequired() const { return this->is_required_; } bool Textbook::GetIsUsed() const { return this->_is_used; } uint64_t Textbook::GetAmountToOrder() const { return this->amount_to_order_; } double Textbook::GetTotalCost() const { return this->total_cost_; } // Setters void Textbook::SetCode(uint64_t code) { this->code_ = code; } void Textbook::SetSingleCopyPrice(double single_copy_price) { this->single_copy_price_ = single_copy_price; } // void Textbook::SetSingleCopyPrice(double single_copy_price) void Textbook::SetNumberOnHand(uint64_t number_on_hand) { this->number_on_hand_ = number_on_hand; } // void Textbook::SetNumberOnHand(uint64_t number_on_hand) void Textbook::SetProspectiveEnrollment(uint64_t prospective_enrollment) { this->prospective_enrollment_ = prospective_enrollment; } // void Textbook::SetProspectiveEnrollment(uint64_t prospective_enrollment) void Textbook::SetIsRequired(bool required) { this->is_required_ = required; } void Textbook::SetIsUsed(bool used) { this->_is_used = used; } // Printers void Textbook::Print() const { // 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->is_required_ ? "Yes" : "No") << "\nUsed: " << (this->_is_used ? "Yes" : "No") << std::endl; } // void Textbook::Print() const void Textbook::PrintNeeds() const { // 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; } // void Textbook::PrintNeeds() const // Evaluators void Textbook::EvaluateNeeds() { /// Calculate the total cost of the books to order based on an input factor const auto set_amount_to_order = [this](double factor) -> void { uint64_t amount_to_order = static_cast( static_cast(this->prospective_enrollment_) * factor) - this->number_on_hand_; this->amount_to_order_ = amount_to_order; }; // Determine amount to order based on given percentages if (this->is_required_ && !this->_is_used) { // This logic seems correct, but doesn't correspond with the sample output. // The flags look to be reversed and not correct in the sample. // // Changing the value from 0.9 to 0.65 yields the same result as the sample // output, but doesn't seem sound. set_amount_to_order(0.9); } else if (this->is_required_ && this->_is_used) { set_amount_to_order(0.65); } else if (!this->is_required_ && !this->_is_used) { set_amount_to_order(0.4); } else if (!this->is_required_ && this->_is_used) { set_amount_to_order(0.2); } // Calculate total cost double total_cost = static_cast(this->amount_to_order_) * this->single_copy_price_; this->total_cost_ = total_cost; } // void Textbook::EvaluateNeeds() // Operator overloads bool Textbook::operator==(const Textbook &other) const { return this->code_ == other.code_; } // bool Textbook::operator==(const Textbook &other) const bool Textbook::operator!=(const Textbook &other) const { return this->code_ != other.code_; } // bool Textbook::operator!=(const Textbook &other) const } // namespace wk1_reviewProgram_zSzabatin