In this project, you will be doing the following:

  • Starting with some files provided to you that are the driver for the parking lot simulator (download from the assignment)
  • You will be building classes like in assignment #2, but the inheritance hierarchy will be shallower. You will use your class hierarchy with the simulation driver that is provided.
  • You will do some minor updates to the driver using the random_shuffle STL algorithm.
  • You will demonstrate your understanding on the driver code by commenting the driver code.

Below is class diagram that illustrates the hierarchy you are expected to create.

Figure: see image.

Constructors and Operators

Constructors: For each class, the default and parameterized constructors should be implemented and have appropriate error detection. For the parameterized constructor, order your parameters as listed above.

Operators: The << operator should be overloaded for each of the 4 concrete classes

Vehicle Class

Create a class named Vehicle with the following attributes and methods:

Attributes

color: The color of the vehicle
licensePlate: A license plate number
make: manufacturer of the vehicle
theID: the internal unique ID for the car

Methods

Base Access Methods: All getters (accessors) and setters (modifiers). Your setters should have appropriate error detection. All accessors should start with "get" and all modifiers should start with set and then followed by the name of attribute using camel case. For example, the accessors and modifiers for the model attribute should be:

getModel
setModel

Note: camel-case will require first letter of the attribute to capitalized.

void park() : A pure virtual method that will be implemented within the child classes. The pure virtual method is what makes the Vehicle class an abstract base class.

void output(): This method should output the attributes in a JSON inspired format. The output should go to cout with no endl at the end. The format is where you have {}'s with a comma delimited list of the attribute values. For example:

{green, VFR8237, Kia, 1882}

Is for a green Kia with the license plate VFR8237 and unique ID of 1882.

Car Class

Create a class named Car that inherits the Vehicle class with the following attributes and methods:

Attributes

noOfDoors: The number of doors on the vehicle

Methods

Base Access Methods: All geQers (accessors) and seQers (modifiers). Your seQers should have appropriate error detection. All accessors should start with "get" and all modifiers should start with set and then followed by the name of aQribute using camel case.

void park(): This method will be fully implemented later. But for now it should output "Parking Car: {red, ABC1234, Ford, 1766}" (example vehicle output)

Motorcycle Class

Create a class named Motorcycle that inherits the Vehicle class with the following aQributes and methods:

Attributes

seatHeight: The height of the cycle's seat from the ground (h)

Methods

Base Access Methods: All getters (accessors) and setters (modifiers). Your setters should have appropriate error detection. All accessors should start with "get" and all modifiers should start with set and then followed by the name of attribute using camel case.

void park(): This method will be fully implemented later. But for now it should output "Parking Motorcycle: {red, ABC1234, Ford, 1766}" (example vehicle output)

Van Class

Create a class named Van that inherits the Vehicle class with the following attributes and methods:

Attributes

noOfBoxes: The number of 2x2x2 h boxes can fit on the van

Methods

Base Access Methods: All getters (accessors) and setters (modifiers). Your setters should have appropriate error detection. All accessors should start with "get" and all modifiers should start with set and then followed by the name of attribute using camel case.

void park(): This method will be fully implemented later. But for now it should output "Parking Van: {red, ABC1234, Ford, 1766}" (example vehicle output)

Truck Class

Create a class named Truck that inherits the Vehicle class with the following attributes and methods:

Attributes

SizeOfContainer: The size of the container that can be carried by the Truck (cubic h)

Methods

Base Access Methods: All getters (accessors) and setters (modifiers). Your setters should have appropriate error detection. All accessors should start with "get" and all modifiers should start with set and then followed by the name of attribute using camel case.

void park(): This method will be fully implemented later. But for now it should output "Parking Truck: {red, ABC1234, Ford, 1766}" (example vehicle output)

Build a main() driver and test your classes as you have done before. Keep in mind that you need to tests each of the leaf classes. NOTE: You should not be able to create any objects of the top- level classes.

The Driver

In the driver, is mostly in-tact. But it's horribly commented. So instead of you writing the driver, you will be commenting all of the code. To successfully add comments, you will need to read the code and understand the code. And ask questions if necessary.

What files are part of the Driver?

  • constants.h
  • main.cpp
  • ParkingSpace.cpp
  • ParkingSpace.hpp

Commenting Code

You should add header comments for prototypes and function definitions.

You should add comments for variable and constant declarations (see constants.h)

You should also add comments to the bodies of the main() and other functions. Your comments should not be placed for every line of code, but instead use good commenting practice and comment the "blocks" of functionality.

createRandomAttributes Function

This function needs calls to random_shuffle to randomize the color, make, and license plate. Add them according to the comments. If you don't fix this code, then your simulation will always park the same vehicle.

Starter Codes

Car.cpp

//
// Car.cpp
// Final
//
//

#include "Car.hpp"
#include < iostream >

Car.hpp

//
// Car.hpp
// Final
//
//

#ifndef Car_hpp
#define Car_hpp
#include < iostream >
#include < string >
using namespace std;
#include < stdio.h >
#include "Vehicle.hpp"

class Car : public Vehicle{
protected:
int noOfDoors;

public:

Car(int noOfDoors) ;

void setNoOfDoors(int noOfDoors) {
this->noOfDoors = noOfDoors;
}
int GetNoOfDoors() {
return noOfDoors;
}
void park() {
cout << "Parking Car" << endl;
}

};
#endif /* Car_hpp */

constants.cpp

//
// constants.cpp
// Final
//

#include "constants.hpp"

constants.hpp

//
// constants.hpp
// Final
//

//

#ifndef constants_hpp
#define constants_hpp

#include < stdio.h >

const int NUM_SPACES = 20;
const int NUM_OF_MINUTES = 400;

#endif

main.cpp

//
// main.cpp
// Final
//
//

#include < list >
#include < vector >
#include < stack >
#include < string >
#include < queue >
#include < set >
#include < algorithm >
#include < iostream >
#include < ctime >
#include < iomanip >
#include < stdlib.h >
#include "constants.hpp"
#include "Car.hpp"
#include "Motorcycle.hpp"
#include "Van.hpp"
#include "Truck.hpp"
#include "ParkingSpace.hpp"
#include "Vehicle.hpp"

using namespace std;

void createRandomAttributes(Vehicle*);
void checkForCarsLeaving(unsigned, vector< ParkingSpace >&);
void parkVehicles(queue< Vehicle* >&, vector< ParkingSpace >&);
void showParkingLot(vector< ParkingSpace >&);

int main() {

int numOfCarsArrived;
queue< Vehicle* > arrivingVehicles;
vector< ParkingSpace > allParkingSpaces(NUM_SPACES);
Car* newCar;
Motorcycle* newMotorcycle;
Van* newVan;
Truck* newTruck;
unsigned vehicleID = 1;

srand((unsigned int)time(NULL));


for (unsigned time = 0; time < NUM_OF_MINUTES; time++) {
cout << endl << "SIM TIME :" << time << endl;
cout << "============================" << endl;

numOfCarsArrived = rand() % 10;

// cars line up for the parking lot
cout << endl << "Vehicles Arriving" << endl << endl;
for (int count = 1; count <= numOfCarsArrived; count++) {
switch (rand() % 4) {
case 0:
newCar = new Car;
createRandomAttributes(newCar);
newCar->setID(vehicleID);
vehicleID++;
cout << "\t++Car Arrives " << *newCar << endl;
arrivingVehicles.push(newCar);
break;
case 1:
newMotorcycle = new Motorcycle;
createRandomAttributes(newMotorcycle);
newMotorcycle->setID(vehicleID);
vehicleID++;
cout << "\t++Motorcycle Arrives " << *newMotorcycle << endl;
arrivingVehicles.push(newMotorcycle);
break;
case 2:
newVan = new Van;
createRandomAttributes(newVan);
newVan->setID(vehicleID);
vehicleID++;
cout << "\t++Van Arrives " << *newVan << endl;
arrivingVehicles.push(newVan);
break;
case 3:
newTruck = new Truck;
createRandomAttributes(newTruck);
newTruck->setID(vehicleID);
vehicleID++;
cout << "\t++Truck Arrives " << *newTruck << endl;
arrivingVehicles.push(newTruck);
break;
default:
cerr << "Error: Should never happpen, improper car type" << endl;
assert(0);
}
}

cout << "Vehicles in Line: " << arrivingVehicles.size() << endl;

checkForCarsLeaving(time, allParkingSpaces);

parkVehicles(arrivingVehicles, allParkingSpaces);

showParkingLot(allParkingSpaces);

}

return 0;
}


void createRandomAttributes(Vehicle* theVehicle) {

std::string colorValues[] = {"red", "blue", "grey", "silver", "white", "yellow", "green"};
vector< std::string > colorsSet(colorValues, colorValues + sizeof(colorValues)/sizeof(std::string));
std::string makeValues[] = {"Ford", "Honda", "Porsche", "Volvo", "Chevy", "Kia", "Hyundai"};
vector< std::string > makeSet(makeValues, makeValues + sizeof(makeValues)/sizeof(std::string));

// TODO: use random_shuffle to randomize the make of the vehicle
// TODO: use random_shuffle to randomize the color of the vehicle

theVehicle->setColor(*(colorsSet.begin()));
theVehicle->setMake(*(makeSet.begin()));

std::string thePlate;

std::string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string digits = "12345678";

// TODO: use random_shuffle to randomize the alphabets
// TODO: use random_shuffle to randomize the digits

thePlate += alphabets[0];
thePlate += alphabets[1];
thePlate += alphabets[2];
thePlate += digits[0];
thePlate += digits[1];
thePlate += digits[2];
thePlate += digits[3];

theVehicle->setLicensePlate(thePlate);

}

void checkForCarsLeaving(unsigned simTime, vector< ParkingSpace >& theLot) {
if (simTime > 5) {
cout << endl << "Vehicles Leaving" << endl << endl;
for (vector< ParkingSpace >::iterator space = theLot.begin();
space != theLot.end();
space++) {
unsigned carLeaves = rand() % 2;
if (carLeaves && !space->isEmpty()) {
cout << "Vehicle leaving: " << (*space).getVehicle() << endl;
space->setEmpty(true);
}
}
}
}


void parkVehicles(queue< Vehicle* >& newVehicles, vector< ParkingSpace >& theLot) {
cout << endl << "Vehicles Parking" << endl << endl;
bool spaceAvailable = false;
for (vector::iterator space = theLot.begin();
space != theLot.end();
space++) {
if (space->isEmpty()) {
spaceAvailable = true;
if (!newVehicles.empty()) {
newVehicles.front()->park();
space->parkCar(newVehicles.front());
newVehicles.pop();
}
}
}


if (!spaceAvailable) {
cout << endl << "LOT FULL: Vehicles Can't Park" << endl << endl;
while (!newVehicles.empty()) {
cout << "Turning away vehicle: " << newVehicles.front() << endl;
newVehicles.pop();
}
}
}


void showParkingLot(vector< ParkingSpace >& theLot) {
cout << endl << "PARKING LOT STATUS" << endl << endl;
for (unsigned spaceNumber = 0; spaceNumber < theLot.size(); spaceNumber++) {
cout << setw(2) << spaceNumber + 1 << ": " << (theLot[spaceNumber].isEmpty() ? "empty" : "occup") << ": ";
if (!theLot[spaceNumber].isEmpty()) {
theLot[spaceNumber].getVehicle()->output();
cout << endl;
} else {
cout << endl;
}
}
}

Motorcycle.cpp

//
// Motorcycle.cpp
// Final
//

//

#include "Motorcycle.hpp"

Motorcycle.hpp

//
// Motorcycle.hpp
// Final

//

#ifndef Motorcycle_hpp
#define Motorcycle_hpp


#include "Vehicle.hpp"
#include < iostream >
#include < string >
using namespace std;
#include < stdio.h >
class Motorcycle : public Vehicle{
protected:
int seatHeight;
public:
void setSeatHeight(int seatHeight) {
this->seatHeight = seatHeight;
}
int GetSeatHeight() {
return seatHeight;
}
void park() {
cout << "Parking Motorcycle" << endl;
}
};

#endif /* Motorcycle_hpp */

ParkingSpace.cpp

//
// ParkingSpace.cpp
// Final
//
//

#include "ParkingSpace.hpp"


void ParkingSpace::parkCar(Vehicle* vh) {
if (vh) {
carInSpace = vh;
empty = false;
}
}


bool ParkingSpace::isEmpty() {
return empty;
}

Vehicle* ParkingSpace::getVehicle() {
return carInSpace;
}

ParkingSpace.hpp

//
// ParkingSpace.hpp
// Final
//

//

#ifndef ParkingSpace_hpp
#define ParkingSpace_hpp

#include < stdio.h >
#include "Vehicle.hpp"


class ParkingSpace {
private:
Vehicle* carInSpace;
bool empty;
public:
ParkingSpace(): carInSpace(NULL), empty(true) {}

void parkCar(Vehicle*);
void setEmpty(bool e) {empty = e;}

bool isEmpty();
Vehicle* getVehicle();


};
#endif /* ParkingSpace_hpp */

Truck.cpp

//
// Truck.cpp
// Final

//

#include "Truck.hpp"

Truck.hpp

//
// Truck.hpp
// Final
//

//

#ifndef Truck_hpp
#define Truck_hpp

#include < stdio.h >
#include < iostream >
#include < string >
using namespace std;
#include "Vehicle.hpp"
#include < stdio.h >
class Truck : public Vehicle{
protected:
int sizeOfContainer;
public:
void setSizeOfContainer(int sizeOfContainer) {
this->sizeOfContainer = sizeOfContainer;
}
int GetSizeOfContainer() {
return sizeOfContainer;
}
void park() {
cout << "Parking Truck" << endl;
}
};

#endif /* Truck_hpp */

Van.cpp

//
// Van.cpp
// Final
//
//

#include "Van.hpp"

Van.hpp

//
// Van.hpp
// Final

//

#ifndef Van_hpp
#define Van_hpp
#include < stdio.h >
#include < iostream >
#include < string >
using namespace std;
#include "Vehicle.hpp"
#include < stdio.h >
class Van : public Vehicle{
protected:
int noOfBoxes;
public:
void setNoOfBoxes(int noOfBoxes) {
this->noOfBoxes = noOfBoxes;
}
int GetNoOfBoxes() {
return noOfBoxes;
}
void park() {
cout << "Parking Van" << endl;
}
};

#endif /* Van_hpp */

Vehicle.cpp

//
// Vehicle.cpp
// Final
//

//

#include "Vehicle.hpp"

Vehicle.hpp

//
// Vehicle.hpp
// Final
//
//

#ifndef Vehicle_hpp
#define Vehicle_hpp

#include < stdio.h >
#include < iostream >
#include < string >
using namespace std;
class Vehicle {
protected:
string make;
string color;
string licensePlate;
int theID;
public:
Vehicle(string make, string color, string licensePlate, int theID) {
this->make = make;
this->color = color;
this->licensePlate = licensePlate;
this->theID = theID;
}
int getTheID() {
return theID;
}
void setTheID(int theID) {
this->theID = theID;
}
string getMake() {
return make;
}
void setMake(string make) {
this->make = make;
}
string getColor() {
return color;
}
void setColor(string color) {
this->color = color;
}
string getLicensePlate() {
return licensePlate;
}
void setLicensePlate(string licensePlate) {
this->licensePlate = licensePlate;
}
virtual void park() = 0;
};

#endif /* Vehicle_hpp */
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.