summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-09-26 22:39:21 -0700
committerFuwn <[email protected]>2023-09-26 22:39:21 -0700
commit826353dfcc7da180e3a92e7d19ad87ae09838b23 (patch)
tree7ff1dee68876a102912559399e7cd746d5b9a799
parentstyle(review_program): rename symbols (diff)
downloadcs260-826353dfcc7da180e3a92e7d19ad87ae09838b23.tar.xz
cs260-826353dfcc7da180e3a92e7d19ad87ae09838b23.zip
style(review_program): implement styles
-rw-r--r--week_1/review_program/review_program.cpp27
-rw-r--r--week_1/review_program/review_program.h16
-rw-r--r--week_1/review_program/textbook.cpp102
-rw-r--r--week_1/review_program/textbook.h47
4 files changed, 133 insertions, 59 deletions
diff --git a/week_1/review_program/review_program.cpp b/week_1/review_program/review_program.cpp
index d89d206..e6efe64 100644
--- a/week_1/review_program/review_program.cpp
+++ b/week_1/review_program/review_program.cpp
@@ -1,10 +1,22 @@
+/* review_program.cpp
+ * Summary: Implementation for main program functionality
+ * Test Cases:
+ * - 1221 69.95 30 150 1 1; 4686.65 937.33
+ * - 8392 60 30 150 1 1; 4020 804
+ * - 8392 60 30 150 1 1; 1800 360
+ * - 8392 60 30 100 1 1; 600 120
+ * Author: Zoltan Szabatin
+ * Created: September 26th, 2023
+ */
+
#include "review_program.h"
#include "textbook.h"
#include <cstdint>
#include <iostream>
-namespace ReviewProgram {
+namespace wk1_reviewProgram_zSzabatin {
+
void InputToTextbook(Textbook &textbook) {
// Obtain and set all non-evaluated values for textbook
textbook.SetCode(InputToTextbookValue<uint64_t>("Book"));
@@ -35,7 +47,7 @@ void TextbookInputHandler(std::vector<Textbook> &textbooks) {
// 5. Add textbook to running count
Textbook textbook;
- ReviewProgram::InputToTextbook(textbook);
+ InputToTextbook(textbook);
std::cout << "---" << std::endl;
@@ -55,7 +67,7 @@ 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) {
+ for (const Textbook &textbook : textbooks) {
total_for_all_books += textbook.GetTotalCost();
}
@@ -64,22 +76,23 @@ void FullTextbookPurchaseSummary(const std::vector<Textbook> &textbooks) {
<< std::endl;
std::cout << "Expected profit: $" << total_for_all_books * 0.2 << std::endl;
}
-} // namespace ReviewProgram
+
+} // namespace wk1_reviewProgram_zSzabatin
int main() {
bool run_again = false;
- std::vector<Textbook::Textbook> textbooks;
+ std::vector<wk1_reviewProgram_zSzabatin::Textbook> textbooks;
// Loop until user is done entering textbooks
do {
- ReviewProgram::TextbookInputHandler(textbooks);
+ wk1_reviewProgram_zSzabatin::TextbookInputHandler(textbooks);
std::cout << "Enter 1 to do another book, 0 to stop. ";
std::cin >> run_again;
} while (run_again);
// Print full purchase summary
- ReviewProgram::FullTextbookPurchaseSummary(textbooks);
+ wk1_reviewProgram_zSzabatin::FullTextbookPurchaseSummary(textbooks);
return 0;
}
diff --git a/week_1/review_program/review_program.h b/week_1/review_program/review_program.h
index 1537a8a..aa39db9 100644
--- a/week_1/review_program/review_program.h
+++ b/week_1/review_program/review_program.h
@@ -1,15 +1,23 @@
+/* review_program.h
+ * Summary: Definition file for program helpers
+ * Author: Zoltan Szabatin
+ * Created: September 26th, 2023
+ */
+
#ifndef REVIEW_PROGRAM_HH
#define REVIEW_PROGRAM_HH
#include "textbook.h"
#include <string>
#include <vector>
-namespace ReviewProgram {
-using Textbook::Textbook;
+namespace wk1_reviewProgram_zSzabatin {
void InputToTextbook(Textbook &);
-template <typename T> auto InputToTextbookValue(const std::string &) -> T;
+
+template <typename T> T InputToTextbookValue(const std::string &);
void TextbookInputHandler(std::vector<Textbook> &);
+
void FullTextbookPurchaseSummary(const std::vector<Textbook> &);
-} // namespace ReviewProgram
+
+} // namespace wk1_reviewProgram_zSzabatin
#endif
diff --git a/week_1/review_program/textbook.cpp b/week_1/review_program/textbook.cpp
index fe39be4..eff0e92 100644
--- a/week_1/review_program/textbook.cpp
+++ b/week_1/review_program/textbook.cpp
@@ -1,79 +1,109 @@
+/* textbook.cpp
+ * Summary: Implementation file for the Textbook class
+ * Author: Zoltan Szabatin
+ * Created: September 26th, 2023
+ */
+
#include "textbook.h"
#include <iomanip>
#include <iostream>
-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; }
+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;
+ return this->prospective_enrollment_;
}
-bool Textbook::GetIsRequired() const { return this->_is_required; }
+
+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; }
-void Textbook::SetCode(uint64_t code) { this->_code = code; }
+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;
+ this->single_copy_price_ = single_copy_price;
}
+
void Textbook::SetNumberOnHand(uint64_t number_on_hand) {
- this->_number_on_hand = number_on_hand;
+ this->number_on_hand_ = number_on_hand;
}
+
void Textbook::SetProspectiveEnrollment(uint64_t prospective_enrollment) {
- this->_prospective_enrollment = prospective_enrollment;
+ this->prospective_enrollment_ = prospective_enrollment;
}
-void Textbook::SetIsRequired(bool required) { this->_is_required = required; }
+
+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")
+ 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::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;
+}
+
+// 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 {
- this->_amount_to_order =
+ uint64_t amount_to_order =
static_cast<uint64_t>(
- static_cast<double>(this->_prospective_enrollment) * factor) -
- this->_number_on_hand;
+ static_cast<double>(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) {
+ if (this->is_required_ && !this->_is_used) {
set_amount_to_order(0.9);
- } else if (this->_is_required && this->_is_used) {
+ } else if (this->is_required_ && this->_is_used) {
set_amount_to_order(0.65);
- } else if (!this->_is_required && !this->_is_used) {
+ } else if (!this->is_required_ && !this->_is_used) {
set_amount_to_order(0.4);
- } else if (!this->_is_required && this->_is_used) {
+ } else if (!this->is_required_ && this->_is_used) {
set_amount_to_order(0.2);
}
// Calculate total cost
- this->_total_cost =
- static_cast<double>(this->_amount_to_order) * this->_single_copy_price;
-}
+ double total_cost =
+ static_cast<double>(this->amount_to_order_) * this->single_copy_price_;
-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;
+ this->total_cost_ = total_cost;
}
+// Operator overloads
bool Textbook::operator==(const Textbook &other) const {
- return this->_code == other._code;
+ return this->code_ == other.code_;
}
+
bool Textbook::operator!=(const Textbook &other) const {
- return this->_code != other._code;
+ return this->code_ != other.code_;
}
-} // namespace Textbook
+
+} // namespace wk1_reviewProgram_zSzabatin
diff --git a/week_1/review_program/textbook.h b/week_1/review_program/textbook.h
index 525d7c1..8bb05d3 100644
--- a/week_1/review_program/textbook.h
+++ b/week_1/review_program/textbook.h
@@ -1,49 +1,69 @@
+/* textbook.h
+ * Summary: Defines the Textbook class.
+ * Author: Zoltan Szabatin
+ * Created: September 26th, 2023
+ */
+
#ifndef TEXTBOOK_HH
#define TEXTBOOK_HH
#include <cstdint>
-namespace Textbook {
+namespace wk1_reviewProgram_zSzabatin {
+
class Textbook {
private:
// Input values
- uint64_t _code;
- double _single_copy_price;
- uint64_t _number_on_hand;
- uint64_t _prospective_enrollment;
- bool _is_required;
+ uint64_t code_;
+ double single_copy_price_;
+ uint64_t number_on_hand_;
+ uint64_t prospective_enrollment_;
+ bool is_required_;
bool _is_used;
// Fix 6 byte misalignment
const char PADDING[6] = {0};
// Evaluated values
- uint64_t _amount_to_order = {0};
- double _total_cost = {0};
+ 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), _is_required(required),
+ : code_(code), single_copy_price_(single_copy_price),
+ number_on_hand_(number_on_hand),
+ prospective_enrollment_(prospective_enrollment), is_required_(required),
_is_used(used) {}
+
Textbook() : Textbook(0, 0, 0, 0, false, false) {}
// Getters
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
void SetCode(uint64_t);
+
void SetSingleCopyPrice(double);
+
void SetNumberOnHand(uint64_t);
+
void SetProspectiveEnrollment(uint64_t);
+
void SetIsRequired(bool);
+
void SetIsUsed(bool);
// Procedures
@@ -51,12 +71,15 @@ public:
// Printers
void Print() const;
+
void PrintNeeds() const;
// Operator overloads
bool operator==(const Textbook &) const;
+
bool operator!=(const Textbook &) const;
};
-} // namespace Textbook
+
+} // namespace wk1_reviewProgram_zSzabatin
#endif