0% found this document useful (0 votes)
8 views4 pages

Garage

The document contains a C++ implementation of a parking garage system that manages car arrivals and departures based on their sizes. It includes a class 'Garage' with methods to arrive, depart, and dispense tickets while maintaining free and occupied parking spots. The program initializes a garage with a specified number of parking spots and demonstrates the arrival and departure of cars.

Uploaded by

Nupur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Garage

The document contains a C++ implementation of a parking garage system that manages car arrivals and departures based on their sizes. It includes a class 'Garage' with methods to arrive, depart, and dispense tickets while maintaining free and occupied parking spots. The program initializes a garage with a specified number of parking spots and demonstrates the arrival and departure of cars.

Uploaded by

Nupur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

// Online C++ compiler to run C++ program online

#include <iostream>

#include <filesystem>

#include <string>

#include <map>

#include <vector>

using namespace std;

#define CAR_SIZE 3

#define TOTAL_TICKETS 20000

class Garage

inline static int counter = 0; - Initialize static variable using inline


within the class

map<int, int> occupiedSpots[CAR_SIZE];

vector<int> freeSpots[CAR_SIZE];

//static int generateTicketNumber(void)

static int generateTicketNumber()

counter = (counter+1)%TOTAL_TICKETS;

return counter;

public:

Garage(vector<int> &totalSpots);

bool arrive(int carSize);

void depart(int carSize, int ticketNumber);


int dispenseTicket(int spot);

};

Garage::Garage(vector<int> &totalSpots)

if(totalSpots.size() > CAR_SIZE)

throw logic_error("Incorrect number of parking spot sizes");

for(int i = 0; i < totalSpots.size() && i < CAR_SIZE; i++)

vector<int> spots;

for(int j = 0; j < totalSpots[i]; j++)

spots.push_back(j);

freeSpots[i] = spots;

bool Garage::arrive(int carSize)

if(carSize > CAR_SIZE)

throw logic_error("No free spot in this size");

return false;

if(freeSpots[carSize].size() <= 0)

throw logic_error("No free spots");


return false;

int freeSpot = freeSpots[carSize][0];

int ticketNumber = dispenseTicket(freeSpot);

occupiedSpots[carSize][ticketNumber] = freeSpot;

return true;

void Garage::depart(int carSize, int ticketNumber)

int freeSpot = occupiedSpots[carSize][ticketNumber];

freeSpots[carSize].push_back(freeSpot);

occupiedSpots[carSize].erase(ticketNumber);

int Garage::dispenseTicket(int freeSpot)

int ticketNumber = generateTicketNumber();

return ticketNumber;

int main() {

// Write C++ code here

std::cout << "Try programiz.pro";

vector<int> spots;

//Garage::initializeTicketCounter();

for(int i = 0; i < 3; i++)

{
spots.push_back(20);

Garage garage(spots);

garage.arrive(1);

garage.depart(2, 100);

return 0;

You might also like