Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdlib>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <locale>
  6. #include <string>
  7. #include <utility>
  8.  
  9. class Time {
  10.  public:
  11.   int hours{0}, minutes{0};
  12.  
  13.  private:
  14.   Time(int m) {
  15.     minutes = std::abs(m) % 60;
  16.     hours = ((std::abs(m) - minutes) / 60) % 24;
  17.   }
  18.  
  19.  public:
  20.   Time() {}
  21.  
  22.   Time(int h, int m) {
  23.     minutes = std::abs(m) % 60;
  24.     hours = (std::abs(h) + (std::abs(m) - minutes) / 60) % 24;
  25.   }
  26.   int totalMinutes() const { return hours * 60 + minutes; }
  27.  
  28.   friend bool operator>(Time& t1, Time& t2) {
  29.     return t1.totalMinutes() > t2.totalMinutes();
  30.   }
  31.  
  32.   friend bool operator==(Time& t1, Time& t2) {
  33.     return t1.totalMinutes() == t2.totalMinutes();
  34.   }
  35.  
  36.   friend Time operator-(Time& t1, Time& t2) {
  37.     int t1h{t1.hours}, t1m{t1.minutes}, t2h{t2.hours}, t2m{t2.minutes};
  38.  
  39.     if (t1m < t2m) {
  40.       --t1h;
  41.       t1m += 60;
  42.     }
  43.     return Time{t1h - t2h, t1m - t2m};
  44.   }
  45.  
  46.   friend std::istream& operator>>(std::istream& in, Time& t) {
  47.     int h, m;
  48.     in >> h >> m;
  49.     t = Time(h, m);
  50.     return in;
  51.   }
  52.  
  53.   friend std::ostream& operator<<(std::ostream& out, const Time& t) {
  54.     out << std::setfill('0') << std::setw(2) << t.hours % 12 << ":"
  55.         << std::setw(2) << t.minutes << (t.hours > 12 ? 'A' : 'P') << 'M';
  56.     return out;
  57.   }
  58. };
  59.  
  60. class Vehicle {
  61.  protected:
  62.   char vehicleType;
  63.   std::string name;
  64.   float higherRate, lowerRate;
  65.   Time rateChangeThreshold;
  66.  
  67.  public:
  68.   Vehicle(char type) : vehicleType{type} {}
  69.   virtual char getType() { return vehicleType; }
  70.   virtual Time getRateChangeThreshold() { return rateChangeThreshold; }
  71.   virtual float getHigherRate() { return higherRate; }
  72.   virtual float getLowerRate() { return lowerRate; }
  73.   virtual const std::string& getName() const { return name; }
  74. };
  75.  
  76. class Cars : public Vehicle {
  77.  protected:
  78.   Cars(char type) : Vehicle{type} {
  79.     Vehicle::higherRate = 0;
  80.     Vehicle::lowerRate = 1.5;
  81.     Vehicle::rateChangeThreshold = Time{3, 0};
  82.   }
  83. };
  84.  
  85. class Car : public Cars {
  86.  public:
  87.   Car(char type = 'C') : Cars{type} { Vehicle::name = "Car"; }
  88. };
  89.  
  90. class Suv : public Cars {
  91.  public:
  92.   Suv(char type = 'S') : Cars{type} { Vehicle::name = "Suv"; }
  93. };
  94.  
  95. class Van : public Cars {
  96.  public:
  97.   Van(char type = 'V') : Cars{type} { Vehicle::name = "Van"; }
  98. };
  99.  
  100. class Bus : public Vehicle {
  101.  public:
  102.   Bus(char type = 'B') : Vehicle{type} {
  103.     Vehicle::name = "Bus";
  104.     Vehicle::rateChangeThreshold = Time{2, 0};
  105.     Vehicle::higherRate = 1;
  106.     Vehicle::lowerRate = 2.5;
  107.   }
  108. };
  109.  
  110. class Truck : public Vehicle {
  111.  public:
  112.   Truck(char type = 'T') : Vehicle{type} {
  113.     Vehicle::name = "Truck";
  114.     Vehicle::rateChangeThreshold = Time{1, 0};
  115.     Vehicle::higherRate = 2;
  116.     Vehicle::lowerRate = 3.5;
  117.   }
  118. };
  119.  
  120. class VehicalEntry {
  121.  protected:
  122.   Vehicle* vehicle;
  123.   Time entryTime;
  124.   Time exitTime;
  125.  
  126.  public:
  127.   VehicalEntry(Vehicle* v, Time in, Time out) : entryTime{in}, exitTime{out} {
  128.     switch (std::toupper(v->getType())) {
  129.       case 'C':
  130.         vehicle = new Car();
  131.         break;
  132.       case 'V':
  133.         vehicle = new Van();
  134.         break;
  135.       case 'S':
  136.         vehicle = new Suv();
  137.         break;
  138.       case 'B':
  139.         vehicle = new Bus();
  140.         break;
  141.       case 'T':
  142.         vehicle = new Truck();
  143.         break;
  144.       default:
  145.         std::cerr << "Bad vehicle type\n";
  146.     }
  147.     std::clog << "Vehical Entry created at " << this << "\n";
  148.   }
  149.  
  150.   ~VehicalEntry() {
  151.     std::clog << "Destructing " << this << "\n";
  152.     delete vehicle;
  153.   }
  154.  
  155.   Time getDuration() { return exitTime - entryTime; }
  156.   int roundedDuration() {
  157.     return std::trunc(std::ceil(getDuration().totalMinutes() / 60.0));
  158.   }
  159.  
  160.   float calculateCharge() {
  161.     int timeFactor = roundedDuration();
  162.     int chargeThreshold = vehicle->getRateChangeThreshold().hours;
  163.  
  164.     return timeFactor < chargeThreshold
  165.                ? timeFactor * vehicle->getLowerRate()
  166.                : chargeThreshold * vehicle->getLowerRate() +
  167.                      (timeFactor - chargeThreshold) * vehicle->getHigherRate();
  168.   }
  169.   void printReceipt() {
  170.     std::cout << "Type of vehicle:\t" << vehicle->getName() << "\n"
  171.               << "Time-in:\t\t" << entryTime << "\n"
  172.               << "Time-exit:\t\t" << exitTime << "\n"
  173.               << std::setw(32) << std::setfill('-') << "\n"
  174.               << "Parking time:\t\t" << getDuration().hours << ":"
  175.               << std::setw(2) << std::setfill('0') << getDuration().minutes
  176.               << "\n"
  177.               << "Rounded parking time:\t" << roundedDuration() << " hours\n"
  178.               << std::setw(32) << std::setfill('-') << "\n"
  179.               << "TOTAL DUE:\t\t$" << std::setprecision(2) << calculateCharge()
  180.               << "\n";
  181.   }
  182. };
  183.  
  184. int main() {
  185.   char type;
  186.   Time in, out;
  187.   char response;
  188.   std::cout << "Passaic County Parking Authority\nParking Lot charges\n";
  189.   do {
  190.     std::cout << "Enter vehicle type, enter and exit times seperated by "
  191.                  "whitespaces: ";
  192.     std::cin >> type >> in >> out;
  193.     VehicalEntry{new Vehicle{type}, in, out}.printReceipt();
  194.     std::cout << "Enter another entry?: ";
  195.     std::cin >> response;
  196.   } while (response == std::toupper('y'));
  197.   std::cout << "Thank you for using Passaic County Parking Authority.\n";
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement