In: Computer Science
Using C++. For this assignment you will design a set of 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 car 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 time that has been purchased.
The ParkingTicket Class: This clas should simulate a aprking ticket. The class's responsibilities are:
-To report the make, model, color, and license number of the 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 illegal parked, plus $10 for every additional hour or part of an hour that the car is illegally parked
-To report the name and badge number of the police officer issuing the ticket
The PoliceOfficer Class: This class should simulate a police officer inspecting parked cars. This 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 expired
-To issue a parking ticket (generate a ParkingTicket object) if the car's time has expired
Write a program that demonstrates how these collaborate.
Below is the program for the above question statement. Go through the program and the comments to better understand the program flow and logic.
//Program to to simulate a police officer issuing a parking ticket
#include <iostream>
using namespace std;
class ParkedCar{
//car's make, model,color ,license number,and the
number of minutes that the car has been parked
//Access specifier
public:
// Data Members
string make;
string model;
string color;
string license_number;
int parked_minutes;
};
class ParkingMeter{
//To know the number of minutes of parking time that
has been purchased.
//Access specifier
public:
// Data Member
int parking_minutes_purchased;
};
class ParkingTicket {
//Access specifier
public:
//To report the make, model, color, and license number
of the illegally parked car
void printcardetails(ParkedCar pc)
{
cout << "Car's
make, model, color, and license number is given below:\n" ;
cout << pc.make
<<"\n";
cout <<
pc.model<<"\n";
cout <<
pc.color<<"\n";
cout <<
pc.license_number<<"\n";
}
//To report the amount of the fine,which is $25 for
the first hour or part of an hour that the car is illegal parked,
plus $10 for every additional hour or part of an hour that the car
is illegally parked
void printfine(ParkedCar pc, ParkingMeter pm)
{ int fine = 0;
//calculate the fine when illegal
parked time is less than or exactly one hour
int extratime = pc.parked_minutes -
pm.parking_minutes_purchased;
if(extratime<=60)
{
fine = 25;
}
else
{ //when illegal parked time is
more than an hour
fine = 25;
extratime = extratime - 60;
//for every additional hour plus $10
while(extratime>0){
fine = fine + 10;
extratime = extratime -
60;
}
}
cout << "Fine is $"<<
fine ;
cout <<"\n";
}
//To report the name and badge number of the police
officer issuing the ticket
void printofficerdetail(string name,int number)
{
cout << "Officer's name : "
<< name;
cout <<"\n";
cout << "Officer's Badge
number : " << number;
cout
<<"\n";
}
};
class PoliceOfficer : public ParkingTicket{
//Access specifier
public:
// Data Members
//To know the police officer's name and badge
number
string officername;
int badge_number;
//To examine a ParkedCar object and a ParkingMeter
object, and determine whether the car's time expired
int examineparkedcar(ParkedCar pc, ParkingMeter
pm){
if(pc.parked_minutes <=
pm.parking_minutes_purchased)
{
return 1;
}
else
return 0;
}
//To issue a parking ticket (generate a ParkingTicket
object) if the car's time has expired
void generateticket(ParkedCar pc, ParkingMeter
pm)
{ //create an objectof Paring ticket
class
ParkingTicket pt;
//generate the parking ticket
pt.printcardetails(pc);
pt.printfine(pc,pm);
pt.printofficerdetail(officername,badge_number);
}
};
int main()
{
//create object for parked car class
ParkedCar pc;
pc.make = "Honda";
pc.model = "Accord";
pc.color = "White";
pc.license_number = "XYZ11111";
pc.parked_minutes = 180;
//create object for parking meter class
ParkingMeter pm;
pm.parking_minutes_purchased= 60;
//create object for policeofficer class
PoliceOfficer po;
po.officername = "John";
po.badge_number = 1111;
//determine whether the car's time expired
if(po.examineparkedcar(pc,pm))
{
cout << "Time not
expired.Fine is Zero!" ;
}
else
{
cout << "Time Expired .\n"
;
po.generateticket(pc,pm);
}
return 0;
}
//End of the program
Please find attached the screenshot of the program and the console output.