I need helping creating this program in c++ language...i couldn\'t find the solu
ID: 3825979 • Letter: I
Question
I need helping creating this program in c++ language...i couldn't find the solution on the textbook solution section.
Parking Ticket Simulator
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 class should simulate a parking 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 illegally 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. 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 ticket (generate a ParkingTicket object) if the car’s time has
expired
Write a program that demonstrates how these classes collaborate.
Explanation / Answer
Ans.
#include<iostream>
#include<string.h>
using namespace std;
int m=0, min, k[10], c;
class ParkedCar{
private:
string carMake;
int model;
string color;
string licenseNumber;
int minutesParked;
public:
ParkedCar(){
carMake=" ";
model=0;
licenseNumber=" ";
color=" ";
minutesParked=0;
}
void setCarMake(string make)
{
carMake=make;
}
string getCarMake() const
{
return (carMake);
}
void setModel(int model1)
{
model=model1;
}
int getModel() const
{
return (model);
}
void setColor(string color1)
{
color=color1;
}
string getColor() const
{
return (color);
}
void setLicenseNumber(string licenseNumber1)
{
licenseNumber=licenseNumber1;
}
string getLicenseNumber() const
{
return (licenseNumber);
}
void setMinutesParked(int minutesParked1)
{
minutesParked=minutesParked1;
}
int getMinutesParked() const
{
return (minutesParked);
}
void print();
};
void ParkedCar::print() //outside class definition
{
cout<<"Car Make :"<<getCarMake()<<endl;
cout<<"Car Model :"<<getModel()<<endl;
cout<<"Car Color :"<<getColor()<<endl;
cout<<"Car License Number :"<<getLicenseNumber()<<endl;
cout<<"Car Color :"<<getMinutesParked()<<endl;
}
class ParkingTicket:public ParkedCar //class calculates and shows
{
private :
int fine;
int minutes;
public:
void calulateFine(); //Method to calculate fine
void showFine(); //Method to show the fine
void getTicket (int min)
{
minutes=min;
calulateFine();
cout<<endl;
}
int getFine() const
{
return(fine);
}
};
void ParkingTicket::calulateFine() //outside class definition
{
if(minutes/60<=0)
{
fine = 45;
}
else
{
int min;
min=minutes - 60;
fine = 45 + 30 + 30*(min/60);
}
}
void ParkingTicket::showFine() //outside class definition
{
cout<<" ILLEGAL PARKING"<<endl;
cout<<" Time in violation is "<<minutes/60<<" hours & "<<minutes
%60<<" minutes "<<endl;
cout<<" Fine : Rs. "<<getFine()<<endl;
}
ParkingTicket ticketObject[10]; //Parking ticket array of objects created
class ParkingMeter //This class take carea of number of minutes purchased
{
int minPurchased;
public:
ParkingMeter()
{
minPurchased=0;
}
void setMinPurchased(int minPurchased1)
{
minPurchased=minPurchased1;
}
int getMinPurchased() const
{
return(minPurchased);
}
void print()
{
cout<<"Mins purchased are"<<getMinPurchased();
}
};
class PoliceOfficer //Responsible for patrolling and issuing parkingticket
{ //in case vehicle is illegally parked
string name;
string badgeNumber;
ParkingTicket *ticket; //Pointer of the type parkingticket class
public:
PoliceOfficer()
{
name=" ";
badgeNumber=" ";
ticket = NULL;
}
void setName(string name1)
{
name=name1;
}
void setBadgeNumber(string badgeNumber1)
{
badgeNumber=badgeNumber1;
}
string getName() const
{
return(name);
}
string getBadgeNumber() const
{
return(badgeNumber);
}
ParkingTicket* petrol(ParkingTicket pc1, ParkingMeter pc2) //Petrol function
{
if ( pc1.getMinutesParked() < pc2.getMinPurchased() ||
pc1.getMinutesParked() == pc2.getMinPurchased() )
{
return NULL; //No crimes
}
else //Illegal parking
{
ticketObject[m].getTicket(pc1.getMinutesParked() - pc2.getMinPurchased());
cout<<"--------------------------------------------------------------------------------"<<endl;
ticketObject[m].showFine();
ticket=&ticketObject[m];
return(ticket);
}
}
void print()
{
cout<<"Name: "<<getName()<<endl;
cout<<"Badge Number: "<<getBadgeNumber()<<endl;
}
};
void intro()
{
cout<<"******************************************* *************************************"<<endl;
cout<<" THIS IS PARKING TICKET SIMULATOR"<<endl;
cout<<"******************************************* *************************************"<<endl;
}
void choice()
{
cin>>c;
if(c==0)
{
m++;
}
else if(c==1)
{
}
else
{
cout<<"Invalid Entry, try again :"<<endl;
choice();
}
}
int main()
{
int i=0, y;
intro();
cout<<"PARKING RATES ::"<<endl;
cout<<"Parking rate is Rs. 25 for 60 minutes"<<endl;
cout<<endl<<endl;
cout<<"FINE RATES ::"<<endl;
cout<<"In case number of minutes car has been parked exceeds the number of minutes purchased, then a fine is applied."<<endl;
cout<<"Rs 45 for first hour or part of an hour that the car is illegally parked and Rs 30 for every additional hour or part of an hour that the car is illegally parked."<<endl;
cout<<"___________________________________________ _____________________________________"<<endl;
PoliceOfficer officer; //Police Officer object created
string pName, pBadge;
cout<<"OFFICER'S INFORMATION ::"<<endl;
cout<<"Name: ";
cin>>pName;
cout<<"Badge Number: ";
cin>>pBadge;
officer.setName(pName);
officer.setBadgeNumber(pBadge);
ParkingMeter meter[10]; //Parking meter array of objects created
ParkingTicket* ticket = NULL;
do //Iteration of statements
{
intro();
cout<<"PARKED CAR'S INFORMATION ::"<<endl; //Officer inputs the data of the car
string make;
cout<<"Make :";
cin>>make;
ticketObject[m].setCarMake(make);
int model;
cout<<"Model :";
cin>>model;
ticketObject[m].setModel(model);
string license;
cout<<"License number :";
cin>>license;
y=0;
while(y<=m)
{
if (y!=m)
{
if (license == ticketObject[y].getLicenseNumber()) //License number being a primary key
{
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<"Car with this License Number already exists"<<endl;
ticketObject[y].print();
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<"Try re-entering the license number :";
cin>>license;
}
}
y++;
}
ticketObject[m].setLicenseNumber(license);
string color;
cout<<"Color :";
cin>>color;
ticketObject[m].setColor(color);
int parkedMin; //The number of minutes a car has been parked
cout<<"Minutes in parking :";
cin>>parkedMin;
ticketObject[m].setMinutesParked(parkedMin);
int purchasedMin; //The number of minutes purchased
cout<<"Minutes purchased :";
cin>>purchasedMin;
meter[m].setMinPurchased(purchasedMin);
intro();
ticket = officer.petrol(ticketObject[m], meter[m]); //The officer patrols
if(ticket==NULL)
{
// Display the ticket information.
ticketObject[m].print();
cout<<endl;
cout<<endl;
cout<<"----------------------"<<endl;
cout<<"| NO CRIMES COMMITTED |"<<endl;
cout<<"----------------------"<<endl;
}
else
{
cout<<endl<<"Non-permitted vehicle ::"<<endl;
ticket->print(); // Display the ticket information.
cout<<"--------------------------------------------------------------------------------"<<endl;
ticket = NULL;
}
k[m] = ticketObject[m].getMinutesParked() - meter[m].getMinPurchased();
cout<<endl<<endl<<endl<<endl<<endl;
cout<<"___________________________________________ _____________________________________"<<endl;
cout<<"Enter your choice -"<<endl;
cout<<"0 - Petrol another car 1 - View cars patrolled"<<endl;
choice();
}while (c == 0);
intro();
for(i=0;i<=m;i++)
{
cout<<" PARKED CAR "<<i+1<<endl<<endl;
ticketObject[i].print();
cout<<endl<<endl;
if (k[i]>0)
{
ticketObject[i].showFine();
cout<<"--------------------------------------------------------------------------------"<<endl;
}
else
{
cout<<"Vehicle permitted for "<<-k[i]/60<<" hours & "<<-k[i]%60<<"minutes"<<endl;
}
cout<<"=========================================== ====================================="<<endl;
}
cout<<"Officer responsible for issuing these tickets and patroling ::"<<endl;
officer.print();
return 0;
};