Thursday, January 11, 2018

Programming Challenge 14.14 - Parking Ticket Simulator

Example Files: ParkingTicketSimulator.7z


ParkedCar.h


#ifndef PARKED_CAR_H_
#define PARKED_CAR_H_

#include <iostream>
using std::ostream;

#include <string>
using std::string;

class ParkedCar
{
    private:
        string make;                        // Car make
        string model;                        // Car model
        string color;                        // Car color
        string plateNumber;                // Car plate number
        int    minutesParked;            // Number of minutes a car is parked

    public:
        ParkedCar()
        { }

        ParkedCar(string, string, string, string, int);

        int getMinutesParked() const
        { return minutesParked; }

        // Friend
        friend ostream &operator << (ostream &, const ParkedCar &);
};

#endif

ParkedCar.cpp


#include "ParkedCar.h"

/* **********************************************************
            ParkedCar::ParkedCar() : Constructor
    Constructor accepting four string objects and an int as
    arguments.
   ********************************************************** */

ParkedCar::ParkedCar(string cMake, string cModel, string cColor, string cLicNum, int minutes)
{
    make = cMake;
    model = cModel;
    color = cColor;
    plateNumber = cLicNum;
    minutesParked = minutes;
}

/* **********************************************************
            Overloaded << operator
   ********************************************************** */

ostream &operator << (ostream &oStrm, const ParkedCar &obj)
{
    return oStrm << "Make:        "           << obj.make             << "\n"
                         << "Model:        "          << obj.model            << "\n"
                         << "Color:        "           << obj.color              << "\n"
                         << "License Number: " << obj.plateNumber << "\n";
}

ParkingMeter.h


#ifndef PARKING_METER_H_
#define PARKING_METER_H_

class ParkingMeter
{
    private:
        int timePurchased;        // The amount of time purchased

    public:
        ParkingMeter()
        { }
       
        ParkingMeter(int minutes)
        { timePurchased = minutes; }

        int getTimePurchased() const
        { return timePurchased; }
};

#endif

PoliceOfficer.h


#ifndef POLICE_OFFICER_H_
#define POLICE_OFFICER_H_

class ParkingTicket;

#include "ParkedCar.h"
#include "ParkingMeter.h"

#include <iostream>
using std::istream;
using std::ostream;

#include <string>
using std::string;

class PoliceOfficer
{
    private:
        string name;                    // Holding the name of an officer
        string badgeNumber;            // Holding the officer's badge number
       
    public:
        PoliceOfficer()
        { }

        PoliceOfficer(string, string);
       
        void setPoliceOfficer(string, string);
        bool isViolator(const ParkingMeter &, const ParkedCar &);
        void issueTicket(const ParkingMeter &, const ParkedCar &);

        // Friends
        friend istream &operator >> (istream &, PoliceOfficer &);
        friend ostream &operator << (ostream &, const PoliceOfficer &);
};

#endif

PoliceOfficer.cpp


#include "PoliceOfficer.h"
#include "ParkingTicket.h"

#include <iostream>
using std::cout;

/* **********************************************************
            PoliceOfficer::PoliceOfficer() - Constructor
    Accepts two string objects as its argument.
   ********************************************************** */

PoliceOfficer::PoliceOfficer(string n, string ID)
{
    setPoliceOfficer(n, ID);
}

/* **********************************************************
            PoliceOfficer::setPoliceOfficer() : string, string
    This function determines whether the string objects passed
    to it are empty. If one or both are empty, a default name
    or badge number is assigned to the appropriate class
    member variables. Else, the values passed to this function
    will be assigned to them.
   ********************************************************** */

void PoliceOfficer::setPoliceOfficer(string n, string ID)
{
    if (!n.empty())
    {
        name = n;
    }
    else
    {
        name = "Haruto Nakayama";
    }

    if (!ID.empty())
    {
        badgeNumber = ID;
    }
    else
    {
        badgeNumber = "NTPD-9284984-RD";
    }
}

/* **********************************************************
            ParkingTicket::isViolator(bool) : const obj &,
                                                         const obj &
    This function accepts two class objects passed to it by
    reference. It compares the time a car is parked to the
    amount of time purchased at the parking meter to determine
    whether the car is legally parked. The result of this
    evaluation is returned from the function.
    ********************************************************** */

bool PoliceOfficer::isViolator(const ParkingMeter &meter, const ParkedCar &car)
{
    bool status = false;

    if (car.getMinutesParked() > meter.getTimePurchased())
    {
        status = true;
    }

    return status;
}

/* **********************************************************
            ParkingTicket::issueTicket() const obj &,
                                                  const obj &
    This function creates a ParkingTicket object and passes to
    its constructor a ParkedCar, an PoliceOfficer, and a
    ParkingMeter object. It then calls the printTicket()
    function of the ParkingTicket class to output the ticket-
    information.
   ********************************************************** */

void PoliceOfficer::issueTicket(const ParkingMeter &meter, const ParkedCar &car)
{
    ParkingTicket tempTicket(car, *this, meter);

    tempTicket.printTicket();
}

/* **********************************************************
            Overloaded >> operator
   ********************************************************** */

istream &operator >> (istream &iStrm, PoliceOfficer &obj)
{
    cout << "Officer name: ";
    getline(iStrm, obj.name);
    cout << "Officer ID:   ";
    getline(iStrm, obj.badgeNumber);

    obj.setPoliceOfficer(obj.name, obj.badgeNumber);

    return iStrm;
}

/* **********************************************************
            Overloaded << operator
   ********************************************************** */

ostream &operator << (ostream &oStrm, const PoliceOfficer &obj)
{
    return oStrm << "Officer Name: " << obj.name << "\n"
                         << "Officer ID:   " << obj.badgeNumber << "\n";
}

ParkingTicket.h


#ifndef PARKING_TICKET_H_
#define PARKING_TICKET_H_

#include "PoliceOfficer.h"

class ParkingTicket
{
    private:   
        const double BASE_FINE   = 25.0;        // Base fine for the first hour or part of an hour
        const double HOURLY_FINE = 10.0;        // Additional fine for every additional hour or part of an hour

        double fine;                                // The amount of fine to be paid
        double hourlyFine;                     // The amount of additional fine to be paid

        // class objects
        PoliceOfficer issuer;
        ParkedCar violator;
        ParkingMeter parkingMeter;

    public:
        ParkingTicket()
        { }

        ParkingTicket(const ParkedCar &, const PoliceOfficer &, const ParkingMeter &);

        void calcFine();
        void printTicket();

        double getFine() const
        { return fine; }

        double getHourlyFine() const
        { return hourlyFine; }
};

#endif

ParkingTicket.cpp


#include "ParkingTicket.h"

#include <iomanip>
using std::cout;
using std::setprecision;

#include <iostream>
using std::fixed;

/* **********************************************************
            ParkingTicket::ParkingTicket()
    Constructor accepting three class objects passed to it by
    reference.
   ********************************************************** */

ParkingTicket::ParkingTicket(const ParkedCar &car, const PoliceOfficer &officer, const ParkingMeter &meter)
{
    violator = car;
    parkingMeter = meter;
    issuer = officer;

    calcFine();
    printTicket();
}

/* **********************************************************
            ParkingTicket::calcFine()
    This function calculates the amount of fine. If the amount
    of time is lower than two hours, the base fine is assigned
    to the fine class member variable. Else, the fine and
    additional fine is calculated and assigned to the class
    member variables fine and addFine.
   ********************************************************** */

void ParkingTicket::calcFine()
{
    int overTime = (1 + (violator.getMinutesParked() - parkingMeter.getTimePurchased()) / 60);

    if (overTime < 2)
    {
        fine = BASE_FINE;
    }
    else
    {
        fine = BASE_FINE + (HOURLY_FINE * overTime);
        hourlyFine = fine - BASE_FINE;
    }
}

/* **********************************************************
            ParkingTicket::printTicket()
    This function outputs a traffic-ticket with all relevant
    information.
   ********************************************************** */

void ParkingTicket::printTicket()
{
    cout << "NEO TOKYO CHIYODA WARD - PARKING VIOLATION NOTICE\n\n";
    cout << "Allowed parking time exceeded\n\n";
    cout << "Minutes Parked:    " << violator.getMinutesParked() << "\n";
    cout << "Minutes Purchased: " << parkingMeter.getTimePurchased() << "\n\n";
   
    cout << "REPORTING PATROL OFFICER\n";
    cout << "------------------------\n\n";
    cout << issuer << "\n";

    cout << "VIOLATOR\n";
    cout << "--------\n";
    cout << violator << "\n";

    cout << fixed << setprecision(2);

    cout << "FINE\n";
    cout << "----\n";

    if (getFine() == BASE_FINE)
    {
        cout << "Base Fine:   NTY " << BASE_FINE << "\n";
    }
    else
    {
        cout << "Base Fine:   NTY " << BASE_FINE << " +\n";
        cout << "Hourly Fine: NTY " << getHourlyFine() << "\n";
    }
    cout << "------------------------\n";
    cout << "Total Fine:  NTY " << getFine() << "\n\n\n";
}

ParkingTicketSimulator.cpp


#include "PoliceOfficer.h"
#include "ParkedCar.h"
#include "ParkingMeter.h"

#include <iostream>
using std::cin;
using std::cout;

#include <vector>
using std::vector;

int main()
{
    int numTicketsIssued = 0;

    vector<ParkedCar> car { ParkedCar("Porsche", "Cayenne", "Cyan", "NT-923865", 480),
                                   ParkedCar("Toyota", "Vellfire 2.5 X MPV", "Black", "NT-239575", 25),
                                   ParkedCar("Mazda", "2 G115 Revolution Top", "Machine Gray", "NT-392755", 61),
                                   ParkedCar("Alfa", "Giulietta Exclusive", "Nero Etna", "NT-23975", 55) };

    vector<ParkingMeter> meter { ParkingMeter(120),
                                          ParkingMeter(30),
                                          ParkingMeter(60),
                                          ParkingMeter(60) };

    PoliceOfficer officer;

    cout << "NEO TOKYO CHIYODA WARD - PARKING TICKET SIMULATOR\n\n";
    cout << "Assign Patrol Officer\n\n";
    cin >> officer;
    cout << "\n";

    for (size_t idx = 0; idx < meter.size(); idx++)
    {
        if (officer.isViolator(meter[idx], car[idx]) == true)
        {
            officer.issueTicket(meter[idx], car[idx]);
            numTicketsIssued++;
        }
    }

    if (numTicketsIssued == 0)
    {
        cout << "Chiyoda Ward's streets are safe!\n"
              << "There have been no parking violation reports.";
    }
   
    cin.get();
    cin.ignore();
    return 0;
}

Example Output:








No comments:

Post a Comment