Parking Lot System: Code
Tags: webdev, beginners, designsystem, tutorial
Published: 2023-05-07 09:25:07

🔥 We'll start from building a Spring Boot Application. Because of some inbuilt annotations. For example: If you want to use Getter setters in the following class:-

```JAVA
package src.entities;

import src.enums.VehicleType;

public class Ticket {
    int floorNo;
    int price;
    String name;
    long time;
    VehicleType vehicleType;
    ParkingSpot parkingSpot;
}
```

Then you have to write code for a constructor and getters and setters of floorNo, price, name etc.

![Spring Boot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0msr1fb529o8veddkng1.png)

> Yes, you don't need to learn Spring Boot, Just go through this [blog](https://dev.to/lovepreetsingh/complete-spring-boot-build-enterprise-level-apps-2h50) to know about its basic things.

Note:- As in our previous [blog](https://dev.to/lovepreetsingh/parking-lot-system-low-level-design-30gi) of Parking Lot System Design, we discussed about its various components and design in this series of LLD (Low level design)

### 😎 **Today We'll go through the code of Parking lot system**

📍 We will implement few of the components like ParkingSpot, Vehicle, VehicleType, ParkingStraegy, Ticket etc.

You can refer this [code](https://github.com/AlphaDecodeX/ParkingLotSystemCode/tree/master) and further can build Entrance, CostComputation etc

## 🎉 Let's go through the code

### 📍 Our Entities are:-
- ParkingSpot
```JAVA
package com.parkinglotsystem.main.entities;

import com.parkinglotsystem.main.parking.ParkingType;

public interface ParkingSpot {
    public boolean isEmpty();
    public void occupy();
    public void vacateParkingSpot();
    public ParkingType getType();

}
```
- Ticket
```JAVA
package com.parkinglotsystem.main.entities;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class Ticket {
    int floorNo;
    int price;
    String name;
    long time;
    Vehicle vehicle;
    ParkingSpot parkingSpot;
}
```
- Vehicle
```JAVA
package com.parkinglotsystem.main.entities;

import com.parkinglotsystem.main.enums.VehicleType;

public class Vehicle {
    VehicleType vehicleType;
    String color;
    String vehicleNumber;
}
```

### 📍 Our enum is:-
- VehicleType
```JAVA
package com.parkinglotsystem.main.enums;

public enum VehicleType {
    TWO_WHEELER("two_wheeler"),
    THREE_WHEELER("three_wheeler"),
    FOUR_WHEELER("four_wheeler");
    
    private final String value;
    
    private VehicleType(String value) {
        this.value = value;
    }
    
    public String getValue() {
        return value;
    }
}
```
### 📍 Our Parking Components are:-
- ParkingSpotStrategy
```JAVA
package com.parkinglotsystem.main.parking;

import com.parkinglotsystem.main.entities.ParkingSpot;

import java.util.List;
import java.util.Optional;

public interface ParkingSpotStrategy {
    Optional<ParkingSpot> findParkingSpot(List<ParkingSpot> parkings);
}
```
- DefaultParkingSpotStrategy
```JAVA
package com.parkinglotsystem.main.parking;

import com.parkinglotsystem.main.entities.ParkingSpot;

import java.util.List;
import java.util.Optional;

public class DefaultParkingStrategy implements  ParkingSpotStrategy{

    @Override
    public Optional<ParkingSpot> findParkingSpot(List<ParkingSpot> parkings){
        return parkings.stream()
                .filter(parkingSpot -> parkingSpot.getType().equals(ParkingType.TWO_WHEELER) && parkingSpot.isEmpty())
                .findFirst();
    }
}
```
- ParkingSpotManager
```JAVA
package com.parkinglotsystem.main.parking;

import com.parkinglotsystem.main.entities.ParkingSpot;
import com.parkinglotsystem.main.entities.Ticket;
import com.parkinglotsystem.main.entities.Vehicle;
import lombok.AllArgsConstructor;

import java.util.List;
import java.util.Optional;

@AllArgsConstructor
public class ParkingSpotManager {
    private List<ParkingSpot> parkings;
    public Optional<ParkingSpot> findParkingSpot(ParkingSpotStrategy ps){
        return ps.findParkingSpot(parkings);
    }

    public boolean parkVehicle(Vehicle v, Ticket t, ParkingSpotStrategy ps){
        Optional<ParkingSpot> parkingSpot = findParkingSpot(ps);
        if(parkingSpot.isPresent()){
            parkingSpot.get().occupy();
            return true;
        }
        return false;
    }
}
```

- ParkingType
```JAVA
package com.parkinglotsystem.main.parking;

public enum ParkingType {
    TWO_WHEELER("twoWheeler"),
    THREE_WHEELER("threeWheeler"),
    FOUR_WHEELER("fourWheeler");

    private final String value;

    ParkingType(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}
```
- TwoWheelerParkingSpot
```JAVA
package com.parkinglotsystem.main.parking;

import com.parkinglotsystem.main.entities.ParkingSpot;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class TwoWheelerParkingSpot implements ParkingSpot {
    private boolean isOccupied;
    private int noOfFloor;
    private int price;

    @Override
    public boolean isEmpty(){
        return !isOccupied;
    }

    @Override
    public void occupy(){
        this.isOccupied = true;
    }

    @Override
    public void vacateParkingSpot(){
       this.isOccupied = false;
    }

    @Override
    public ParkingType getType(){
        return ParkingType.TWO_WHEELER;
    }
}
```

> Note that we used different annotations like 
```TEXT
@Getter @Setter @AllArgCsConstructor
``` 

These annotations are provided by lombok that is included in build.gradle file so that it can be imported.

----

🚀 That was it for today, You will get an understanding of how to code from LLD and How easy it is. Further you can complete this [code](https://github.com/AlphaDecodeX/ParkingLotSystemCode/tree/master) and make a PR to contribute to this project.

Thanks and Follow for more