Sobes.tech
Senior
35

/** * Parašykite "Automobilių stovėjimo aikštelės" klasę (be konkurencijos ar pan.) * 1. Konstruktoriuje nustatomas dydis (talpa). Yra du metodai * - boolean enter() - Automobilio įvažiavimas, grąžina false, jei aikštelė pilna. * - void exit() - Automobilio išvažiavimas. */

Įmonės kur klausė
СБЕРСБЕР

Atsakymas iš AI

sobes.tech 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--;
    }
}

}


Класс `ParkingLot` хранит вместимость и текущее количество машин. Метод `enter()` проверяет, есть ли свободное место, и если да — увеличивает счётчик и возвращает `true`. Если мест нет — возвращает `false`. Метод `exit()` уменьшает счётчик, если в данный момент есть машины на стоянке.