Parking Lot (Low Level Design)
Tags: java, softwareengineering, lowleveldesign
Published: 2023-04-08 13:36:22

## Problem definition

A parking lot is a designated area for parking vehicles and is a feature found in almost all popular venues such as shopping malls, sports stadiums, offices, etc. 

In a parking lot, there are a different parking spots available for different types of vehicles. Each of these spots is charged according to the time the vehicle has been parked in the parking lot. The parking time is tracked with a parking ticket issued to the vehicle at the entrance of the parking lot. Once the vehicle is ready to exit, it can pay at the exit gate using a card, card or UPI payment method.

## Requirements

1. Parking Lot should have multiple floors
2. Parking Lot should have multiple entry and exit gates
3. It should support parking of multiple vehicles- two, four, heavy
4. Ticket must be generated at entry gate
5. Payment should be done at exit gate through different modes- cash, card, upi
6. Payment can be done on hourly or minute basis.
7. Parking Space must be allocated close to the entrance gate of vehicle

## Objects Required

1. Vehicle
2. Parking Lot
3. Floor
4. Parking Space- two-wheeler, four-wheeler, heavy-vehicle 
5. Entry Gate
6. Exit Gate
7. GateManager
8. Ticket
9. Payment
10. Payment Strategy

[UML CLass Diagram](https://lucid.app/documents/embedded/42f7ff98-c046-4d4f-9318-820e77b321af)


```
class Vehicle
{
	int regNum;
	VehicleType vType;
    

    //define getter and setter in each class
}
public enum VehicleType
{
    TWO, FOUR, HEAVY;
}
class ParkingSpace
{
	int spaceId;
	int floorId;
	Vehicle vehicle;
	int price;
	boolean isEmpty;
	
	public void parkVehicle(Vehicle v)
	{
		isEmpty= false;
		vehicle= v;	
	}
	public void removeVehicle()
	{
		vehicle= null;
		isEmpty= True;
	}
}
class TwoWheelerSpace extends ParkingSpace
{
	int price= 10;
}
class FourWheelerSpace extends ParkingSpace
{
	int price= 20;
}
class HeavyVehicleSpace extends ParkingSpace
{
	int price= 40;
}
class Floor
{
	int floorId;
    boolean isEmpty;
	List<ParkingSpace> pSpaces;
	
	public void addParkingSpace(ParkingSpace p){}
	public void removeParkingSpace(ParkingSpace p){}
}

class PakingSpaceFactory
{
    ParkingSpace pSpace;
	public ParkingSpace getParkingSpace(Vehicle v)
	{
		//return object of TwoWheelerSpace or FourWheeler based on VehicleType
	}
}
class ParkingLot
{
	List<Floor> floorList; //initialized in constructor
	ParkingSpaceFactory pSpaceFact;
	
	
	public void addFloor(Floor f, List<ParkingSpace> p){}
	public void removeFloor(Floor f){}
	public ParkingSpace findParkingSpace(EntryGate entry, Vehicle v)
	{
		//Parking space found on the basis of type of vehicle using ParkingSpace Factory class
		//Parking space is found close to entry gate
	}
}
class GateManager
{
	List<EntryGate> entries;
	List<ExitGate> exits;
	
	public void addEntryGate(EntryGate entry){}
	public void removeEntryGate(EntryGate entry){}
	public void addExitGate(ExitGate exit){}
	public void removeExitGate(ExitGate exit){}
	
}
class EntryGate 
{
	int gateid;
	ParkingLot pLot;
	
    public Vehicle getVehicle(Vehicle v){}
	public ParkingSpace findSpace(){
        //uses object of parkingLot to find Space
    } 
	public void updateParkingSpace(ParkingSpace pSpace){}
	public Ticket generateTicket(){}
}
class Ticket
{
	int ticketId;
    Date today;
	Time entryTime;
    Time exitTime;
	Vehicle v;
	ParkingSpace pSpace;

    public void getExitTime(Time time){}
	
}
class PaymentInfo
{
	int transactionId;
	double amount;
    Date date;
	Ticket ticket;
	PaymentType pType;
	PaymentStrategy pStrategy;
	
}
public enum PaymentType
{
    CASH, CARD, UPI;
}
class ExitGate
{
	int gateId;
	Ticket t;
	
    
	public double calculatePrice(PaymentStrategy pStrategy)
	{
		return pStrategy* calculateCost();
	}
	public PaymentInfo generatePaymentInfo(PaymentType pType){}
	public void freeParkingSpace(){}
}
class PaymentStrategy
{
	public double calculateCost(){}
	
}
class HourlyPaymentStrategy extends PaymentStrategy
{
	public double calculateCost(Ticket t)
	{
		double parkedTime=(Currenttime- t.entryTime); // convert to hours
		return parkedTime * t.pSpace.price; 
    }
}
class MinutesPaymentStrategy extends PaymentStrategy
{
	public double calculateCost(Ticket t)
	{
		double parkedTime=(Currenttime- t.entryTime); // convert to minutes
		return parkedTime * t.pSpace.price; 
    }
}
```


[See on Github](https://github.com/swatijha-2906/LowLevelDesigns)