Parking Lot design
Sun Sep 08 2024 12:47:24 GMT+0000 (Coordinated Universal Time)
Saved by @codestored #cpp
// Problem Statement // I own a parking lot that can hold up to 'n' cars at any given point in time. Each slot is given a number starting at 1 increasing with increasing distance from the entry point in steps of one. I want to create an automated ticketing system that allows my customers to use my parking lot without human intervention. // When a car enters my parking lot, I want to have a ticket issued to the driver. The ticket issuing process includes us documenting the registration number (number plate) and the colour of the car and allocating an available parking slot to the car before actually handing over a ticket to the driver (we assume that our customers are nice enough to always park in the slots allocated to them). The customer should be allocated a parking slot which is nearest to the entry. At the exit the customer returns the ticket which then marks the slot they were using as being available. // Due to government regulation, the system should provide me with the ability to find out: // Registration numbers of all cars of a particular colour. // Slot number in which a car with a given registration number is parked. // Slot numbers of all slots where a car of a particular colour is parked. #include <bits/stdc++.h> #define ll long long int #define pb push_back #define FOR(i, n) for (long long int i = 0; i < n; i++) using namespace std; class Vehicle { public: int colour, reg_number; Vehicle(int colour, int reg_number) { this->colour = colour; this->reg_number = reg_number; } }; class Ticket { public: int colour, reg_number, slot_number; Ticket(int colour, int reg_number, int slot_number) { this->colour = colour; this->reg_number = reg_number; this->slot_number = slot_number; } }; class ParkingLot { set<int> available_slots; map<int, set<int>> reg_by_color; map<int, int> slot_by_reg; public: ParkingLot(int n) { for (int i = 1; i <= n; i++) { available_slots.insert(i); } } void get_available_slots() { if(available_slots.size() == 0) { cout<<"No available slots for parking."<<endl; return; } cout << "Available slots are : "; for (auto itr = available_slots.begin(); itr != available_slots.end(); itr++) { cout << (*itr) << " "; } cout << endl; } Ticket take_slot(Vehicle vehicle) { if (available_slots.size() == 0) { cout << "No available slots" << endl; return Ticket(-1, -1, -1); } else { int taken_slot = (*available_slots.begin()); reg_by_color[vehicle.colour].insert(vehicle.reg_number); slot_by_reg[vehicle.reg_number] = taken_slot; available_slots.erase(taken_slot); cout << "Slot : " << taken_slot << " is occupied." << endl; return Ticket(vehicle.colour, vehicle.reg_number, taken_slot); } } void get_reg_by_colour(int colour) { cout << "Reg number of Cars with " << colour << " are : "; for (auto itr = reg_by_color[colour].begin(); itr != reg_by_color[colour].end(); itr++) { cout << (*itr) << " "; } cout << endl; } void getSlot_by_reg(int reg_number) { if (slot_by_reg.find(reg_number) == slot_by_reg.end()) { cout << "No Cars with " << reg_number << " exists in parking." << endl; } } void get_slot_by_colour(int colour) { cout << "Slot number of Cars with " << colour << " are : "; for (auto itr = reg_by_color[colour].begin(); itr != reg_by_color[colour].end(); itr++) { cout << slot_by_reg[(*itr)] << " "; } cout << endl; } void leave_slot(Ticket ticket) { available_slots.insert(ticket.slot_number); reg_by_color[ticket.colour].erase(ticket.reg_number); slot_by_reg.erase(ticket.reg_number); cout << "Car with Reg. number " << ticket.reg_number << " left." << endl; } }; int main() { ParkingLot parking_lot(3); parking_lot.get_available_slots(); Vehicle vehicle1(1, 123); Ticket ticket1 = parking_lot.take_slot(vehicle1); Vehicle vehicle2(1, 223); Ticket ticket2 = parking_lot.take_slot(vehicle2); parking_lot.get_available_slots(); parking_lot.get_reg_by_colour(1); Vehicle vehicle3(2, 125); Vehicle vehicle4(2, 135); Ticket ticket3 = parking_lot.take_slot(vehicle3); Ticket ticket4 = parking_lot.take_slot(vehicle4); parking_lot.leave_slot(ticket1); ticket4 = parking_lot.take_slot(vehicle3); parking_lot.get_available_slots(); return 0; }
Comments