Senior
/** * Escrever uma classe "Estacionamento" (sem concorrência ou algo do tipo) * 1. No construtor define-se o tamanho (capacidade). Existem dois métodos * - boolean enter() - Entrada do carro, retorna false se o estacionamento estiver cheio. * - void exit() - Saída do carro. */
sobes.tech IA
Resposta da IA
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--;
}
}
}
Класс `ParkingLot` хранит вместимость и текущее количество машин. Метод `enter()` проверяет, есть ли свободное место, и если да — увеличивает счётчик и возвращает `true`. Если мест нет — возвращает `false`. Метод `exit()` уменьшает счётчик, если в данный момент есть машины на стоянке.