Parking Lot without level and fee
unknown
java
a month ago
4.6 kB
2
Indexable
Never
import java.util.HashMap; import java.util.Map; // Interfaces interface ParkingSpot { boolean isAvailable(); void occupy(); void vacate(); String getSpotNumber(); } interface Vehicle { String getLicensePlate(); String getVehicleType(); // e.g., "CAR", "BIKE" } interface ParkingLot { boolean parkVehicle(Vehicle vehicle); Vehicle retrieveVehicle(String licensePlate); int getAvailableSpots(); } interface ParkingTicket { Vehicle getVehicle(); ParkingSpot getSpot(); } // Concrete Implementations class ParkingSpotImpl implements ParkingSpot { private String spotNumber; private boolean available = true; public ParkingSpotImpl(String spotNumber) { this.spotNumber = spotNumber; } @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; } } class VehicleImpl implements Vehicle { private String licensePlate; private String vehicleType; public VehicleImpl(String licensePlate, String vehicleType) { this.licensePlate = licensePlate; this.vehicleType = vehicleType; } @Override public String getLicensePlate() { return licensePlate; } @Override public String 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 ParkingLotImpl implements ParkingLot { private Map<String, ParkingSpot> spots = new HashMap<>(); private Map<String, Vehicle> parkedVehicles = new HashMap<>(); public ParkingLotImpl(int numOfSpots) { for (int i = 1; i <= numOfSpots; i++) { spots.put("Spot" + i, new ParkingSpotImpl("Spot" + i)); } } @Override public boolean parkVehicle(Vehicle vehicle) { for (ParkingSpot spot : spots.values()) { if (spot.isAvailable()) { spot.occupy(); parkedVehicles.put(vehicle.getLicensePlate(), vehicle); System.out.println("Vehicle with license plate " + vehicle.getLicensePlate() + " parked at " + spot.getSpotNumber()); return true; } } System.out.println("No available spots for vehicle with license plate " + vehicle.getLicensePlate()); 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()); return vehicle; } } } System.out.println("Vehicle with license plate " + licensePlate + " not found."); return null; } @Override public int getAvailableSpots() { int availableSpots = 0; for (ParkingSpot spot : spots.values()) { if (spot.isAvailable()) { availableSpots++; } } return availableSpots; } } // Main Application public class ParkingLotSystem { public static void main(String[] args) { // Create parking lot with 10 spots ParkingLot parkingLot = new ParkingLotImpl(10); // Create vehicles Vehicle car1 = new VehicleImpl("ABC123", "CAR"); Vehicle bike1 = new VehicleImpl("XYZ789", "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