+ 20

What I'm missing in parking fee solution?

This is the 2nd end of modules project of the Kotlin course. The task is given below 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 https://code.sololearn.com/c0ut79eL45B8/?ref=app NB: The output should be a Double, even if the amount is a round number.

15th Feb 2021, 4:08 PM
Simba
Simba - avatar
26 ответов
+ 10
(1) If hours is 49. It should be 15 * 2 + 1* 0.5 = 30.5, not 15 + 25 * 0.5 = 27.5 (2) In the else if statement the hours should be < 24 instead of <=. (3) The output is always double, but in the first if statement you print an int.
15th Feb 2021, 4:13 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 21
Rafique Some corrections in your logic. I don't think to store hours/24 in a variable. It will unnecessary create a memory and also there should be else if instead of else as Simba said or you can just have else without hours >=24 because it will go bydefault in that condition when hours will be greater than 24. No need write println in every condition. var hours = readLine()!!.toInt() var total: Double = 0.0 if (hours <= 5) { total = hours * 1.0 } else if (hours > 5 && hours < 24) { total=5+((hours-5)*0.5) } else { total= (15 * hours / 24) + (0.5 * hours % 24) } println(total)
16th Feb 2021, 2:34 AM
A͢J
A͢J - avatar
+ 17
Simba Hours is an integer value no need to convert in double There hours maybe more than 48 hours. You can try this logic 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) }
16th Feb 2021, 2:47 AM
A͢J
A͢J - avatar
+ 6
CarrieForle perfect! I think I'm on the track. Let me check it
15th Feb 2021, 5:39 PM
Simba
Simba - avatar
+ 6
Rafique Sir Thanks for taking your time. I think we need to use else if instead of else statement or have to remove the condition in our solution.
16th Feb 2021, 2:07 AM
Simba
Simba - avatar
+ 5
This is mine. I hope this can help you fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 var day : Int = hours/24 var addhours : Int = hours%24 total = when { hours >5 && hours <24 -> 5.0+(hours-5)*0.5 hours >= 24 -> (addhours*0.5) + (day*15) else -> hours*1.0 } println(total) }
6th Mar 2021, 2:13 PM
M. Huda Saputra
M. Huda Saputra - avatar
+ 5
Pablo Kagioglu Jr. You need to multiply hours by 1.0 in your first condition since total is Double hours <= 5 -> hours * 1.0 It would be good if you could create an own post to clear your doubts :)
24th Mar 2021, 2:41 PM
Simba
Simba - avatar
+ 4
Simba Not necessary to make a variable but this is a proper way to write code.
16th Feb 2021, 6:04 AM
A͢J
A͢J - avatar
+ 4
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 } println(total)
16th May 2021, 5:34 PM
Ahmad Zherati
Ahmad Zherati - avatar
+ 3
Oh, I didn't notice that. And yes, in both my solution and your solution but modified by me, every tests are passed without loops.
15th Feb 2021, 5:07 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 3
I tired several logics write the function in an unbuggy way... this is what I have written and it worked perfectly!! var hours = readLine()!!.toInt() var r = (hours / 24).toInt() var total: Double = 0.0 if(hours >= 24){ hours = hours - (r*24) total = total+(r*15) if(hours >=0){ total = total+(0.5*hours) } } else if(hours > 5 && hours < 24){ hours -= 5 total += 5 total = total+(0.5*hours) }else{ total = hours * 1.0 } println(total)
19th Feb 2021, 9:22 AM
Hussein Alaeddine
Hussein Alaeddine - avatar
+ 2
var hours = readLine()!!.toInt() var total: Double = 0.0 var days = hours/24 if (hours <= 5){ total= hours*1.0 println(total) } else if (hours > 5 && hours < 24){ total=5+((hours-5)*0.5) println(total) } else (hours >=24){ total= (days*15)+((hours%24)*0.5) println(total) } I retained the variable total as given in the problem, and accommodated it in the code given by Simba, but seemed to have missed out somewhere as it outputs nothing.
15th Feb 2021, 7:28 PM
Rafique
Rafique - avatar
+ 2
I tried doing it with a when clause: fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 total = when { hours <= 5 -> hours hours > 5 && hours < 24 -> 5+((hours-5)*0.5) else -> { (15 * hours/24) + (0.5 * hours % 24) } } println(total) } for some reason this keeps returning "no output" even though the print statement is RIGHT THERE. Just do it with if statements, the program wants you to fail for being creative.
24th Mar 2021, 1:28 PM
Pablo Kagioglu Jr.
Pablo Kagioglu Jr. - avatar
+ 2
fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = calculateFee(hours) println(total) } fun calculateFee(hours: Int):Double { return when{ hours<=0 -> 0.0 hours>=24 -> (hours/24).toInt()*15 + (hours%24)*0.5 hours>=5 -> 5+(hours-5)*0.5 hours>0 -> hours*1.0 else -> 0.0 } }
24th Dec 2021, 6:34 PM
Sharath
+ 2
if (hours <= 5) { total = hours * 1.0 } else if (hours > 5 && hours < 24) { total=5+((hours-5)*0.5) } else { total= (15 * (hours / 24)) + (0.5 * (hours % 24)) } println(total)
1st Apr 2022, 7:54 AM
P®|€$π
+ 2
if (hours <= 5) { total = hours * 1.0 } else if (hours > 5 && hours < 24) { total=5+((hours-5)*0.5) } else { total= (15 * (hours / 24)) + (0.5 * (hours % 24)) } println(total) Try this...
1st Apr 2022, 7:56 AM
Omanudhowho Oreva Benedict
Omanudhowho Oreva Benedict - avatar
+ 1
A bit of a wilder approach (using no extra variables): var hours = readLine()!!.toInt() var total: Double = 0.0 if (hours <= 5.0) { total = hours * 1.0 } else if (hours < 24.0) { total = 5.0 + (hours - 5.0) * 0.5 } else { total = (((hours - (hours % 24.0)) / 24) * 15.0) + (hours % 24.0 * 0.5) } println(total) In the last calculation (the left part from the '+'), I try to figure out how many full days we have in the amount of hours by doing a modulus and subtracting the remainder from the full amount of hours, which I then have to divide by 24 to get the amount of full days. Bit more complicated but gets it done all the same
1st Mar 2021, 9:03 AM
Mike
Mike - avatar
+ 1
I had thinking in the solution 2 days in a row, this works in my case. I know it could be optimized but i had not program since 5 year ago fun main(args: Array<String>) { var hours = readLine()!!.toDouble() var total: Double = 0.0 var ayudador: Double = 1.0 var ayudador2: Double = hours if(hours<=5){ total=hours }else if (hours>5 && hours<24 ){ total=5+((hours-5)*0.5) } else if (hours>=24 ){ if(hours == 24.0 ){ total=15.0 }else if(hours>24 ){ while((ayudador2>= 24)){ ayudador2= (hours-(ayudador*24)) if(ayudador2<24 ){ total=(15*(ayudador))+((hours-(24*ayudador))*0.5) }else{ ayudador2=hours ayudador+=1 } } } } println(total) // { }<> }
27th Jun 2021, 7:36 AM
Bharush Lucha Lopez
Bharush Lucha Lopez - avatar
+ 1
whats wrong with this code fun main(args: Array<String>) { var hours = readLine()!!.toInt() var total: Double = 0.0 if (hours < 5) { total = hours*1.0 } else if (hours in 6..23) { total = ((hours-5) * 0.5) + 5.0 } else if (hours == 24) { total = 15.0 } else if (hours >= 25) { total = ((hours-24)*0.5) + 15.0 } println(total) }
11th Oct 2021, 9:29 PM
Nats Angel
Nats Angel - avatar
+ 1
Solution 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) }
1st Dec 2021, 11:14 AM
Nichervan Essa Mahammad
Nichervan Essa Mahammad - avatar