Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Parking Ticket Simulator ( C++ ) For this assignment you will design a set of cl

ID: 3578157 • Letter: P

Question

Parking Ticket Simulator ( 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 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

Solution:

First Method: Using C++

//.h file code:

#include <iostream>
#include <conio>
#include <stdlib>
#include <string>
#include <vector>

class ParkedCar
{
private:
   std::wstring make;
   std::wstring model;
   std::wstring color;
   std::wstring licenseNumber;
   int minutesParked = 0;

public:
   ParkedCar(const std::wstring &mk, const std::wstring &mod, const std::wstring &col, const std::wstring &lic, int min);

   ParkedCar(ParkedCar *car2);

   virtual void setMake(const std::wstring &m);

   virtual void setModel(const std::wstring &m);

   virtual void setColor(const std::wstring &c);

   virtual void setLicenseNumber(const std::wstring &lic);

   virtual void setMinutesParked(int m);

   virtual std::wstring getMake();

   virtual std::wstring getModel();

   virtual std::wstring getColor();

   virtual std::wstring getLicenseNumber();

   virtual int getMinutesParked();

   virtual std::wstring toString() override;
};

//.cpp file code:

ParkedCar::ParkedCar(const std::wstring &mk, const std::wstring &mod, const std::wstring &col, const std::wstring &lic, int min)
{
   this->make = mk;
   this->model = mod;
   this->color = col;
   this->licenseNumber = lic;
   this->minutesParked = min;
}

ParkedCar::ParkedCar(ParkedCar *car2)
{
   this->make = car2->make;
   this->model = car2->model;
   this->color = car2->color;
   this->licenseNumber = car2->licenseNumber;
   this->minutesParked = car2->minutesParked;
}

void ParkedCar::setMake(const std::wstring &m)
{
   this->make = m;
}

void ParkedCar::setModel(const std::wstring &m)
{
   this->model = m;
}

void ParkedCar::setColor(const std::wstring &c)
{
   this->color = c;
}

void ParkedCar::setLicenseNumber(const std::wstring &lic)
{
   this->licenseNumber = lic;
}

void ParkedCar::setMinutesParked(int m)
{
   this->minutesParked = m;
}

std::wstring ParkedCar::getMake()
{
   return make;
}

std::wstring ParkedCar::getModel()
{
   return model;
}

std::wstring ParkedCar::getColor()
{
   return color;
}

std::wstring ParkedCar::getLicenseNumber()
{
   return licenseNumber;
}

int ParkedCar::getMinutesParked()
{
   return minutesParked;
}

std::wstring ParkedCar::toString()
{
   std::wstring str = std::wstring(L"Make: ") + make + std::wstring(L" Model: ") + model + std::wstring(L" Color: ") + color + std::wstring(L" License Number: ") + licenseNumber + std::wstring(L" Minutes Parked: ") + std::to_wstring(minutesParked);
   return str;
}

//.h file code:

#include <string>
#include <vector>
#include <iostream>

// Forward class declarations:
class PoliceOfficer;

class ParkingMeter
{
public:
   int minForParking = 0;

   virtual int getMinForParking();

   virtual void setMinForParking(int minForParking);
};

class ParkingTicket
{

public:
   int fine = 0;
   int illegallyParkedHr = 0;

   virtual int getFine();

   virtual void setFine(int fine);

   virtual void reportIllegalCar(ParkedCar *pc, ParkingMeter *pm, PoliceOfficer *po);
};

class PoliceOfficer
{
public:
   std::wstring officerName;
   int badgeNum = 0;

   virtual std::wstring getOfficerName();

   virtual void setOfficerName(const std::wstring &officerName);

   virtual int getBadgeNum();

   virtual void setBadgeNum(int badgeNum);

   virtual bool isExpired(int purchaceTime, int parkedTime);

   ParkedCar *pc = new ParkedCar(nullptr, nullptr, nullptr, nullptr, 0);
   ParkingMeter *pm = new ParkingMeter();

  
};

//.cpp file code:

void main(std::vector<std::wstring> &args)
{
clrscr();
int ParkingMeter::getMinForParking()
{
   return minForParking;
}

void ParkingMeter::setMinForParking(int minForParking)
{
   this->minForParking = minForParking;
}

int ParkingTicket::getFine()
{
   return fine;
}

void ParkingTicket::setFine(int fine)
{
   this->fine = fine;
}

void ParkingTicket::reportIllegalCar(ParkedCar *pc, ParkingMeter *pm, PoliceOfficer *po)
{
   cout << std::wstring(L"Details of illegally parked car:") << std::endl;
   cout << std::wstring(L"Car's make :") << pc->getMake() << std::endl;
   cout << std::wstring(L"Car's model :") << pc->getModel() << std::endl;
   cout << std::wstring(L"Car's color :") << pc->getColor() << std::endl;
   cout << std::wstring(L"Car's licence number :") << pc->getLicenseNumber() << std::endl;

   illegallyParkedHr = ((pc->getMinutesParked() - pm->getMinForParking()) / 60) + 1;
   if (illegallyParkedHr <= 1)
   {
       setFine(25);
   }
   else
   {
       setFine(25 + ((illegallyParkedHr - 1) * 10));
   }

   cout << std::wstring(L"Fine is : $") << getFine() << std::endl;
   cout << std::wstring(L"Name of officier :") << po->getOfficerName() << std::endl;
   cout << std::wstring(L"Badge name :") << po->getBadgeNum() << std::endl;
}

std::wstring PoliceOfficer::getOfficerName()
{
   return officerName;
}

void PoliceOfficer::setOfficerName(const std::wstring &officerName)
{
   this->officerName = officerName;
}

int PoliceOfficer::getBadgeNum()
{
   return badgeNum;
}

void PoliceOfficer::setBadgeNum(int badgeNum)
{
   this->badgeNum = badgeNum;
}

bool PoliceOfficer::isExpired(int purchaceTime, int parkedTime)
{
   if (parkedTime - purchaceTime > 0)
   {
       return true;
   }
   else
   {
       return false;
   }
}

void PoliceOfficer::main(std::vector<std::wstring> &args)
{
   bool status;

   PoliceOfficer *po = new PoliceOfficer();
   cout << std::wstring(L"Enter the name of police officer :") << std::endl;
   Scanner *input = new Scanner(System::in);
   po->setOfficerName(input->nextLine());
   cout << std::wstring(L"Enter the badge number :") << std::endl;
   po->setBadgeNum(input->nextInt());

   cout << std::wstring(L"Enter the car's make:") << std::endl;
   po->pc->setMake(input->next());
   cout << std::wstring(L"Enter the car's model:") << std::endl;
   po->pc->setModel(input->next());
   cout << std::wstring(L"Enter the car's color:") << std::endl;
   po->pc->setColor(input->next());
   cout << std::wstring(L"Enter the car's licence number:") << std::endl;
   po->pc->setLicenseNumber(input->next());
   cout << std::wstring(L"Enter the no. of minutes car parked:") << std::endl;
   po->pc->setMinutesParked(input->nextInt());
   cout << std::wstring(L"Enter the no of minutes puchased for parking:") << std::endl;
   po->pm->setMinForParking(input->nextInt());

   status = po->isExpired(po->pm->getMinForParking(), po->pc->getMinutesParked());

   if (status == true)
   {

       ParkingTicket *pt = new ParkingTicket();
       pt->reportIllegalCar(po->pc, po->pm, po);
   }
   else
   {
   cout << std::wstring(L"Car is Legally Parked.") << std::endl;
   }
getch();
}


Second Method: using Java

package PoliceOfficer;

import java.util.Scanner;

class ParkedCar {
private String make;
private String model;
private String color;
private String licenseNumber;
private int minutesParked;

public ParkedCar(String mk, String mod, String col, String lic, int min) {
this.make = mk;
this.model = mod;
this.color = col;
this.licenseNumber = lic;
this.minutesParked = min;
}

public ParkedCar(ParkedCar car2) {
this.make = car2.make;
this.model = car2.model;
this.color = car2.color;
this.licenseNumber = car2.licenseNumber;
this.minutesParked = car2.minutesParked;
}

public void setMake(String m) {
this.make = m;
}

public void setModel(String m) {
this.model = m;
}

public void setColor(String c) {
this.color = c;
}

public void setLicenseNumber(String lic) {
this.licenseNumber = lic;
}

public void setMinutesParked(int m) {
this.minutesParked = m;
}

public String getMake() {
return make;
}

public String getModel() {
return model;
}

public String getColor() {
return color;
}

public String getLicenseNumber() {
return licenseNumber;
}

public int getMinutesParked() {
return minutesParked;
}

public String toString() {
String str = "Make: " + make + " Model: " + model + " Color: "
+ color + " License Number: " + licenseNumber
+ " Minutes Parked: " + minutesParked;
return str;
}
}

class ParkingMeter {
int minForParking;

public int getMinForParking() {
return minForParking;
}

public void setMinForParking(int minForParking) {
this.minForParking = minForParking;
}
}

class ParkingTicket {

int fine;
int illegallyParkedHr;

public int getFine() {
return fine;
}

public void setFine(int fine) {
this.fine = fine;
}

void reportIllegalCar(ParkedCar pc, ParkingMeter pm, PoliceOfficer po) {
System.out.println("Details of illegally parked car:");
System.out.println("Car's make :" + pc.getMake());
System.out.println("Car's model :" + pc.getModel());
System.out.println("Car's color :" + pc.getColor());
System.out.println("Car's licence number :" + pc.getLicenseNumber());

illegallyParkedHr = ((pc.getMinutesParked() - pm.getMinForParking()) / 60) + 1;
if (illegallyParkedHr <= 1)
setFine(25);
else
setFine(25 + ((illegallyParkedHr - 1) * 10));

System.out.println("Fine is : $" + getFine());
System.out.println("Name of officier :" + po.getOfficerName());
System.out.println("Badge name :" + po.getBadgeNum());
}
}

public class PoliceOfficer {
String officerName;
int badgeNum;

public String getOfficerName() {
return officerName;
}

public void setOfficerName(String officerName) {
this.officerName = officerName;
}

public int getBadgeNum() {
return badgeNum;
}

public void setBadgeNum(int badgeNum) {
this.badgeNum = badgeNum;
}

boolean isExpired(int purchaceTime, int parkedTime) {
if (parkedTime - purchaceTime > 0)
return true;
else
return false;
}

ParkedCar pc = new ParkedCar(null, null, null, null, 0);
ParkingMeter pm = new ParkingMeter();

public static void main(String[] args) {
boolean status;

PoliceOfficer po = new PoliceOfficer();
System.out.println("Enter the name of police officer :");
Scanner input = new Scanner(System.in);
po.setOfficerName(input.nextLine());
System.out.println("Enter the badge number :");
po.setBadgeNum(input.nextInt());

System.out.println("Enter the car's make:");
po.pc.setMake(input.next());
System.out.println("Enter the car's model:");
po.pc.setModel(input.next());
System.out.println("Enter the car's color:");
po.pc.setColor(input.next());
System.out.println("Enter the car's licence number:");
po.pc.setLicenseNumber(input.next());
System.out.println("Enter the no. of minutes car parked:");
po.pc.setMinutesParked(input.nextInt());
System.out.println("Enter the no of minutes puchased for parking:");
po.pm.setMinForParking(input.nextInt());

status = po.isExpired(po.pm.getMinForParking(),
po.pc.getMinutesParked());

if (status == true) {

ParkingTicket pt = new ParkingTicket();
pt.reportIllegalCar(po.pc, po.pm, po);
} else
System.out.println("Car is Legally Parked.");
}
}

Output:

Enter the name of police officer :Charlee
Enter the badge number :26
Enter the car's make:Alpha
Enter the car's model:B35
Enter the car's color:Red
Enter the car's licence number:543/B
Enter the no. of minutes car parked:1673
Enter the no of minutes puchased for parking:1500


Details of illegally parked car:
Car's make :Alpha
Car's model :B35
Car's color :Red
Car's licence number :543/B
Fine is : $45
Name of officier :Charlee
Badge name :26