Kotlin Parking Fee Project | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Kotlin Parking Fee Project

My code is a little hard to read but it works with all test cases except 6, can someone tell me whats wrong with it? You are making a car parking software that needs to calculate and output the amount due based on the number of hours the car was parked. The fee is calculated based on the following price structure: - the first 5 hours are billed at $1 per hour. - after that, each hour is billed at $0.5 per hour. - for each 24 hours, there is a flat fee of $15. fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 var x = hours-5 if (hours>=24) {var y = (((hours-(hours%24))/24)) var z = ((((hours-(y*24)))/2)) total += (y*15)+z} if (hours>=5 && hours <=24) {total += (x*0.5)+5} if (hours<5) {total += hours} println(total) }

8th Mar 2023, 5:17 AM
Tanner
Tanner - avatar
3 Answers
+ 2
The var 'z' is of inferred type Int, but should evaluate to float or double. In particular, odd numbers of parking hours longer than 24h willcalculate incorrectly.
8th Mar 2023, 5:45 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total : Double = 0.0 if(hours <= 5){ var result: Double = hours * 1.0 total = result } else if (hours > 5 && hours < 24){ var result: Double = (hours - 5.0) * 0.5 total = 5.0 + result } else { var result: Double = (15.0 * Math.floor(hours /24.0)) + ((hours% 24.0)*0.5) total = result } println(total) } //Tanner use this don't be so complicated
8th Mar 2023, 5:45 AM
I am offline
I am offline - avatar
+ 1
Thanks guys. I am working on this myself. I like Kotlin.
8th Mar 2023, 6:08 PM
The Tina Experience
The Tina Experience - avatar