fork download
  1. /////////////////////////////////////
  2. // timepoint classes (booking.hpp)
  3.  
  4. struct timepoint
  5. {
  6. int hour, minute;
  7.  
  8. timepoint normalized() const;
  9. int totalMinutes () const;
  10. int roundedHours () const;
  11. timepoint operator- (timepoint const& rhs) const;
  12. };
  13.  
  14. struct booking_t
  15. {
  16. char vehicle;
  17. timepoint enter, exit;
  18.  
  19. timepoint parked() const { return exit - enter; }
  20. };
  21.  
  22. /////////////////////////////////////
  23. // main program (main.cpp)
  24. booking_t inputData();
  25. void displayBill(booking_t const& booking);
  26.  
  27. int main(void)
  28. {
  29. auto booking = inputData();
  30. displayBill(booking);
  31. }
  32.  
  33. /////////////////////////////////////
  34. // timepoint methods (booking.cpp)
  35.  
  36. timepoint timepoint::normalized() const
  37. {
  38. timepoint tmp { (hour + minute/60) % 24, minute % 60 };
  39. while (tmp.minute < 0) tmp.hour--, tmp.minute+=60;
  40. while (tmp.hour < 0) tmp.hour+=24;
  41. return tmp;
  42. }
  43.  
  44. int timepoint::roundedHours() const
  45. {
  46. return (totalMinutes()-1) / 60 + 1; // TODO check rounding logic
  47. }
  48.  
  49. int timepoint::totalMinutes() const
  50. {
  51. return hour*60 + minute;
  52. }
  53.  
  54. timepoint timepoint::operator-(timepoint const& rhs) const
  55. {
  56. return timepoint { 0, totalMinutes() - rhs.totalMinutes() } .normalized();
  57. }
  58.  
  59. #include <iostream> //used for cout/cin
  60.  
  61. timepoint getTime(std::string label)
  62. {
  63. int hour, minute;
  64. std::cout << "\nHour " << label << " 0-24: ";
  65. std::cin >> hour;
  66. std::cout << "\nMinute " << label << " 0-60: ";
  67. std::cin >> minute;
  68. return { hour, minute };
  69. }
  70.  
  71. /////////////////////////////////////
  72. // global functions - input
  73. booking_t inputData()
  74. {
  75. std::cout << "Enter C for car, B for bus, T for truck: ";
  76. char v;
  77. std::cin >> v;
  78. auto entered = getTime("vehicle entered");
  79. auto exited = getTime("vehicle exited");
  80. return { v, entered.normalized(), exited.normalized() };
  81. }
  82.  
  83. /////////////////////////////////////
  84. // calculation + billing
  85. #include <sstream>
  86. #include <iomanip> //used to manipulate data
  87. #include <map>
  88.  
  89. static std::ostream& operator <<(std::ostream& os, timepoint const& tp)
  90. {
  91. std::ostringstream oss;
  92. oss << std::setw(2) << std::setfill('0') << tp.hour << ':'
  93. << std::setw(2) << std::setfill('0') << tp.minute;
  94. return os << oss.str();
  95. }
  96.  
  97. std::pair<float,float> charge(booking_t const& booking)
  98. {
  99. struct tariff_t { int threshold; float rate1, rate2; };
  100. const static auto tariffs = std::map<char, tariff_t> {
  101. { 'c', { 3, 0 , 1.25 } },
  102. { 'b', { 2, 2. , 2.5 } },
  103. { 't', { 1, 3.75, 4.5 } } ,
  104. };
  105.  
  106. auto& tariff = tariffs.at(std::tolower(booking.vehicle));
  107. auto parked = booking.parked().roundedHours();
  108.  
  109. return std::make_pair(
  110. tariff.rate1 * std::min(tariff.threshold, parked) ,
  111. tariff.rate2 * std::max(0, parked - tariff.threshold));
  112. }
  113.  
  114. void displayBill(booking_t const& booking)
  115. {
  116. std::cout << "\n\n PARKING LOT CHARGE\n";
  117. std::cout << "Type of vehicle: Car or Bus or Truck\n";
  118. std::cout << "TIME-IN " << booking.enter << "\n";
  119. std::cout << "TIME-OUT " << booking.exit << "\n";
  120. std::cout << " " << "--------\n";
  121. std::cout << "PARKING TIME " << booking.parked() << "\n";
  122. std::cout << " ROUNDED " << booking.parked().roundedHours() << "\n";
  123. std::cout << " " << "--------\n";
  124.  
  125. auto rates = charge(booking);
  126. float total = rates.first + rates.second;
  127. std::cout << "TOTAL CHARGE " << std::fixed << std::setw(7) << std::setprecision(2) << total << "\n\n";
  128. }
  129.  
Success #stdin #stdout 0s 3028KB
stdin
c 23 55 4 56
stdout
Enter C for car, B for bus, T for truck: 
Hour vehicle entered 0-24: 
Minute vehicle entered 0-60: 
Hour vehicle exited 0-24: 
Minute vehicle exited 0-60: 

    PARKING LOT CHARGE
Type of vehicle: Car or Bus or Truck
TIME-IN         23:55
TIME-OUT        04:56
                --------
PARKING TIME    05:01
        ROUNDED 6
                --------
TOTAL CHARGE       3.75