The balance must never be negative. Find the object-boundary bug.
kotlin
class Wallet(initialCents: Int) {
var balanceCents: Int = initialCents
init {
require(initialCents >= 0)
}
fun spend(cents: Int): Boolean {
require(cents > 0)
if (cents > balanceCents) return false
balanceCents -= cents
return true
}
}
fun main() {
val wallet = Wallet(500)
wallet.balanceCents = -1
println(wallet.balanceCents)
}