+ 5

Where the error?

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. Code: fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if(hours in 1..5) total=hours*1.0 else if(hours>5&&hours<24) total=5+((hours-5)*0.2) else total=15+(2*0.5) print(total) }

30th Apr 2021, 11:06 PM
JavaidIqbal
JavaidIqbal - avatar
7 Respuestas
+ 11
In your 2nd condition total should be like this total = 5+((hours-5)*0.5) If hours is 50, it should be total = 50/24*15+(50%24)*0.5
1st May 2021, 12:27 AM
Simba
Simba - avatar
+ 5
Thanks it worked but can you explain it total=hours/24*15+(hours%24)*0.5 Simba
1st May 2021, 12:37 AM
JavaidIqbal
JavaidIqbal - avatar
+ 5
If the guy parks for 2 or more full days you will need to calculate each 24 hours and multiply that by 15$ That's why you get the "hours/24*15" section. Then you need to get the amount of hours that remains unaccounted for using the % operator and multiply that by 0.5$ that gets you to "(hours%24)*0.5"
1st May 2021, 5:03 AM
Abd-ElRahman Abdeen
+ 4
JavaidIqbal This is my code, Hope maybe helps you: fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if (hours > 24) { total += 15 * (hours / 24) + 0.5 * (hours % 24) } else if(hours < 24 && hours > 5) { total += (hours - 5) * 0.5 + 5 } else { total += hours * 1 } println(total) }
2nd May 2021, 7:25 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
+ 3
How about if someone parks for 2 days or longer? You need to calculate out all the days for the 24 hour rate. Then 0.5 for the balance. If someone parked for 50 hours. That would be 2 days and 2 hours. That's why we have to calculate total in this way :)
1st May 2021, 12:53 AM
Simba
Simba - avatar
1st May 2021, 5:36 AM
JavaidIqbal
JavaidIqbal - avatar
+ 1
Welcome ☺️☺️
3rd May 2021, 2:27 AM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar