-
Notifications
You must be signed in to change notification settings - Fork 0
/
parkinglot.kt
140 lines (114 loc) · 4.23 KB
/
parkinglot.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package questions.parkinglot
import java.util.concurrent.ConcurrentHashMap
val EMPTY_LICENSE_PLATE = ""
object ParkingLot {
private val levels: MutableList<Level> = mutableListOf()
private val vehicleSpotMap: ConcurrentHashMap<String, ParkingSpot> = ConcurrentHashMap()
fun addLevel(level: Level) {
levels.add(level)
}
@Synchronized
fun parkVehicle(vehicle: Vehicle): Boolean {
// Check if the vehicle is already parked
if (vehicleSpotMap.containsKey(vehicle.licensePlate)) {
println("Vehicle with license plate ${vehicle.licensePlate} is already parked.")
return false
}
// Find an available spot and park the vehicle
for (level in levels) {
val spot = level.findAvailableSpot(vehicle)
if (spot != null) {
spot.parkVehicle(vehicle)
vehicleSpotMap[vehicle.licensePlate] = spot
println("Vehicle parked at level ${level.floor}, spot ${spot.spotNumber}.")
return true
}
}
println("No available spots for vehicle type: ${vehicle.vehicleType}.")
return false
}
@Synchronized
fun unparkVehicle(vehicle: Vehicle): Boolean {
// Find the spot from the map and unpark the vehicle
val spot = vehicleSpotMap[vehicle.licensePlate]
if (spot != null) {
spot.unparkVehicle()
vehicleSpotMap.remove(vehicle.licensePlate)
println("Vehicle with license plate ${vehicle.licensePlate} unparked.")
return true
}
println("Vehicle with license plate ${vehicle.licensePlate} not found.")
return false
}
fun displayAvailability() {
for (level in levels) {
level.displayAvailability()
}
}
}
data class Level(val floor: Int, val capacity: Int) {
private val parkingSpots: MutableList<ParkingSpot> = mutableListOf()
init {
for (i in 0 until capacity) {
parkingSpots.add(ParkingSpot(spotNumber = i, spotType = SpotType.CAR_SPOT)) // Customize spot type as needed
}
}
fun findAvailableSpot(vehicle: Vehicle): ParkingSpot? {
// Return the first available spot that can fit the vehicle
return parkingSpots.firstOrNull { it.isAvailable && it.canFitVehicle(vehicle) }
}
fun displayAvailability() {
val availableCount = parkingSpots.count { it.isAvailable }
println("Level $floor: $availableCount parking spots available")
}
}
enum class SpotType {
CAR_SPOT,
TRUCK_SPOT,
MOTORCYCLE_SPOT
}
data class ParkingSpot(val spotNumber: Int, val spotType: SpotType) {
var licensePlate: String = EMPTY_LICENSE_PLATE
private set
var isAvailable: Boolean = true
private set
fun canFitVehicle(vehicle: Vehicle): Boolean {
return when (vehicle.vehicleType) {
VehicleType.CAR -> spotType == SpotType.CAR_SPOT
VehicleType.TRUCK -> spotType == SpotType.TRUCK_SPOT
VehicleType.MOTORCYCLE -> spotType == SpotType.MOTORCYCLE_SPOT
}
}
fun parkVehicle(vehicle: Vehicle) {
isAvailable = false
licensePlate = vehicle.licensePlate
}
fun unparkVehicle() {
isAvailable = true
licensePlate = EMPTY_LICENSE_PLATE
}
}
sealed class Vehicle(open val licensePlate: String, val vehicleType: VehicleType) {
data class Car(override val licensePlate: String) : Vehicle(licensePlate, VehicleType.CAR)
data class Truck(override val licensePlate: String) : Vehicle(licensePlate, VehicleType.TRUCK)
data class Motorcycle(override val licensePlate: String) : Vehicle(licensePlate, VehicleType.MOTORCYCLE)
}
enum class VehicleType {
CAR,
TRUCK,
MOTORCYCLE
}
fun main() {
val parkingLot = ParkingLot
parkingLot.addLevel(Level(floor = 1, capacity = 100))
parkingLot.addLevel(Level(floor = 2, capacity = 80))
val car = Vehicle.Car("ABC123")
val truck = Vehicle.Truck("XYZ789")
val motorcycle = Vehicle.Motorcycle("M1234")
parkingLot.parkVehicle(car)
parkingLot.parkVehicle(truck)
parkingLot.parkVehicle(motorcycle)
parkingLot.displayAvailability()
parkingLot.unparkVehicle(motorcycle)
parkingLot.displayAvailability()
}