PL without Level
import java.util.ArrayList; import java.util.List; import java.util.Optional; public class ParkingLotSystem { // Enum for Vehicle Size public enum VehicleSize { SMALL, COMPACT, LARGE } // Interface for Vehicle public interface Vehicle { String getLicensePlate(); VehicleSize getSize(); } // Interface for ParkingSpot public interface ParkingSpot { boolean isAvailable(); void parkVehicle(Vehicle vehicle); void removeVehicle(); Vehicle getVehicle(); int getSpotId(); } // Interface for ParkingLot public interface ParkingLot { Optional<ParkingSpot> findAvailableSpot(Vehicle vehicle); boolean parkVehicle(Vehicle vehicle); boolean leaveSpot(int spotId); int availableSpots(); double calculateFee(int spotId, long hoursParked); } // Interface for PaymentProcessor public interface PaymentProcessor { double processPayment(int spotId, long hoursParked); } // Implementing Vehicle Classes public static class Car implements Vehicle { private String licensePlate; private VehicleSize size = VehicleSize.COMPACT; public Car(String licensePlate) { this.licensePlate = licensePlate; } @Override public String getLicensePlate() { return licensePlate; } @Override public VehicleSize getSize() { return size; } } public static class Bike implements Vehicle { private String licensePlate; private VehicleSize size = VehicleSize.SMALL; public Bike(String licensePlate) { this.licensePlate = licensePlate; } @Override public String getLicensePlate() { return licensePlate; } @Override public VehicleSize getSize() { return size; } } public static class Truck implements Vehicle { private String licensePlate; private VehicleSize size = VehicleSize.LARGE; public Truck(String licensePlate) { this.licensePlate = licensePlate; } @Override public String getLicensePlate() { return licensePlate; } @Override public VehicleSize getSize() { return size; } } // Implementing ParkingSpot Class public static class SimpleParkingSpot implements ParkingSpot { private int spotId; private Vehicle currentVehicle; private VehicleSize spotSize; public SimpleParkingSpot(int spotId, VehicleSize spotSize) { this.spotId = spotId; this.spotSize = spotSize; } @Override public boolean isAvailable() { return currentVehicle == null; } @Override public void parkVehicle(Vehicle vehicle) { if (vehicle.getSize().ordinal() <= spotSize.ordinal()) { this.currentVehicle = vehicle; } } @Override public void removeVehicle() { this.currentVehicle = null; } @Override public Vehicle getVehicle() { return currentVehicle; } @Override public int getSpotId() { return spotId; } } // Implementing ParkingLot Class public static class SimpleParkingLot implements ParkingLot { private List<ParkingSpot> spots; private PaymentProcessor paymentProcessor; public SimpleParkingLot(int numberOfSpots, PaymentProcessor paymentProcessor) { this.spots = new ArrayList<>(); this.paymentProcessor = paymentProcessor; for (int i = 1; i <= numberOfSpots; i++) { spots.add(new SimpleParkingSpot(i, VehicleSize.COMPACT)); } } @Override public Optional<ParkingSpot> findAvailableSpot(Vehicle vehicle) { return spots.stream() .filter(ParkingSpot::isAvailable) .filter(spot -> vehicle.getSize().ordinal() <= spot.getVehicle().getSize().ordinal()) .findFirst(); } @Override public boolean parkVehicle(Vehicle vehicle) { Optional<ParkingSpot> availableSpot = findAvailableSpot(vehicle); if (availableSpot.isPresent()) { availableSpot.get().parkVehicle(vehicle); return true; } return false; } @Override public boolean leaveSpot(int spotId) { for (ParkingSpot spot : spots) { if (spot.getSpotId() == spotId && !spot.isAvailable()) { spot.removeVehicle(); return true; } } return false; } @Override public int availableSpots() { return (int) spots.stream().filter(ParkingSpot::isAvailable).count(); } @Override public double calculateFee(int spotId, long hoursParked) { return paymentProcessor.processPayment(spotId, hoursParked); } } // Implementing PaymentProcessor Class public static class FlatRatePaymentProcessor implements PaymentProcessor { private static final double RATE_PER_HOUR = 2.0; @Override public double processPayment(int spotId, long hoursParked) { return RATE_PER_HOUR * hoursParked; } } // Main Class for Testing public static void main(String[] args) { PaymentProcessor paymentProcessor = new FlatRatePaymentProcessor(); ParkingLot parkingLot = new SimpleParkingLot(10, paymentProcessor); Vehicle car = new Car("ABC123"); Vehicle bike = new Bike("XYZ456"); parkingLot.parkVehicle(car); parkingLot.parkVehicle(bike); System.out.println("Available spots: " + parkingLot.availableSpots()); parkingLot.leaveSpot(1); System.out.println("Available spots after leaving: " + parkingLot.availableSpots()); double fee = parkingLot.calculateFee(1, 3); System.out.println("Parking fee for 3 hours: $" + fee); } }
Leave a Comment