 What I'm missing in parking fee solution? | Sololearn: Learn to code for FREE!

+22

# 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.

+8

(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.

+19

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)

+12

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) }

+7

CarrieForle perfect! I think I'm on the track. Let me check it

+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.

+6

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) }

+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 :)

+4

Simba Not necessary to make a variable but this is a proper way to write code.

+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)

+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.

+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)

+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.

+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.

+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 } }

+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)

+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...

+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

+1