└── Parking Lot └── task └── src └── parking └── Main.kt /Parking Lot/task/src/parking/Main.kt: -------------------------------------------------------------------------------- 1 | package parking 2 | 3 | import java.util.* 4 | 5 | val scanner = Scanner(System.`in`) 6 | 7 | data class Car(val registration: String, val color: String) 8 | 9 | fun main() { 10 | var spots: MutableList = mutableListOf() 11 | 12 | while (true) { 13 | val action = scanner.next() 14 | when (action) { 15 | "exit" -> break 16 | "status" -> status(spots) 17 | "create" -> spots = create() 18 | "leave" -> leave(spots) 19 | "park" -> park(spots) 20 | "reg_by_color" -> reg_by_color(spots) 21 | "spot_by_color" -> spot_by_color(spots) 22 | "spot_by_reg" -> spot_by_reg(spots) 23 | } 24 | } 25 | } 26 | 27 | fun spot_by_reg(spots: MutableList) { 28 | if (spots.isEmpty()) { 29 | println("Sorry, a parking lot has not been created.") 30 | return 31 | } 32 | 33 | val reg = scanner.next() 34 | var count = 0 35 | 36 | for (i in spots.indices) { 37 | val car = spots[i] 38 | 39 | if (car != null) { 40 | if (car.registration.toUpperCase() == reg.toUpperCase()) { 41 | println("${i + 1}") 42 | count++ 43 | } 44 | } 45 | } 46 | if (count == 0) 47 | println("No cars with registration number $reg were found.") 48 | } 49 | 50 | fun spot_by_color(spots: MutableList) { 51 | if (spots.isEmpty()) { 52 | println("Sorry, a parking lot has not been created.") 53 | return 54 | } 55 | 56 | val color = scanner.next() 57 | var sp = "" 58 | 59 | for (i in spots.indices) { 60 | val car = spots[i] 61 | if (car != null) { 62 | if (car.color.toLowerCase() == color.toLowerCase()) { 63 | sp += " " + i 64 | } 65 | } 66 | } 67 | 68 | var nnn = 0 69 | val p = sp.split(" ") 70 | 71 | if (p.size == 0 || (p[0] == "" && p.size == 1)) 72 | println("No cars with color $color were found.") 73 | 74 | else { 75 | nnn = p[1].toInt() + 1 76 | print(nnn) 77 | if (p.size > 2) 78 | for (i in 2 until p.size) { 79 | nnn = p[i].toInt() + 1 80 | print(", " + nnn) 81 | } 82 | println() 83 | } 84 | } 85 | 86 | fun reg_by_color(spots: MutableList) { 87 | if (spots.isEmpty()) { 88 | println("Sorry, a parking lot has not been created.") 89 | return 90 | } 91 | 92 | val color = scanner.next() 93 | var sp = "" 94 | 95 | for (i in spots.indices) { 96 | val car = spots[i] 97 | if (car != null) { 98 | if (car.color.toLowerCase() == color.toLowerCase()) { 99 | sp += "${car.registration} " 100 | } 101 | } 102 | } 103 | if (sp.length == 0) 104 | println("No cars with color $color were found.") 105 | else { 106 | val a= sp.split(" ") 107 | print(a[0]) 108 | if (a.size > 1) 109 | for (i in 1 until a.size - 1) { 110 | print(", " + a[i]) 111 | } 112 | println() 113 | } 114 | } 115 | 116 | fun status(spots: List) { 117 | if (spots.isEmpty()) { 118 | println("Sorry, a parking lot has not been created.") 119 | return 120 | } 121 | 122 | val noOfOccupiedSpots = spots.count { car -> car != null } 123 | if (noOfOccupiedSpots == 0) { 124 | println("Parking lot is empty.") 125 | return 126 | } 127 | 128 | for (i in spots.indices) { 129 | val car = spots[i] 130 | if (car != null) { 131 | println("${i + 1} ${car.registration} ${car.color}") 132 | } 133 | } 134 | } 135 | 136 | fun create(): MutableList { 137 | val size = scanner.nextInt() 138 | println("Created a parking lot with $size spots.") 139 | return MutableList(size) { null } 140 | } 141 | 142 | fun leave(spots: MutableList) { 143 | if (spots.isEmpty()) { 144 | println("Sorry, a parking lot has not been created.") 145 | return 146 | } 147 | 148 | val spot = scanner.nextInt() 149 | if (spots[spot - 1] == null) { 150 | println("There is no car in spot $spot.") 151 | } else { 152 | spots[spot - 1] = null 153 | println("Spot $spot is free.") 154 | } 155 | } 156 | 157 | fun park(spots: MutableList) { 158 | if (spots.isEmpty()) { 159 | println("Sorry, a parking lot has not been created.") 160 | return 161 | } 162 | 163 | val firstFreeSpot = spots.indexOfFirst { car -> car == null } 164 | if (firstFreeSpot < 0) { 165 | println("Sorry, the parking lot is full.") 166 | return 167 | } 168 | 169 | val registration = scanner.next() 170 | val color = scanner.next() 171 | 172 | spots[firstFreeSpot] = Car(registration, color) 173 | 174 | println("$color car parked in spot ${firstFreeSpot + 1}.") 175 | } --------------------------------------------------------------------------------