This content originally appeared on DEV Community and was authored by Lovepreet Singh
🔥 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
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
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
package com.parkinglotsystem.main.entities;
import com.parkinglotsystem.main.enums.VehicleType;
public class Vehicle {
VehicleType vehicleType;
String color;
String vehicleNumber;
}
📍 Our enum is:-
- VehicleType
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
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
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
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
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
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
@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
This content originally appeared on DEV Community and was authored by Lovepreet Singh
Lovepreet Singh | Sciencx (2023-05-07T09:25:07+00:00) Parking Lot System: Code. Retrieved from https://www.scien.cx/2023/05/07/parking-lot-system-code/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.