package bhylu.bikelab class HireCounter(val capacity: Int) { init { require(capacity >= 0) } var hired = 0 private set val free: Int get() = capacity - hired fun hire(amount: Int): Boolean { if (amount <= 0 || amount > free) return false hired += amount return true } } data class HireRow(val id: String, val station: String, val minutes: Int) fun stationTotals(rows: List): Map { require(rows.size <= 1000) val seen = mutableSetOf() val totals = mutableMapOf() for (row in rows) { require(row.id.isNotBlank() && row.station.isNotBlank() && row.minutes in 1..240) require(seen.add(row.id)) { "Duplicate hire id" } totals[row.station] = (totals[row.station] ?: 0L) + row.minutes.toLong() } return totals.toSortedMap() }