design a set of C++ classes that work together to simulate a police officer issuing a parking ticket
Parking Ticket Simulator
For this assignment you will design a set of C++ classes that work together to simulate a
police officer issuing a parking ticket. The classes you should design are:
• The ParkedCar Class: This class should simulate a parked car. The class’s
responsibilities are:
To know the car’s make, model, color, license number, and the number of
minutes that the ca r has been parked
• The ParkingMeter Class: This class should simulate a parking meter. The class’s
only responsibility is:
To know the number of minutes of parking rime that has been purchased
Review Questions and Exercises
• The ParkingTicket Class: This class should simulate a parking ticket. The
class’s responsibilifies are:
To report the make, model, color, and license number of th e illegally
parked car
To report the amount of the fine, which is $25 for the first hour or part of an
hour that the car is illegally parked, plus $10 for every additional hour or
parr of an hour that the ca r is illegally parked
To report the name and badge number of the police officer issuing the
ticket
• The PoliceOffieer Class: This class should simula te a police officer inspecting
parked cars. The class’s responsibilities are:
To know the police officer’s name and badge number
To examine a ParkedCar object and a parkingMeter object, and determine
whether the car’s time has expired
To issue a parking ticker (generate a ParkingTicket object) if the car’s time
has expired
Write a program that demonstrates how these classes collaborate.
Answer:
Parking Ticket Simulator
#include <iostream> #include "ParkedCar.h" #include "ParkingMeter.h" #include "ParkingTicket.h" #include "PoliceOfficer.h" using namespace std; int main() { // Create a ParkingTicket pointer. If a parking ticket // is issued, this will point to it. ParkingTicket *ticket = NULL;; // Create a ParkedCar object. // The car was parked for 125 minutes. ParkedCar car("Volkswagen", "1972", "Red", "147RHZM", 125); // Create a ParkingMeter object. // 60 minutes were purchased. ParkingMeter meter(60); // Create a PoliceOfficer object. PoliceOfficer officer("Joe Friday", "4788"); // Let the officer patrol. ticket = officer.patrol(car, meter); if (ticket != NULL) { // Display the officer information. officer.print(); // Display the ticket information. ticket->print(); // We're done with the ticket, so delete it. delete ticket; ticket = NULL; } else cout << "No crimes were committed.\n"; return 0; }
Implementation file for the ParkedCar class#include <iostream> #include "ParkedCar.h" using namespace std; // Default Constructor ParkedCar::ParkedCar() { make = ""; model = ""; color = ""; licenseNumber = ""; minutesParked = 0; } // Constructor // Parameters: // mk The car's make. // mod The car's model. // col The car's color. // lic The car's license number. // min The number of minutes parked. ParkedCar::ParkedCar(string mk, string mod, string col, string lic, int min) { make = mk; model = mod; color = col; licenseNumber = lic; minutesParked = min; } // Copy constructor ParkedCar::ParkedCar(const ParkedCar &car2) { make = car2.make; model = car2.model; color = car2.color; licenseNumber = car2.licenseNumber; minutesParked = car2.minutesParked; } // print function void ParkedCar::print() { cout << "Car Information:\n"; cout << "\tMake: " << make << endl; cout << "\tmodel: " << model << endl; cout << "\tColor: " << color << endl; cout << "\tLicense Number: " << licenseNumber << endl; cout << "\tMinutes Parked: " << minutesParked << endl; }
Specification file for the ParkedCar class#ifndef PARKED_CAR_H #define PARKED_CAR_H #include<string> using namespace std; // ParkedCar class class ParkedCar { private: string make; // The car's make string model; // The car's model string color; // The car's color string licenseNumber; // The car's license number int minutesParked; // Minutes parked public: // Default constructor ParkedCar(); // Constructor ParkedCar(string, string, string, string, int); // Copy constructor ParkedCar(const ParkedCar &); // Mutators void setMake(string m) { make = m; } void setModel(string m) { model = m; } void setColor(string c) { color = c; } void setLicenseNumber(string lic) { licenseNumber = lic; } void setMinutesParked(int m) { minutesParked = m; } // Accessors string getMake() const { return make; } string getModel() const { return model;} string getColor() const { return color; } string getLicenseNumber() const { return licenseNumber; } int getMinutesParked() const { return minutesParked; } // print function void print(); }; #endif
Specification file for the ParkingMeter class#ifndef PARKING_METER_H #define PARKING_METER_H #include <iostream> using namespace std; // ParkingMeter class class ParkingMeter { private: int minutesPurchased; // Minutes purchased public: // Default constructor ParkingMeter() { minutesPurchased = 0; } // Constructor. The parameter m is // the number of minutes purchased. ParkingMeter(int m) { minutesPurchased = m; } // Mutator void setMinutesPurchased(int m) { minutesPurchased = m; } // Accessor int getMinutesPurchased() const { return minutesPurchased; } // print function void print() { cout << "Meter Information:\n"; cout << "\tMinutes Purchases: " << minutesPurchased << endl; } }; #endif
Implementation file for the ParkingTicket class#include "ParkingTicket.h" #include <iostream> #include <iomanip> using namespace std; // Default constructor ParkingTicket::ParkingTicket() { fine = 0.0; minutes = 0; } // Constructor // Parameters: // aCar - A ParkedCar object. // min - Minutes illegally parked. ParkingTicket::ParkingTicket(ParkedCar aCar, int min) { car = aCar; minutes = min; // Calculate the fine. calculateFine(); } // Copy constructor ParkingTicket::ParkingTicket(const ParkingTicket &ticket2) { car = ticket2.car; fine = ticket2.fine; } // calculateFine method // This method calculates the amount of the parking fine. void ParkingTicket::calculateFine() { // Get the time parked in hours. double hours = minutes / 60.0; // Get the hours as an int. int hoursAsInt = static_cast<int>(hours); // If there was a portion of an hour, round up. if ((hours - hoursAsInt) > 0) hoursAsInt++; // Assign the base fine. fine = BASE_FINE; // Add the additional hourly fines. fine += (hoursAsInt * HOURLY_FINE); } // print function void ParkingTicket::print() { // Print car information. car.print(); // Print ticket information. cout << "Ticket Information:\n"; cout << "\tMinutes in violation: " << minutes << endl; cout << "\tFine: $ " << setprecision(2) << fixed << showpoint << fine << endl; }
Specification file for the ParkingTicket class#ifndef PARKING_TICKET_H #define PARKING_TICKET_H #include "ParkedCar.h" // Constant for the base fine. const double BASE_FINE = 25.0; // Constant for the additional hourly fine. const double HOURLY_FINE = 10.0; // ParkingTicket class class ParkingTicket { private: ParkedCar car; // The parked car double fine; // The parking fine int minutes; // Minutes illegally parked // calculateFine method // This method calculates the amount of the parking fine. void calculateFine(); public: // Default Constructor ParkingTicket(); // Constructor ParkingTicket(ParkedCar, int); // Copy constructor ParkingTicket(const ParkingTicket &); // Mutators void setCar(ParkedCar c) { car = c; } void setMinutes(int m) { minutes = m; } // Accessors ParkedCar getCar() const { return car; } double getFine() const { return fine; } // print function void print(); }; #endif
Implementation file for the PoliceOfficer class#include "ParkedCar.h" #include "ParkingMeter.h" #include "ParkingTicket.h" #include "PoliceOfficer.h" #include <iostream> using namespace std; // The patrol function looks at the number of // minutes a car has been parked and the number // of minutes purchased. If the minutes parked is // greater than the minutes purchased, a pointer // to a ParkingTicket object is returned. Otherwise // the function returns NULL. ParkingTicket* PoliceOfficer::patrol(ParkedCar car, ParkingMeter meter) { // Get the minutes parked over the amount purchased. int illegalMinutes = car.getMinutesParked() - meter.getMinutesPurchased(); // Determine whether the car is illegally parked. if (illegalMinutes > 0) { // Yes, it is illegally parked. // Create a ParkingTicket object. ticket = new ParkingTicket(car, illegalMinutes); } // Return the ticket, if any. return ticket; } // print function void PoliceOfficer::print() { cout << "Police Officer Information:\n"; cout << "\tName: " << name << endl; cout << "\tBadge Number: " << badgeNumber << endl; }
Specification file for the PoliceOfficer class
#ifndef POLICE_OFFICER_H #define POLICE_OFFICER_H #include "ParkedCar.h" #include "ParkingMeter.h" #include "ParkingTicket.h" // PoliceOfficer class class PoliceOfficer { private: string name; // Officer's name string badgeNumber; // Badge number ParkingTicket *ticket; // Pointer to a ticket public: // Default constructor PoliceOfficer() { name = ""; badgeNumber = ""; ticket = NULL; } // Constructor // The parameter n is the officer's name. // The parameter bn is the officer's badge number. PoliceOfficer(string n, string bn) { name = n; badgeNumber = bn; ticket = NULL; } // Copy constructor PoliceOfficer(const PoliceOfficer &officer2) { name = officer2.name; badgeNumber = officer2.badgeNumber; ticket = new ParkingTicket(*officer2.ticket); } // Mutators void setName(string n) { name = n; } void setBadgeNumber(string b) { badgeNumber = b; } // Accessors string getName() { return name; } string getBadgeNumber() { return badgeNumber; } // patrol function ParkingTicket *patrol(ParkedCar, ParkingMeter); // print function void print(); }; #endif
Leave a reply