Parking lot with fee & level
unknown
java
a month ago
9.4 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 ParkingTicket { Vehicle getVehicle(); ParkingSpot getSpot(); Payment getPayment(); void setPayment(Payment payment); } interface Payment { double getAmount(); String getPaymentMethod(); // e.g., "CREDIT_CARD", "DEBIT_CARD", "CASH" } interface PaymentProcessor { boolean processPayment(Payment payment); } interface Level { boolean parkVehicle(Vehicle vehicle); Vehicle retrieveVehicle(String licensePlate); int getAvailableSpots(); void addParkingSpot(ParkingSpot spot); ParkingSpot getSpot(String spotNumber); String getLevelName(); } interface ParkingLot { boolean parkVehicle(Vehicle vehicle, Payment payment); Vehicle retrieveVehicle(String licensePlate); int getAvailableSpots(); void addLevel(Level level); Level getLevel(String levelName); boolean processPayment(Payment payment); } // 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 PaymentImpl implements Payment { private double amount; private String paymentMethod; public PaymentImpl(double amount, String paymentMethod) { this.amount = amount; this.paymentMethod = paymentMethod; } @Override public double getAmount() { return amount; } @Override public String getPaymentMethod() { return paymentMethod; } } class PaymentProcessorImpl implements PaymentProcessor { @Override public boolean processPayment(Payment payment) { // Simulate payment processing System.out.println("Processing payment of " + payment.getAmount() + " using " + payment.getPaymentMethod()); return true; // Assume payment is always successful } } class ParkingTicketImpl implements ParkingTicket { private Vehicle vehicle; private ParkingSpot spot; private Payment payment; public ParkingTicketImpl(Vehicle vehicle, ParkingSpot spot) { this.vehicle = vehicle; this.spot = spot; } @Override public Vehicle getVehicle() { return vehicle; } @Override public ParkingSpot getSpot() { return spot; } @Override public Payment getPayment() { return payment; } @Override public void setPayment(Payment payment) { this.payment = payment; } } 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<>(); private PaymentProcessor paymentProcessor = new PaymentProcessorImpl(); @Override public boolean parkVehicle(Vehicle vehicle, Payment payment) { for (Level level : levels.values()) { if (level.parkVehicle(vehicle)) { // Set the payment for the ticket ParkingTicket ticket = new ParkingTicketImpl(vehicle, level.getSpot(vehicle.getLicensePlate())); ticket.setPayment(payment); // Process the payment return processPayment(payment); } } 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); } @Override public boolean processPayment(Payment payment) { return paymentProcessor.processPayment(payment); } } // 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"); // Create payments Payment paymentCar = new PaymentImpl(20.0, "CREDIT_CARD"); Payment paymentBike = new PaymentImpl(10.0, "DEBIT_CARD"); // Park vehicles with payment parkingLot.parkVehicle(car1, paymentCar); parkingLot.parkVehicle(bike1, paymentBike); // Retrieve a vehicle parkingLot.retrieveVehicle("ABC123"); // Check available spots System.out.println("Available spots: " + parkingLot.getAvailableSpots()); } }
Leave a Comment