π₯ 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;
}
Then you have to write code for a constructor and getters and setters of floorNo, price, name etc.
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;
}
- 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 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();
}
}
- 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;
}
}
- 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 and make a PR to contribute to this project.
Thanks and Follow for more
Top comments (2)
Can you share the github repo link.
It is there in the blog bro.