Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdlib>
- #include <iomanip>
- #include <iostream>
- #include <locale>
- #include <string>
- #include <utility>
- class Time {
- public:
- int hours{0}, minutes{0};
- private:
- Time(int m) {
- minutes = std::abs(m) % 60;
- hours = ((std::abs(m) - minutes) / 60) % 24;
- }
- public:
- Time() {}
- Time(int h, int m) {
- minutes = std::abs(m) % 60;
- hours = (std::abs(h) + (std::abs(m) - minutes) / 60) % 24;
- }
- int totalMinutes() const { return hours * 60 + minutes; }
- friend bool operator>(Time& t1, Time& t2) {
- return t1.totalMinutes() > t2.totalMinutes();
- }
- friend bool operator==(Time& t1, Time& t2) {
- return t1.totalMinutes() == t2.totalMinutes();
- }
- friend Time operator-(Time& t1, Time& t2) {
- int t1h{t1.hours}, t1m{t1.minutes}, t2h{t2.hours}, t2m{t2.minutes};
- if (t1m < t2m) {
- --t1h;
- t1m += 60;
- }
- return Time{t1h - t2h, t1m - t2m};
- }
- friend std::istream& operator>>(std::istream& in, Time& t) {
- int h, m;
- in >> h >> m;
- t = Time(h, m);
- return in;
- }
- friend std::ostream& operator<<(std::ostream& out, const Time& t) {
- out << std::setfill('0') << std::setw(2) << t.hours % 12 << ":"
- << std::setw(2) << t.minutes << (t.hours > 12 ? 'A' : 'P') << 'M';
- return out;
- }
- };
- class Vehicle {
- protected:
- char vehicleType;
- std::string name;
- float higherRate, lowerRate;
- Time rateChangeThreshold;
- public:
- Vehicle(char type) : vehicleType{type} {}
- virtual char getType() { return vehicleType; }
- virtual Time getRateChangeThreshold() { return rateChangeThreshold; }
- virtual float getHigherRate() { return higherRate; }
- virtual float getLowerRate() { return lowerRate; }
- virtual const std::string& getName() const { return name; }
- };
- class Cars : public Vehicle {
- protected:
- Cars(char type) : Vehicle{type} {
- Vehicle::higherRate = 0;
- Vehicle::lowerRate = 1.5;
- Vehicle::rateChangeThreshold = Time{3, 0};
- }
- };
- class Car : public Cars {
- public:
- Car(char type = 'C') : Cars{type} { Vehicle::name = "Car"; }
- };
- class Suv : public Cars {
- public:
- Suv(char type = 'S') : Cars{type} { Vehicle::name = "Suv"; }
- };
- class Van : public Cars {
- public:
- Van(char type = 'V') : Cars{type} { Vehicle::name = "Van"; }
- };
- class Bus : public Vehicle {
- public:
- Bus(char type = 'B') : Vehicle{type} {
- Vehicle::name = "Bus";
- Vehicle::rateChangeThreshold = Time{2, 0};
- Vehicle::higherRate = 1;
- Vehicle::lowerRate = 2.5;
- }
- };
- class Truck : public Vehicle {
- public:
- Truck(char type = 'T') : Vehicle{type} {
- Vehicle::name = "Truck";
- Vehicle::rateChangeThreshold = Time{1, 0};
- Vehicle::higherRate = 2;
- Vehicle::lowerRate = 3.5;
- }
- };
- class VehicalEntry {
- protected:
- Vehicle* vehicle;
- Time entryTime;
- Time exitTime;
- public:
- VehicalEntry(Vehicle* v, Time in, Time out) : entryTime{in}, exitTime{out} {
- switch (std::toupper(v->getType())) {
- case 'C':
- vehicle = new Car();
- break;
- case 'V':
- vehicle = new Van();
- break;
- case 'S':
- vehicle = new Suv();
- break;
- case 'B':
- vehicle = new Bus();
- break;
- case 'T':
- vehicle = new Truck();
- break;
- default:
- std::cerr << "Bad vehicle type\n";
- }
- std::clog << "Vehical Entry created at " << this << "\n";
- }
- ~VehicalEntry() {
- std::clog << "Destructing " << this << "\n";
- delete vehicle;
- }
- Time getDuration() { return exitTime - entryTime; }
- int roundedDuration() {
- return std::trunc(std::ceil(getDuration().totalMinutes() / 60.0));
- }
- float calculateCharge() {
- int timeFactor = roundedDuration();
- int chargeThreshold = vehicle->getRateChangeThreshold().hours;
- return timeFactor < chargeThreshold
- ? timeFactor * vehicle->getLowerRate()
- : chargeThreshold * vehicle->getLowerRate() +
- (timeFactor - chargeThreshold) * vehicle->getHigherRate();
- }
- void printReceipt() {
- std::cout << "Type of vehicle:\t" << vehicle->getName() << "\n"
- << "Time-in:\t\t" << entryTime << "\n"
- << "Time-exit:\t\t" << exitTime << "\n"
- << std::setw(32) << std::setfill('-') << "\n"
- << "Parking time:\t\t" << getDuration().hours << ":"
- << std::setw(2) << std::setfill('0') << getDuration().minutes
- << "\n"
- << "Rounded parking time:\t" << roundedDuration() << " hours\n"
- << std::setw(32) << std::setfill('-') << "\n"
- << "TOTAL DUE:\t\t$" << std::setprecision(2) << calculateCharge()
- << "\n";
- }
- };
- int main() {
- char type;
- Time in, out;
- char response;
- std::cout << "Passaic County Parking Authority\nParking Lot charges\n";
- do {
- std::cout << "Enter vehicle type, enter and exit times seperated by "
- "whitespaces: ";
- std::cin >> type >> in >> out;
- VehicalEntry{new Vehicle{type}, in, out}.printReceipt();
- std::cout << "Enter another entry?: ";
- std::cin >> response;
- } while (response == std::toupper('y'));
- std::cout << "Thank you for using Passaic County Parking Authority.\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement