summaryrefslogtreecommitdiff
path: root/week_1/review_program/review_program.cc
blob: 61317ad1325a337cc31536b527843c9b6e3a012c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "review_program.hh"
#include "textbook.hh"

#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