Parking Low with level

mail@pastecode.io avatar
unknown
java
23 days ago
7.3 kB
2
Indexable
Never
 import java.util.HashMap;
import java.util.Map;

// Enum for Vehicle Types
public enum VehicleType {
    CAR,
    BIKE,
    ELECTRIC,
    TRUCK
}

// Interfaces

interface ParkingSpot {
    boolean isAvailable();
    void occupy();
    void vacate();
    String getSpotNumber();
    VehicleType getSpotType();
}

interface Vehicle {
    String getLicensePlate();
    VehicleType getVehicleType();
}

interface ParkingLot {
    boolean parkVehicle(Vehicle vehicle);
    Vehicle retrieveVehicle(String licensePlate);
    int getAvailableSpots();
    void addLevel(Level level);
    Level getLevel(String levelName);
}

interface Level {
    boolean parkVehicle(Vehicle vehicle);
    Vehicle retrieveVehicle(String licensePlate);
    int getAvailableSpots();
    void addParkingSpot(ParkingSpot spot);
    ParkingSpot getSpot(String spotNumber);
    String getLevelName();
}

// Concrete Implementations

class ParkingSpotImpl implements ParkingSpot {
    private String spotNumber;
    private boolean available = true;
    private VehicleType spotType;

    public ParkingSpotImpl(String spotNumber, VehicleType spotType) {
        this.spotNumber = spotNumber;
        this.spotType = spotType;
    }

    @Override
    public boolean isAvailable() {
        return available;
    }

    @Override
    public void occupy() {
        if (!available) {
            throw new IllegalStateException("Spot is already occupied.");
        }
        available = false;
    }

    @Override
    public void vacate() {
        if (available) {
            throw new IllegalStateException("Spot is already vacant.");
        }
        available = true;
    }

    @Override
    public String getSpotNumber() {
        return spotNumber;
    }

    @Override
    public VehicleType getSpotType() {
        return spotType;
    }
}

class VehicleImpl implements Vehicle {
    private String licensePlate;
    private VehicleType vehicleType;

    public VehicleImpl(String licensePlate, VehicleType vehicleType) {
        this.licensePlate = licensePlate;
        this.vehicleType = vehicleType;
    }

    @Override
    public String getLicensePlate() {
        return licensePlate;
    }

    @Override
    public VehicleType getVehicleType() {
        return vehicleType;
    }
}

class ParkingTicketImpl implements ParkingTicket {
    private Vehicle vehicle;
    private ParkingSpot spot;

    public ParkingTicketImpl(Vehicle vehicle, ParkingSpot spot) {
        this.vehicle = vehicle;
        this.spot = spot;
    }

    @Override
    public Vehicle getVehicle() {
        return vehicle;
    }

    @Override
    public ParkingSpot getSpot() {
        return spot;
    }
}

class LevelImpl implements Level {
    private String levelName;
    private Map<String, ParkingSpot> spots = new HashMap<>();
    private Map<String, Vehicle> parkedVehicles = new HashMap<>();

    public LevelImpl(String levelName) {
        this.levelName = levelName;
    }

    @Override
    public boolean parkVehicle(Vehicle vehicle) {
        for (ParkingSpot spot : spots.values()) {
            if (spot.isAvailable() && spot.getSpotType() == vehicle.getVehicleType()) {
                spot.occupy();
                parkedVehicles.put(vehicle.getLicensePlate(), vehicle);
                System.out.println("Vehicle with license plate " + vehicle.getLicensePlate() + " parked at " + spot.getSpotNumber() + " on " + levelName);
                return true;
            }
        }
        System.out.println("No available spot for vehicle with license plate " + vehicle.getLicensePlate() + " on " + levelName);
        return false;
    }

    @Override
    public Vehicle retrieveVehicle(String licensePlate) {
        Vehicle vehicle = parkedVehicles.remove(licensePlate);
        if (vehicle != null) {
            for (ParkingSpot spot : spots.values()) {
                if (spot.getSpotNumber().equals(licensePlate)) {
                    spot.vacate();
                    System.out.println("Vehicle with license plate " + licensePlate + " retrieved from " + spot.getSpotNumber() + " on " + levelName);
                    return vehicle;
                }
            }
        }
        System.out.println("Vehicle with license plate " + licensePlate + " not found on " + levelName);
        return null;
    }

    @Override
    public int getAvailableSpots() {
        int availableSpots = 0;
        for (ParkingSpot spot : spots.values()) {
            if (spot.isAvailable()) {
                availableSpots++;
            }
        }
        return availableSpots;
    }

    @Override
    public void addParkingSpot(ParkingSpot spot) {
        spots.put(spot.getSpotNumber(), spot);
    }

    @Override
    public ParkingSpot getSpot(String spotNumber) {
        return spots.get(spotNumber);
    }

    @Override
    public String getLevelName() {
        return levelName;
    }
}

class ParkingLotImpl implements ParkingLot {
    private Map<String, Level> levels = new HashMap<>();

    @Override
    public boolean parkVehicle(Vehicle vehicle) {
        for (Level level : levels.values()) {
            if (level.parkVehicle(vehicle)) {
                return true;
            }
        }
        System.out.println("No available spot for vehicle with license plate " + vehicle.getLicensePlate());
        return false;
    }

    @Override
    public Vehicle retrieveVehicle(String licensePlate) {
        for (Level level : levels.values()) {
            Vehicle vehicle = level.retrieveVehicle(licensePlate);
            if (vehicle != null) {
                return vehicle;
            }
        }
        System.out.println("Vehicle with license plate " + licensePlate + " not found.");
        return null;
    }

    @Override
    public int getAvailableSpots() {
        int availableSpots = 0;
        for (Level level : levels.values()) {
            availableSpots += level.getAvailableSpots();
        }
        return availableSpots;
    }

    @Override
    public void addLevel(Level level) {
        levels.put(level.getLevelName(), level);
    }

    @Override
    public Level getLevel(String levelName) {
        return levels.get(levelName);
    }
}

// Main Application

public class ParkingLotSystem {
    public static void main(String[] args) {
        // Create parking lot
        ParkingLot parkingLot = new ParkingLotImpl();

        // Create levels
        Level level1 = new LevelImpl("Level 1");
        Level level2 = new LevelImpl("Level 2");

        // Add parking spots to levels
        for (int i = 1; i <= 5; i++) {
            level1.addParkingSpot(new ParkingSpotImpl("SpotL1-" + i, VehicleType.CAR));
            level2.addParkingSpot(new ParkingSpotImpl("SpotL2-" + i, VehicleType.BIKE));
        }

        // Add levels to parking lot
        parkingLot.addLevel(level1);
        parkingLot.addLevel(level2);

        // Create vehicles
        Vehicle car1 = new VehicleImpl("ABC123", VehicleType.CAR);
        Vehicle bike1 = new VehicleImpl("XYZ789", VehicleType.BIKE);

        // Park vehicles
        parkingLot.parkVehicle(car1);
        parkingLot.parkVehicle(bike1);

        // Retrieve a vehicle
        parkingLot.retrieveVehicle("ABC123");

        // Check available spots
        System.out.println("Available spots: " + parkingLot.getAvailableSpots());
    }
}
Leave a Comment