Design a parking lot using object oriented design

“Design a parking lot” is a common interview question for senior software developers.

The problem statement is very similar to Grokking object oriented design interview question. https://www.educative.io/courses/grokking-the-object-oriented-design-interview/gxM3gRxmr8Z.

Requirements are

  1. The parking lot is a 10 floor building with multiple entry and exit points, both electric and non-electric cars can park. Each floor has 100 parking spots, out of which 4 are reserved for handicapped people. Only top 5 floors can be used for electric cars. The 10th floor is complete electric, other 4 floors, each have 50 spots reserved for electric cars. A non electric car can park in an electric car spot.
  2. The customer get the ticket at the entry point, the ticket will have a printed timestamp(usually in the format dd-mm-yyyy hh:mm) and a parking spot. The customer a device which emits the vechicle number and type of vechicle(electric/non-electric).
  3. At the exit, the customer has to pay the fee. The fee includes parking cost plus charging cost for electric cars. The cost of parking is $2 for first 2 hours, $1.5 for every extra 1 hour till 6 hours and $1 if he parks for more than 6 hours. The charging fee of electric vechicles is $E(varies between car to car).
  4. The parking system can reject parking request if it’s full. Ocasionnaly some spots can’t be used due to broken car/maintenance or reserved by parking manager’s girlfriend(s).

The candidate has to design a fully automated system for 2 kinds of users

  1. admin : Admins can mark a spot as usable/non-usuable, open/close the entrance, decide the parking and charging fee.
  2. customer: Customers can get the parking ticket, park, pay and leave.

The parking system also has a small display system at the exit, which shows how much money the customer has to pay, parking information(parking spot, duration etc).

Solution

First we need to design the system, design the use case diagram, class diagram and sequence diagram.

Use case diagram

The parking system has 3 actors

  1. Admin
  2. Customer
  3. System

Here are the use cases

  1. Admin can add/remove parking spot(s)
  2. Admin can add/remove an entire floor
  3. Admin can add/remove entrances/exits
  4. Admin can modify the parking/charging fee
  5. Admin can view a status of entire building/floor/spot.
  6. Admin can check if a car with a specific registration is parked in the parking lot.
  7. Customer can get the ticket
  8. Customer can scan the ticket
  9. Customer can pay the bill
  10. System can open the entry gate
  11. System can the exit gate
  12. Allocate/deallocate a spot to a customer

Class Diagram

The implementation is very straight forword.

Algorithim to find an empty spot

for each floor in the building
begin
for each spot in the floor
begin
if spot.type == car.type && spot.status == free
begin
update entry time of the car
spot.park(car)
endif
endfor
endfor
if car.startTime == 0 && car.type == "normal"
begin
// a non electric car can park in electric car's spot. It will
be choosen when there is no empty spot for non electric cars
for each floor in the building
begin
for each spot in the floor
begin
if spot.type == "electric" && spot.status == free
begin
update entry time of the car
spot.park(car)
endif
endfor
endfor
endif

Algorithm to calculate the cost of parking

duration = ceil(car.exitTime - car.entryTime)
cost = 0
if duration is between 0 to 2
begin:
cost = duration * 2
elseif duration is between 3 to 6
begin:
cost = 2 * 2 + (duration - 2) * 1.5
else:
cost = 2 * 2 + 4 * 1.5 + (duration - 6)* 1
endif
if car.type == "electric"
begin
cost += cost_of_charging
endif

Thanks for reading !!

C++11/14, Qt, Juce

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store