summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--week_1/review_program/review_program.cpp51
-rw-r--r--week_1/review_program/review_program.h14
-rw-r--r--week_1/review_program/textbook.cpp63
-rw-r--r--week_1/review_program/textbook.h52
4 files changed, 85 insertions, 95 deletions
diff --git a/week_1/review_program/review_program.cpp b/week_1/review_program/review_program.cpp
index 38fb0f3..d89d206 100644
--- a/week_1/review_program/review_program.cpp
+++ b/week_1/review_program/review_program.cpp
@@ -4,21 +4,20 @@
#include <cstdint>
#include <iostream>
-namespace review_program {
-auto textbook_input(textbook::Textbook &textbook) -> void {
+namespace ReviewProgram {
+void InputToTextbook(Textbook &textbook) {
// 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)"));
+ textbook.SetCode(InputToTextbookValue<uint64_t>("Book"));
+ textbook.SetSingleCopyPrice(InputToTextbookValue<double>("Price"));
+ textbook.SetNumberOnHand(InputToTextbookValue<uint64_t>("Number on hand"));
+ textbook.SetProspectiveEnrollment(
+ InputToTextbookValue<uint64_t>("Enrollment"));
+ textbook.SetIsRequired(
+ InputToTextbookValue<bool>("Required (1 = required, 0 = optional)"));
+ textbook.SetIsUsed(InputToTextbookValue<bool>("Used (1 = used, 0 = new)"));
}
-template <typename T>
-auto input_textbook_value(const std::string &message) -> T {
+template <typename T> T InputToTextbookValue(const std::string &message) {
// Helper for repetitive textbook value prompts
T value = 0;
@@ -28,38 +27,36 @@ auto input_textbook_value(const std::string &message) -> T {
return value;
}
-auto textbook_input_loop_handler(std::vector<textbook::Textbook> &textbooks)
- -> void {
+void TextbookInputHandler(std::vector<Textbook> &textbooks) {
// 1. Input textbook
// 2. Print textbook
// 3. Evaluate textbook needs
// 4. Print textbook needs
// 5. Add textbook to running count
- textbook::Textbook textbook;
+ Textbook textbook;
- review_program::textbook_input(textbook);
+ ReviewProgram::InputToTextbook(textbook);
std::cout << "---" << std::endl;
- textbook.print();
+ textbook.Print();
std::cout << "---" << std::endl;
- textbook.evaluate_needs();
- textbook.print_needs();
+ textbook.EvaluateNeeds();
+ textbook.PrintNeeds();
std::cout << "---" << std::endl;
textbooks.push_back(textbook);
}
-auto full_purchase_summary(const std::vector<textbook::Textbook> &textbooks)
- -> void {
+void FullTextbookPurchaseSummary(const std::vector<Textbook> &textbooks) {
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();
+ total_for_all_books += textbook.GetTotalCost();
}
// Print total cost for all books and expected profits
@@ -67,22 +64,22 @@ auto full_purchase_summary(const std::vector<textbook::Textbook> &textbooks)
<< std::endl;
std::cout << "Expected profit: $" << total_for_all_books * 0.2 << std::endl;
}
-} // namespace review_program
+} // namespace ReviewProgram
-auto main() -> int {
+int main() {
bool run_again = false;
- std::vector<textbook::Textbook> textbooks;
+ std::vector<Textbook::Textbook> textbooks;
// Loop until user is done entering textbooks
do {
- review_program::textbook_input_loop_handler(textbooks);
+ ReviewProgram::TextbookInputHandler(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);
+ ReviewProgram::FullTextbookPurchaseSummary(textbooks);
return 0;
}
diff --git a/week_1/review_program/review_program.h b/week_1/review_program/review_program.h
index 45fe940..1537a8a 100644
--- a/week_1/review_program/review_program.h
+++ b/week_1/review_program/review_program.h
@@ -4,10 +4,12 @@
#include <string>
#include <vector>
-namespace review_program {
-auto textbook_input(textbook::Textbook &) -> void;
-template <typename T> auto input_textbook_value(const std::string &) -> T;
-auto textbook_input_loop_handler(std::vector<textbook::Textbook> &) -> void;
-auto full_purchase_summary(const std::vector<textbook::Textbook> &) -> void;
-} // namespace review_program
+namespace ReviewProgram {
+using Textbook::Textbook;
+
+void InputToTextbook(Textbook &);
+template <typename T> auto InputToTextbookValue(const std::string &) -> T;
+void TextbookInputHandler(std::vector<Textbook> &);
+void FullTextbookPurchaseSummary(const std::vector<Textbook> &);
+} // namespace ReviewProgram
#endif
diff --git a/week_1/review_program/textbook.cpp b/week_1/review_program/textbook.cpp
index dfca4cd..fe39be4 100644
--- a/week_1/review_program/textbook.cpp
+++ b/week_1/review_program/textbook.cpp
@@ -3,51 +3,42 @@
#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 {
+namespace Textbook {
+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;
}
-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; }
+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; }
-auto Textbook::set_code(uint64_t code) -> void { this->_code = code; }
-auto Textbook::set_single_copy_price(double single_copy_price) -> void {
+void Textbook::SetCode(uint64_t code) { this->_code = code; }
+void Textbook::SetSingleCopyPrice(double single_copy_price) {
this->_single_copy_price = single_copy_price;
}
-auto Textbook::set_number_on_hand(uint64_t number_on_hand) -> void {
+void Textbook::SetNumberOnHand(uint64_t number_on_hand) {
this->_number_on_hand = number_on_hand;
}
-auto Textbook::set_prospective_enrollment(uint64_t prospective_enrollment)
- -> void {
+void Textbook::SetProspectiveEnrollment(uint64_t prospective_enrollment) {
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; }
+void Textbook::SetIsRequired(bool required) { this->_is_required = required; }
+void Textbook::SetIsUsed(bool used) { this->_is_used = used; }
-auto Textbook::print() const -> void {
+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->_required ? "Yes" : "No")
- << "\nUsed: " << (this->_used ? "Yes" : "No") << std::endl;
+ << "\nRequired: " << (this->_is_required ? "Yes" : "No")
+ << "\nUsed: " << (this->_is_used ? "Yes" : "No") << std::endl;
}
-auto Textbook::evaluate_needs() -> void {
+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 {
this->_amount_to_order =
@@ -57,13 +48,13 @@ auto Textbook::evaluate_needs() -> void {
};
// Determine amount to order based on given percentages
- if (this->_required && !this->_used) {
+ if (this->_is_required && !this->_is_used) {
set_amount_to_order(0.9);
- } else if (this->_required && this->_used) {
+ } else if (this->_is_required && this->_is_used) {
set_amount_to_order(0.65);
- } else if (!this->_required && !this->_used) {
+ } else if (!this->_is_required && !this->_is_used) {
set_amount_to_order(0.4);
- } else if (!this->_required && this->_used) {
+ } else if (!this->_is_required && this->_is_used) {
set_amount_to_order(0.2);
}
@@ -72,17 +63,17 @@ auto Textbook::evaluate_needs() -> void {
static_cast<double>(this->_amount_to_order) * this->_single_copy_price;
}
-auto Textbook::print_needs() const -> void {
+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;
}
-auto Textbook::operator==(const Textbook &other) const -> bool {
+bool Textbook::operator==(const Textbook &other) const {
return this->_code == other._code;
}
-auto Textbook::operator!=(const Textbook &other) const -> bool {
+bool Textbook::operator!=(const Textbook &other) const {
return this->_code != other._code;
}
-} // namespace textbook
+} // namespace Textbook
diff --git a/week_1/review_program/textbook.h b/week_1/review_program/textbook.h
index 879b11d..525d7c1 100644
--- a/week_1/review_program/textbook.h
+++ b/week_1/review_program/textbook.h
@@ -2,7 +2,7 @@
#define TEXTBOOK_HH
#include <cstdint>
-namespace textbook {
+namespace Textbook {
class Textbook {
private:
// Input values
@@ -10,10 +10,10 @@ private:
double _single_copy_price;
uint64_t _number_on_hand;
uint64_t _prospective_enrollment;
- bool _required;
- bool _used;
+ bool _is_required;
+ bool _is_used;
// Fix 6 byte misalignment
- const char _padding[6] = {0};
+ const char PADDING[6] = {0};
// Evaluated values
uint64_t _amount_to_order = {0};
double _total_cost = {0};
@@ -24,39 +24,39 @@ public:
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) {}
+ _prospective_enrollment(prospective_enrollment), _is_required(required),
+ _is_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;
+ uint64_t GetCode() const;
+ double GetSingleCopyPrice() const;
+ uint64_t GetNumberOnHand() const;
+ uint64_t GetProspectiveEnrollment() const;
+ bool GetIsRequired() const;
+ bool GetIsUsed() const;
+ uint64_t GetAmountToOrder() const;
+ double GetTotalCost() const;
// 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;
+ void SetCode(uint64_t);
+ void SetSingleCopyPrice(double);
+ void SetNumberOnHand(uint64_t);
+ void SetProspectiveEnrollment(uint64_t);
+ void SetIsRequired(bool);
+ void SetIsUsed(bool);
// Procedures
- auto evaluate_needs() -> void;
+ void EvaluateNeeds();
// Printers
- auto print() const -> void;
- auto print_needs() const -> void;
+ void Print() const;
+ void PrintNeeds() const;
// Operator overloads
- auto operator==(const Textbook &) const -> bool;
- auto operator!=(const Textbook &) const -> bool;
+ bool operator==(const Textbook &) const;
+ bool operator!=(const Textbook &) const;
};
-} // namespace textbook
+} // namespace Textbook
#endif