Sobes.tech
Senior
39

/** * Napišite klasu "Parking" (bez konkurencije ili nečega sličnog) * 1. U konstruktoru se postavlja veličina (kapacitet). Postoje dve metode * - boolean enter() - Ulazak automobila, vraća false ako je parking pun. * - void exit() - Izlazak automobila. */

Компаније где су питане
СБЕРСБЕР

Одговор од АИ

sobes.tech АИ

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