Senior
/ ** * Write a class "ParkingLot" (no competition or anything else) * 1. The size (capacity) is set in the constructor. There are two methods: * - boolean enter() - Car entry, returns false if the parking lot is full. * - void exit() - Car exit. * /
sobes.tech AI
Answer from AI
java public class ParkingLot { private final int capacity; private int currentCount = 0;
public ParkingLot(int capacity) {
this.capacity = capacity;
}
public boolean enter() {
if (currentCount < capacity) {
currentCount++;
return true;
} else {
return false;
}
}
public void exit() {
if (currentCount > 0) {
currentCount--;
}
}
}
The `ParkingLot` class stores capacity and the current number of cars. The `enter()` method checks if there is a free space, and if so — increments the counter and returns `true`. If there are no spaces — returns `false`. The `exit()` method decreases the counter if there are cars currently in the parking lot.