The parking lot system can be divided into various components:
import java.util.ArrayList;
import java.util.List;
// Enum for vehicle types
enum VehicleType {
CAR, TRUCK, VAN, MOTORCYCLE
}
// Enum for parking spot types
enum ParkingSpotType {
HANDICAPPED, COMPACT, LARGE, MOTORCYCLE
}
// ParkingSpot class
class ParkingSpot {
private ParkingSpotType type;
private boolean isOccupied;
public ParkingSpot(ParkingSpotType type) {
this.type = type;
this.isOccupied = false;
}
public boolean isOccupied() {
return isOccupied;
}
public void occupy() {
isOccupied = true;
}
public void vacate() {
isOccupied = false;
}
public ParkingSpotType getType() {
return type;
}
}
// Vehicle class
class Vehicle {
private VehicleType type;
public Vehicle(VehicleType type) {
this.type = type;
}
public VehicleType getType() {
return type;
}
}
// ParkingTicket class
class ParkingTicket {
private Vehicle vehicle;
private ParkingSpot spot;
private long entryTime;
private long exitTime;
private double fee;
public ParkingTicket(Vehicle vehicle, ParkingSpot spot) {
this.vehicle = vehicle;
this.spot = spot;
this.entryTime = System.currentTimeMillis();
}
public void setExitTime(long exitTime) {
this.exitTime = exitTime;
this.fee = calculateFee();
}
private double calculateFee() {
long durationInMillis = exitTime - entryTime;
long durationInHours = durationInMillis / (1000 * 60 * 60);
// Charge is $10 per hour
return durationInHours * 10.0;
}
public double getFee() {
return fee;
}
}
// ParkingLot class
class ParkingLot {
private List<ParkingSpot> spots;
private int capacity;
private int currentOccupancy;
private DisplayBoard displayBoard;
public ParkingLot(int capacity) {
this.capacity = capacity;
this.spots = new ArrayList<>();
this.currentOccupancy = 0;
this.displayBoard = new DisplayBoard(this);
initializeParkingSpots();
}
private void initializeParkingSpots() {
for (int i = 0; i < capacity; i++) {
if (i % 4 == 0) {
spots.add(new ParkingSpot(ParkingSpotType.HANDICAPPED));
} else if (i % 4 == 1) {
spots.add(new ParkingSpot(ParkingSpotType.COMPACT));
} else if (i % 4 == 2) {
spots.add(new ParkingSpot(ParkingSpotType.LARGE));
} else {
spots.add(new ParkingSpot(ParkingSpotType.MOTORCYCLE));
}
}
}
public boolean isFull() {
return currentOccupancy >= capacity;
}
public ParkingTicket parkVehicle(Vehicle vehicle) {
if (isFull()) {
displayBoard.displayFullMessage();
return null;
}
for (ParkingSpot spot : spots) {
if (!spot.isOccupied() && isSuitableSpot(vehicle, spot)) {
spot.occupy();
currentOccupancy++;
return new ParkingTicket(vehicle, spot);
}
}
return null;
}
private boolean isSuitableSpot(Vehicle vehicle, ParkingSpot spot) {
// Logic to check if a vehicle type can park in a spot
if (vehicle.getType() == VehicleType.MOTORCYCLE && spot.getType() == ParkingSpotType.MOTORCYCLE) {
return true;
}
if (vehicle.getType() == VehicleType.CAR && (spot.getType() == ParkingSpotType.COMPACT || spot.getType() == ParkingSpotType.LARGE)) {
return true;
}
if (vehicle.getType() == VehicleType.TRUCK && spot.getType() == ParkingSpotType.LARGE) {
return true;
}
if (vehicle.getType() == VehicleType.VAN && (spot.getType() == ParkingSpotType.COMPACT || spot.getType() == ParkingSpotType.LARGE)) {
return true;
}
return false;
}
public void vacateSpot(ParkingTicket ticket) {
ticket.getSpot().vacate();
currentOccupancy--;
displayBoard.updateDisplay();
}
public int getCurrentOccupancy() {
return currentOccupancy;
}
public int getCapacity() {
return capacity;
}
}
// DisplayBoard class
class DisplayBoard {
private ParkingLot parkingLot;
public DisplayBoard(ParkingLot parkingLot) {
this.parkingLot = parkingLot;
}
public void displayFullMessage() {
System.out.println("Parking Lot is Full!");
}
public void updateDisplay() {
System.out.println("Available Spots: " + (parkingLot.getCapacity() - parkingLot.getCurrentOccupancy()));
}
}
// Main class for testing
public class ParkingLotSystem {
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot(5000);
Vehicle car = new Vehicle(VehicleType.CAR);
Vehicle truck = new Vehicle(VehicleType.TRUCK);
ParkingTicket carTicket = parkingLot.parkVehicle(car);
if (carTicket != null) {
System.out.println("Car parked. Ticket issued.");
}
ParkingTicket truckTicket = parkingLot.parkVehicle(truck);
if (truckTicket != null) {
System.out.println("Truck parked. Ticket issued.");
}
// Simulate exit and payment
truckTicket.setExitTime(System.currentTimeMillis() + 3600000); // 1 hour later
System.out.println("Truck parking fee: $" + truckTicket.getFee());
parkingLot.vacateSpot(truckTicket);
parkingLot.displayBoard.updateDisplay();
}
}