In: Computer Science
Code should be written in C++ using Visual Studios Community
This requires several classes to interact with each other. Two class aggregations are formed. The program will simulate a police officer giving out tickets for parked cars whose meters have expired.
You must include both a header file and an implementation file for each class.
Car class (include Car.h and Car.cpp) Contains the information about a car.
ParkedCar class (include ParkedCar.cpp ParkedCar.h) simulates a parked car. Knows which type of car is parked and the number of minutes that the car has been parked.
ParkingMeter class (include ParkingMeter.h and ParkingMeter.cpp) simulates a parking meter. Should know the number of minutes of parking time that has been purchased.
ParkingTicket class (include ParkingTicket.h and ParkingTicket.cpp) simulates a parking ticket.
PoliceOfficer class (include PoliceOffier.h and PoliceOfficer.cpp)
Main should test the interactions of all the classes. The main example below is how the program should work. This is not a complete main. The program should have a car that is not given a ticket, one with a ticket within an hour and another for more than one hour.
EXAMPLE
#include #include "ParkedCar.h" #include "ParkingMeter.h" #include "ParkingTicket.h" #include "PoliceOfficer.h" using namespace std; int main() { ParkingTicket *ticket = nullptr; ParkedCar car("Volkswagen", "1972", "Red", "147RHZM", 125); ParkingMeter meter(60); PoliceOfficer officer("Joe Friday", "4788"); ticket = officer.patrol(car, meter); if (ticket != nullptr) { cout << officer; cout << *ticket; delete ticket; ticket = nullptr; } else cout << "No crimes were committed.\n"; return 0; }
car.h
#pragma once
#include<iostream>
using namespace std;
class Car {
#pragma region Data Members
string _make, _model, _color, _licenseNumber;
#pragma endregion
public:
#pragma region functions
//setters
void setMake(string make);
void setModel(string model);
void setColor(string color);
void setLicenseNum(string licenseNumber);
//getters
string getMake();
string getModel();
string getColor();
string getLicenseNumber();
//constructor
Car();
//operator overloading
friend ostream& operator<<(ostream&,Car);
#pragma endregion
};
car.cpp
#include"Car.h"
#include<iostream>
#include<string>
#define MakeSize 10
using namespace std;
//setters
void Car::setMake(string make){ this->_make = make;}
void Car::setModel(string model){this->_model = model;}
void Car::setColor(string color){this->_color = color;}
void Car::setLicenseNum(string licenseNumber) {
this->_licenseNumber = _licenseNumber; }
//getters
string Car::getMake() { return this->_make; }
string Car::getModel() { return this->_model; }
string Car::getColor() { return this->_color; }
string Car::getLicenseNumber() { return this->_licenseNumber;
}
//constructor
Car::Car(){
this->_color = "";
this->_licenseNumber = "";
this->_make = "";
this->_model = "";
}
//operator overloading
ostream& operator<<(ostream& out,Car car) {
out << "Make : " + car.getMake() << endl;
out << "Model : " + car.getModel() << endl;
out << "Color : " + car.getColor() << endl;
out << "License Number : " + car.getLicenseNumber() <<
endl;
return out;
}
parkedcar.h
#pragma once
#include<iostream>
#include"Car.h"
class ParkedCar
{
int _minutes;
Car _car;
public:
//setters
void setCar(Car);
void setMinutes(int);
//getters
int getMinutes();
Car getCar();
//constructors
ParkedCar();
ParkedCar(string, string, string, string, int);
ParkedCar(const ParkedCar&);
//operator overloading
friend ostream& operator<<(ostream&,ParkedCar);
};
parkedcar.cpp
#include "ParkedCar.h"
using namespace std;
//setters
void ParkedCar::setMinutes(int min) { this->_minutes = min;
}
void ParkedCar::setCar(Car car) { this->_car = car; }
//getters
int ParkedCar::getMinutes() { return this->_minutes; }
Car ParkedCar::getCar() { return this->_car; }
//constructors
ParkedCar::ParkedCar(){
this->_minutes = 0;
}
ParkedCar::ParkedCar(string carMake, string carModel, string
carColor, string carLicense, int minParked) {
this->_car.setMake(carMake);
this->_car.setModel(carModel);
this->_car.setColor(carColor);
this->_car.setLicenseNum(carLicense);
this->_minutes = minParked;
}
ParkedCar::ParkedCar(const ParkedCar& parkedCar) {
// no hints are given for woorking of copy constructor
}
//operator overloading
ostream& operator<<(ostream& out, ParkedCar car)
{
out << car;
out << "minutes : " + car.getMinutes()<<endl;
return out;
}
parkingmeter.h
#pragma once
#include<iostream>
using namespace std;
class ParkingMeter
{
int _minPurchased;
public:
//setter
void setMinPurchased(int);
//getter
int getMinPurchased();
//constructors
ParkingMeter();
ParkingMeter(int);
//operator overloading
friend ostream& operator<<(ostream&,
ParkingMeter);
};
parkingmeter.cpp
#include "ParkingMeter.h"
#include<iostream>
using namespace std;
//setter
void ParkingMeter::setMinPurchased(int minPurchase) {
this->_minPurchased = minPurchase; }
//getter
int ParkingMeter::getMinPurchased() { return
this->_minPurchased; }
//constructors
ParkingMeter::ParkingMeter(){
this->_minPurchased = 0;
}
ParkingMeter::ParkingMeter(int minPurchase) {
this->_minPurchased = minPurchase; }
//operator overloading
ostream& operator<<(ostream& out, ParkingMeter
meter)
{
out << "Parking Minutes purchased : " +
meter.getMinPurchased() << endl;
return out;
}
parkingticket.h
#pragma once
#include"ParkedCar.h"
class ParkingTickets
{
ParkedCar _parkedCar;
double _fine;
int _min;
double _calculateFine();
public:
//setters
void setParkedCar(ParkedCar);
void setFine(double);
void setMinIllegalParkingTime(int);
//getters
ParkedCar getParkedCar();
double getFine();
int getIllegalMin();
//constructors
ParkingTickets();
ParkingTickets(ParkedCar, int);
//operator overloading
friend ostream&
operator<<(ostream&,ParkingTickets);
};
parkingtickets.cpp
#include "ParkingTickets.h"
double ParkingTickets::_calculateFine(){
int temp = 0;
//for first hour
this->_fine = 25;
temp %= 60;
while (temp > 0) {
this->_fine += 10;
temp %= 60;
}
return temp;
}
//setters
void ParkingTickets::setParkedCar(ParkedCar car) {
this->_parkedCar = car; }
void ParkingTickets::setFine(double fine) { this->_fine = fine;
}
void ParkingTickets::setMinIllegalParkingTime(int min) {
this->_min = min; }
//getters
ParkedCar ParkingTickets::getParkedCar() { return
this->_parkedCar; }
double ParkingTickets::getFine() { return this->_fine; }
int ParkingTickets::getIllegalMin() { return this->_min; }
//constructors
ParkingTickets::ParkingTickets(){
this->_fine = 90;
this->_min = 0;
}
ParkingTickets::ParkingTickets(ParkedCar car, int min) {
this->_parkedCar = car;
this->_min = min;
}
//operator overloading
ostream& operator<<(ostream& out, ParkingTickets
ticket) {
out << ticket.getParkedCar() << endl;
out << "Illegal minutes : " + ticket.getIllegalMin() <<
endl;
out << "Fine : " << ticket.getFine() <<
endl;
return out;
}
policeofficer.h
#pragma once
#include<iostream>
#include"ParkingTickets.h"
#include"ParkingMeter.h"
using namespace std;
class PoliceOfficer
{
string _name, _badge;
ParkingTickets *_ticket;
public:
//setters
void setName(string);
void setBadge(string);
//getters
string getName();
string getBadge();
ParkingTickets* Patrol(ParkedCar,ParkingMeter);
//constructor
PoliceOfficer();
PoliceOfficer(string, string);
//operator overloading
friend ostream&
operator<<(ostream&,PoliceOfficer);
};
policeofficer.cpp
#include "PoliceOfficer.h"
#include<string>
//setters
void PoliceOfficer::setName(string name) { this->_name = name;
}
void PoliceOfficer::setBadge(string badge) { this->_badge =
badge; }
//getters
string PoliceOfficer::getName() { return this->_name; }
string PoliceOfficer::getBadge() { return this->_badge; }
ParkingTickets* PoliceOfficer::Patrol(ParkedCar car, ParkingMeter
meter) {
if (meter.getMinPurchased() <= car.getMinutes())//if time is
expire, issue new ticket
this->_ticket = new
ParkingTickets(car,meter.getMinPurchased());//this will double the
purchased time of the car
return this->_ticket;
}
//constructor
PoliceOfficer::PoliceOfficer() { this->_name = "";
this->_badge = ""; }
PoliceOfficer::PoliceOfficer(string name, string badge) {
this->_name = name;
this->_badge = badge;
}
//operator overloading
ostream& operator<<(ostream &out,PoliceOfficer
officer) {
out << "Name : "+officer.getName() << endl;
out << "Enter Badge : "+ officer.getBadge() <<
endl;
return out;
}
main
#include <iostream>
#include "ParkedCar.h"
#include "ParkingMeter.h"
#include "ParkingTickets.h"
#include "PoliceOfficer.h"
using namespace std;
int main()
{
ParkingTickets *ticket = nullptr;
ParkedCar car("Volkswagen", "1972", "Red", "147RHZM", 125);
ParkingMeter meter(60);
PoliceOfficer officer("Joe Friday", "4788");
ticket = officer.Patrol(car, meter);
if (ticket != nullptr)
{
cout << officer;
cout << *ticket;
delete ticket;
ticket = nullptr;
}
else
cout << "No crimes were committed.\n";
system("pause");
return 0;
}