Tamas Szigeti
01/15/2024, 4:21 PMdata
class
*ParkingLot*(*val* size: Int) {
data
class
*Car*(*val* plate: String, *val* color: String) {
override
fun
*toString*() = "$plate $color"
}
data
class
*Spot*(*val* spotNumber: Int, *val* isFree: Boolean, *val* car: Car?) {
override
fun
*toString*() = "$spotNumber $car"
}
private
*val* parkingData = Array(size) { Spot(it + 1, *true*, *null*) }
*init* {
*if* (size != 0) println("Created a parking lot with $size spots.")
}
override
fun
*toString*(): String {
*val* str = parkingData.filter { !it.isFree }.joinToString("\n") { it.toString() }
return
*when* {
size == 0 -> "Sorry, a parking lot has not been created."
str.isEmpty() -> "Parking lot is empty."
*else* -> str
}
}
}
fun
*main*() {
*var* parkingLot = ParkingLot(0)
*while* (*true*) {
*val* command = readLine()!!.split(" ")
*when* (command[0]) {
"create" -> parkingLot = ParkingLot(command[1].toInt())
//"park" -> parkingLot.park(ParkingLot.Car(command[1], command[2]))
//"leave" -> parkingLot.leave(command[1].toInt())
"status" -> println(parkingLot)
"exit" -> *return*
}
}
}
If it is not the correct channel, which channel would it be?Goetz Markgraf
01/15/2024, 5:11 PMwhen
statement, there is a status
case. In this case, you explicitly call println
with the instance of ParkingLot
. This class decends from Any
which is Object
in Java (more or less).
println(Object)
calls toString
on the object. That’s why this is called.