Loading...
Loading...
A parking lot has n numbered slots and charges a rate for every hour parked, with at least one hour charged. A car takes the lowest free slot; when it leaves, its slot becomes free (null) again.
Write a class Car (fields number, arrival) and a class ParkingLot with a Car[] slots field, a rate field and methods int findSlot(String number), int park(Car car) and int leave(int slot, int hour).
park NUMBER hour, leave NUMBER hour or status.PARKED NUMBER SLOT s, FULL or ALREADY PARKED NUMBER. For leave: FEE NUMBER amount or NOT FOUND NUMBER. For status: one line 1:X 2:Y … where each slot shows the car number or EMPTY.Example
Input
2 20 6 park KA01 9 park KA02 10 park KA03 10 leave KA01 12 park KA03 13 status
Output
PARKED KA01 SLOT 1 PARKED KA02 SLOT 2 FULL FEE KA01 60 PARKED KA03 SLOT 1 1:KA03 2:KA02
The lot is full for KA03 until KA01 leaves after 3 hours (fee 60); KA03 then takes slot 1.
Try the problem first. Open one hint at a time.
A new Car[n] array has n null slots: null means empty.
park returns the index of the first null slot it fills, or -1 when there is none.
Check slots[i] != null before reading slots[i].number, and compare numbers with equals.
The fee is (leave hour - arrival) × rate, with 0 hours counted as 1.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor