Hi, today we will be building a Parking Lot System which has multiple features and supports different types of vehicles to be parked, and the most important feature will be its extendibility.
Before designing a software lets first write down its features.
Parking Lot should have floors on which vehicles are parked.
On parking floors there can be different parking space for different types of vehicles.
A vehicle can only be parked in its own type of parking space. For example, A bike cannot be parked in parking space of a car, since that will be waste of space.
Tickets should be generated after vehicle is parked.
Later these tickets can be used to unpark or retrieve the parked vehicle.
Also for convenience a vehicle should be parked at the closest parking space possible.
These are the basic features that we need to implement in our parking system, but we will need to design the software in such a way that new features can be introduced without any problem, like introducing a new vehicle type that is allowed by parking lot or a limitation on a certain type of vehicle category.
The approach I am choosing for this problem is factory design pattern (I will explain later in implementation section).
In our case vehicles and parking space are two different entity which are bound by vehicle's type.
First lets start by designing parking space, I will call it slot.
Here I have made different classes for different types of parking slots, this will help us if we would later want a certain parking slot should behave different than others. For example, a truck slot can have higher ticket cost than a car or bike slot and this can be defined separately in its own class without needing to change other classes.
Now to vehicles which will be parked in parking slot,
It is the same case with vehicle class too, if we would later want to add additional properties to a vehicle type than it would be lot easier to change its own class than to make a mess of if and else statements.
How do we make sure a vehicle is parked in its own category of parking slot?
We will use factory design pattern for that.
What is factory design pattern?
Well it is the highlight of this problem,
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. - Wikipedia
So here we will create object of vehicle and map to proper slot using dictionary.
VEHICLES_MAPmaps input string to its vehicle class and
SLOTS_MAPmaps vehicle class to its slot class.
DEFAULT_FLOOR_MAP is location of where each type of vehicle will be store on parking floor.
So with parking floor in discussion lets first see the floor class.
Floor covers a lot of functionalities for our parking lot. Lets start exploring each
park_vehicle()should park vehicle in the first empty slot of its type.
unpark_vehicle()should take a ticket and remove the vehicle and empty the slot.
floor_mapis dictionary of vehicle types mapped to its starting and end location of slots on floor.
busy_slot_mapis a dictionary which keeps track of all the busy parking slots of a vehicle type.
By now we can add or remove a vehicle type without needing to change anything in floor, hence we have decoupled vehicles and parking slot with floor.
At last we need to design parking lot which will be made up of floors.
park_vehicle()will park a vehicle in the first empty floor it will find.
unpark_vehicle()will remove vehicle from floor and close the ticket.
Interesting part is that
park_vehicle() of parking lot finds the first empty floor and
park_vehicle() finds the first empty slot to park.
ParkingLot is decoupled from the entire parking system it just manages floors and that is how it should be. No feature or entity should be least dependent on others.
Now about Tickets which I skipped.
Static variable of class is property of a class not of an instance. I use this property to manage all the Ticket instances.
ticketsis static variable of type dictionary which will store all the instances created.
generateTicket()generates a string based on parking lot, floor and slot the vehicle is parked in.
openTicket()adds ticket to
getTicket()is class method which returns the Ticket instance if the ticket string passed is in
closeTicket()removes ticket from
These are the basic features that a ticket manager needs to have,
being able to open, close and find a ticket.
Up to this point we have created a parking system which parks and unparks a vehicle at the closest point and generates a ticket.
Taking the input and managing Parking Lot does not have a lot of new things to discuss so I am skipping that part but here is the link to source which takes input from console.
Thank you for reading till the end, I am new to writing articles so let me know about the mistakes that I made.