0

Kotlin Shipping Calculator

I am having trouble producing an output in the terminal. Can someone tell me what is wrong with my code? The parameters are that orders that are over $75 have free shipping. Orders less than $75 have a 10% fee to the total amount. There is an international fee of 15% with the maximum fee of $50. The two inputs are a double and bolean. Where I am having trouble is the output. I do not know what is wrong with the code; in the terminal it says "no output." Please help. fun main(args: Array<String>) { fun shippingCost(amount: Double, international: Boolean): Double { if (international == false && amount > 75) { println(amount + 0.0) } else if (international == false && amount < 75) { return (amount * 0.10) } else if (international == true && amount < 333.3334) { return (amount * 0.15) } else { return (amount + 50.0) } val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) return(shippingCost(total, international)) } }

29th Apr 2021, 6:22 AM
nathon c
nathon c - avatar
6 ответов
+ 3
You need to add additional closing curly bracket to close the shippingCost method before reading the values. Also, inside the first if statement there should be return instead of println. Remove the extra closing bracket at the end of your code. This should get rid of errors, but your calculations are a bit off still.
29th Apr 2021, 3:22 PM
Aleksandrs
Aleksandrs - avatar
+ 2
Ejeh Wayne I never responded because Aleksandrs had already provided a suitable answer to the problem.
8th Aug 2021, 10:52 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
solution with less code - fun shippingCost(amount: Double, international: Boolean): Double { return when{ amount>75 && !international -> 0.0 amount<75 && !international -> amount*0.1 (amount*0.15>50) && international -> 50.0 else -> amount*0.15 } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
19th Feb 2022, 10:09 AM
rashid sayyed
rashid sayyed - avatar
+ 1
Hi nathon c I would like to test your code a bit to see if I can help, but can't get access to playground due to the current glitch. When it returns, I will see if I can help you resolve this problem.
29th Apr 2021, 8:39 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Rik Wittkopp hasnt it return?...its been four months now
8th Aug 2021, 8:45 PM
Ejeh Wayne
Ejeh Wayne - avatar
+ 1
1) Why you have a return statement at the bottom of the main function? it's completely unnecessary 2) Even with removing it, I get errors running your code that the code is unreachable 3) Why comparing amount to 333.3334? that's really a difficult way of doing this. The whole code need to be restructured. Please have a look here: https://code.sololearn.com/kotlin
2nd Apr 2022, 12:37 AM
Raed
Raed - avatar