Sobes.tech
Senior

/** * Écrire une classe "Stationnement" (sans concurrence ni autres fonctionnalités) * 1. La taille (capacité) est définie dans le constructeur. Il y a deux méthodes * - boolean enter() - Entrée de la voiture, retourne false si le parking est plein. * - void exit() - Sortie de la voiture. */

sobes.tech IA

Réponse de l'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()` уменьшает счётчик, если в данный момент есть машины на стоянке.