Senior
33
/** * "Avto to'xtash joyi" sinfini yozing (raqobat yoki boshqa narsa bo'lmasin) * 1. Konstruktorda o'lcham (sig'im) belgilangan. Ikki metod mavjud * - boolean enter() - Mashina kirishi, to'xtash joyi to'la bo'lsa false qaytaradi. * - void exit() - Mashina chiqishi. */
Şirkətlər soruşuldu
AI-dan cavab
sobes.tech Süni İntellekt
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()` уменьшает счётчик, если в данный момент есть машины на стоянке.