DEV Community

Cover image for Parking Lot System: Code
server side digest
server side digest

Posted on

Parking Lot System: Code

πŸ”₯ 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:-



package src.entities;

import src.enums.VehicleType;

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


Enter fullscreen mode Exit fullscreen mode

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

Spring Boot

Yes, you don't need to learn Spring Boot, Just go through this blog to know about its basic things.

Note:- As in our previous blog 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 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;
}


Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}


Enter fullscreen mode Exit fullscreen mode

πŸ“ 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 findParkingSpot(List 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();
    }
}


Enter fullscreen mode Exit fullscreen mode
  • 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 parkings;
public Optional 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;
}
Enter fullscreen mode Exit fullscreen mode

}


- 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;
    }
}


Enter fullscreen mode Exit fullscreen mode
  • 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;
}
Enter fullscreen mode Exit fullscreen mode

}


> Note that we used different annotations like 
```TEXT


@Getter @Setter @AllArgCsConstructor


Enter fullscreen mode Exit fullscreen mode

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 and make a PR to contribute to this project.

Thanks and Follow for more

Top comments (2)

Collapse
 
vaibhavkhandare profile image
Vaibhav Dilip Khandare

Can you share the github repo link.

Collapse
 
ssd profile image
server side digest

It is there in the blog bro.