Write a C program to manage a parking lot Build Specificatio

Write a C++ program to manage a parking lot.

Build Specifications:

1. The parking lot has multiple levels. Each level has multiple rows of spots.

2. The parking lot can park motorcycles, cars, and buses.

3. The parking lot has motorcycle spots, compact spots, and large spots.

4. A motorcycle can park in any spot.

5. A car can park in either a single compact spot or a single large spot.

6. A bus can park in five large spots that are consecutive and within the same row. It cannot park in small spots.

Function description:

1. Setup the parking lot. You should be able to create a customizable parking lot.

2. Edit the parking lot layout: You should be able to change the parking lot layout. For example: Add levels, change the spot layout at a level, etc.

3. Reserve: You need to be able to reserve a spot. The user should provide the level number, the row number, and then the spot number.

4. Search: See how many free spots on a specific level.

5. Get Free spots: How to get how many free spots of each kind in the whole parking lot.

6. View: View all levels and their information

The program must have the following properties: - You should do error handling (Ex: you cannot choose a level that does not exist) - You should use inheritance, polymorphism, and aggregation. Example: You can design a generic spot class, then design derived classes for different kinds of spots.

Also, design a menu (should still appear until the exit option is chosen) in the Main program that has the following options implemented to test your classes’ functionality:

1. Setup the parking lot

2. Edit

3. Look for capacity

4. View level information

5. Exit

I\'ve gotten this far in the code, but I\'m stuck. Any help would be appreciated.

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

using namespace std;
class Vehicle
{
public:
   int VehicleID; // ID for each vehicle
   char vehicleType;//char VehicleType; //M for motorcycle, B for Bus, C for Car
   int numberOfrowsOccupied;
   Vehicle(int id, char type, int numRowsOccupied)
   {
       VehicleID = id;
       vehicleType = type;
       numberOfrowsOccupied = numRowsOccupied;
   }
};

class Level
{
public:
   int levelNumber;
   int numberOfRows;
   vector<Vehicle> vehicles;
   Level(int lNumber, int numRows)
   {
       levelNumber = lNumber;
       numberOfRows = numRows;
   }

   void addVehicle(Vehicle veh)
   {
       vehicles.push_back(veh);
   }

   void removeVehicle(int id)
   {
       for(std::vector<Vehicle>::iterator it = vehicles.begin(); it != vehicles.end(); ++it) {
           Vehicle veh = *it;
           if(veh.VehicleID == id)
           {
               vehicles.erase(std::remove(vehicles.begin(), vehicles.end(), veh), vehicles.end());
               break;
           }
       }
   }
};

class ParkingLot
{
public:
   int numberOfLevels;
   vector<Level> levels;
   ParkingLot(int numLevels)
   {
       numberOfLevels = numLevels;
   }

   void addLevel(Level lev)
   {
       levels.push_back(lev);
   }
};

int main()
{
  
}

Solution

Answer: See the code for main function below. Also note the inline comments.

----------------------------------------------------------

//main function
int main() {
   ParkingLot *parkinglot;
   //loop to display menu and process user input
   int choice; //choice of user
   while(1)
   {
       fflush(stdout);
       //menu
       cout<<\"Available options:\"<<endl;
       cout<<\"1. Setup the parking lot\"<<endl;
       cout<<\"2. Edit\"<<endl;
       cout<<\"3. Look for capacity\"<<endl;
       cout<<\"4. View level information\"<<endl;
       cout<<\"5. Exit\"<<endl;
       cout<<\"Choice:\";
       cin>>choice;

       //input processing
       switch(choice)
       {
       case 1:
           cout<<\"Setting up Parking lot...\"<<endl;
           cout<<\"Enter no. of levels in parking lot:\";
           int num_levels;
           cin>>num_levels;
           parkinglot=new ParkingLot(num_levels);
           cout<<\"Parking lot set up!\"<<endl;
           break;
       case 2:
           cout<<\"Edit of parking lot:\"<<endl;
           cout<<\"Want to add a level (y/n)?\";
           char answer;
           cin>>answer;
           if(answer == \'y\')
           {
               int num_rows=100; //default no. of rows in a level
               int level_no=parkinglot->getNumberOfLevels()+1; //level no. of new level
               Level level(level_no,num_rows); //create new level
               parkinglot->addLevel(level);
               cout<<\"Level added.\"<<endl;
           }
           //you can add other edit functionality here also as required.
           break;
       case 3:
           cout<<\"Look for capacity:\"<<endl;
           cout<<\"Level-wise number of free spots are as below:\"<<endl;
           for(int i=1;i<=parkinglot->getNumberOfLevels();i++)
           {
               cout<<\"Level no.:\"<<i<<endl;
               cout<<\"Unoccupied spots:\"; //add functionality to display required info here.
               cout<<endl;
           }
           break;
       case 4:
           cout<<\"View level information:\"<<endl;
           cout<<\"Enter level no.:\";
           int level_no;
           cin>>level_no;
           cout<<\"Information about level \"<<level_no<<\":\"<<endl;
           cout<<\"Number of rows:\"<<parkinglot->getLevels().at(level_no).getNumberOfRows()<<endl;
           //display other information here.
           break;
       case 5:
           cout<<\"Thanks for using application. Exiting...\"<<endl;
           exit(1);
       default:
           cout<<\"Wrong choice! Re-enter.\"<<endl;
           break;
       }
   }
}

--------------------------------------

Note: Updated code for classes defined by you is also given below.

---------------------------------------

//Vehicle class
class Vehicle {
public:
   int VehicleID; // ID for each vehicle
   char vehicleType; //char VehicleType; //M for motorcycle, B for Bus, C for Car
   int numberOfrowsOccupied;
   Vehicle(int id, char type, int numRowsOccupied) {
       VehicleID = id;
       vehicleType = type;
       numberOfrowsOccupied = numRowsOccupied;
   }

   int getNumberOfrowsOccupied() const {
       return numberOfrowsOccupied;
   }

   void setNumberOfrowsOccupied(int numberOfrowsOccupied) {
       this->numberOfrowsOccupied = numberOfrowsOccupied;
   }

   int getVehicleId() const {
       return VehicleID;
   }

   void setVehicleId(int vehicleId) {
       VehicleID = vehicleId;
   }

   char getVehicleType() const {
       return vehicleType;
   }

   void setVehicleType(char vehicleType) {
       this->vehicleType = vehicleType;
   }
};

//Level class
class Level {
public:
   int levelNumber;
   int numberOfRows;
   vector<Vehicle> vehicles;
   Level(int lNumber, int numRows) {
       levelNumber = lNumber;
       numberOfRows = numRows;
   }

   void addVehicle(Vehicle veh) {
       vehicles.push_back(veh);
   }

   void removeVehicle(int id) {
       for (std::vector<Vehicle>::iterator it = vehicles.begin();
               it != vehicles.end(); ++it) {
           Vehicle veh = *it;
           if (veh.VehicleID == id) {
               /*vehicles.erase(
                       std::remove(vehicles.begin(), vehicles.end(), veh),
                       vehicles.end());*/
               vehicles.erase(it);
               break;
           }
       }
   }

   int getLevelNumber() const {
       return levelNumber;
   }

   void setLevelNumber(int levelNumber) {
       this->levelNumber = levelNumber;
   }

   int getNumberOfRows() const {
       return numberOfRows;
   }

   void setNumberOfRows(int numberOfRows) {
       this->numberOfRows = numberOfRows;
   }
};

class ParkingLot {
public:
   int numberOfLevels;
   vector<Level> levels;
   ParkingLot(int numLevels) {
       numberOfLevels = numLevels;
       for(int i=0;i<numLevels;i++)
       {
           Level level(i+1,100); //creates a level of 100 rows as default.
           levels.push_back(level);
       }
   }

   //adds a new level
   void addLevel(Level lev) {
       levels.push_back(lev);
       numberOfLevels++;
   }

   int getNumberOfLevels() const {
       return numberOfLevels;
   }

   void setNumberOfLevels(int numberOfLevels) {
       this->numberOfLevels = numberOfLevels;
   }

   const vector<Level>& getLevels() const {
       return levels;
   }
};

------------------------------------------

Output of main function:

Available options:
1. Setup the parking lot
2. Edit
3. Look for capacity
4. View level information
5. Exit
Choice:1
Setting up Parking lot...
Enter no. of levels in parking lot:3
Parking lot set up!
Available options:
1. Setup the parking lot
2. Edit
3. Look for capacity
4. View level information
5. Exit
Choice:2
Edit of parking lot:
Want to add a level (y/n)?n
Available options:
1. Setup the parking lot
2. Edit
3. Look for capacity
4. View level information
5. Exit
Choice:3
Look for capacity:
Level-wise number of free spots are as below:
Level no.:1
Unoccupied spots:
Level no.:2
Unoccupied spots:
Level no.:3
Unoccupied spots:
Available options:
1. Setup the parking lot
2. Edit
3. Look for capacity
4. View level information
5. Exit
Choice:4
View level information:
Enter level no.:2
Information about level 2:
Number of rows:100
Available options:
1. Setup the parking lot
2. Edit
3. Look for capacity
4. View level information
5. Exit
Choice:5
Thanks for using application. Exiting...

Write a C++ program to manage a parking lot. Build Specifications: 1. The parking lot has multiple levels. Each level has multiple rows of spots. 2. The parking
Write a C++ program to manage a parking lot. Build Specifications: 1. The parking lot has multiple levels. Each level has multiple rows of spots. 2. The parking
Write a C++ program to manage a parking lot. Build Specifications: 1. The parking lot has multiple levels. Each level has multiple rows of spots. 2. The parking
Write a C++ program to manage a parking lot. Build Specifications: 1. The parking lot has multiple levels. Each level has multiple rows of spots. 2. The parking
Write a C++ program to manage a parking lot. Build Specifications: 1. The parking lot has multiple levels. Each level has multiple rows of spots. 2. The parking
Write a C++ program to manage a parking lot. Build Specifications: 1. The parking lot has multiple levels. Each level has multiple rows of spots. 2. The parking

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site