Parking fee | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Parking fee

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. This means, that, for example, if a car parked for 26 hours, the bill should be 15+(2*0.5) = 16.0, because it was parked for 24 hours plus 2 additional hours. Sample Input: 8 Sample Output: 6.5 Explanation: The first 5 hours are billed at $1/hour, which results in $5. After that, the next 3 hours are billed at $0.5/hour = $1.5. So, the total would be $5+$1.5 = $6.5 GUYS WHAT'S MISSING IN THIS CODE var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours>24){ total+=15*(hours)24)+0.5*(hours%24) } elseif(hours<24 && hours>5){ total+=((hours-5)*0.5+5)) } else{ total+=hours*1 } println(total)

14th Aug 2022, 10:16 AM
Ayebazibwe Morris
Ayebazibwe Morris - avatar
1 Answer
+ 2
You missed the case when hours is exactly 24, and some typos: you wrote (hours)24 instead of (hours/24), and you're closing too many parentheses at ((hours-5)*0.5 + 5)), you should just (hours-5)*0.5 + 5. Like this: if(hours >= 24) total += 15 * (hours/24) + 0.5 * (hours%24) if(hours < 24 && hours > 5) total += (hours-5)*0.5 + 5 The rest seems fine. (interesting way of billing, btw: if you park for 23 hours, you pay $14, but if you stay one hour longer, you pay $15, as if the last hour is $1 again. Moreover, if you park for 47h you pay $26.5, but stay one hour more and it will cost you $30, as if that one last hour costs $3.5 🙂)
14th Aug 2022, 10:44 AM
lion
lion - avatar