+ 1

[Solved] Q. Can someone help in shipping calculator task in Kotlin? My attempt is below.

fun shippingCost(amount: Double, international: Boolean): Double { if (total<=75) { total*10/100 } while (international==true){ total*15/100{ break(total>=50) } } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }

3rd Mar 2021, 12:23 PM
Shobhit :)
Shobhit :) - avatar
19 ответов
+ 6
Here is the full runnable code from my solution -> https://code.sololearn.com/cdyDo0Yljw0m/?ref=app
4th Mar 2021, 6:21 PM
Trinity ⚜
Trinity ⚜ - avatar
+ 10
fun shippingCost(amount: Double, international: Boolean): Double { var cost = 0.0 if (international) { cost = if (amount * 0.15 > 50) 50.0 else amount * 0.15 } else if (amount < 75) { cost = amount * 0.1 } return cost } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
8th Mar 2021, 12:15 PM
Namrata Dattani
Namrata Dattani - avatar
+ 5
You don't have a variable "total" in your function. The fuction gets it as parameter from the function call in the mainmethod. So you should rename it to "amount" to work with the user input value. Declare a new variable e.g. "costs" which takes the result and don't forget to return your that variable at the end.
3rd Mar 2021, 12:55 PM
Trinity ⚜
Trinity ⚜ - avatar
+ 4
fun shippingCost(amount: Double, international: Boolean): Double { if(international==false){ if(amount >=75.0) { return 0.0 } else { return amount*0.1 } } else { return if(amount*0.15 >50.0){ return 50.0 } else { amount *0.15 } } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
20th Mar 2021, 9:53 AM
Wamala Ferdinard Albakr
+ 3
fun shippingCost(amount: Double, international: Boolean): Double { var total: Double = 0.0 if(international == true){ if(amount * 0.15 < 50){ total = amount * 0.15 }else{ total = 50.0 } }else{ if(amount > 75){ total }else{ total = amount * 0.1 } } return total } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
16th Jan 2022, 4:46 PM
Francis Njeri
Francis Njeri - avatar
8th Mar 2021, 9:41 PM
Hassan Ghobeyshvi
Hassan Ghobeyshvi - avatar
+ 1
please resend your full code i don't do it this project
4th Mar 2021, 3:01 PM
Muhammadaziz Ravshanbekov
Muhammadaziz Ravshanbekov - avatar
+ 1
Thank you so much Trinity ⚜ 😊
5th Mar 2021, 2:49 AM
Shobhit :)
Shobhit :) - avatar
+ 1
Paramjit Singh Rana, I've already done the task with the help of Trinity ⚜:)
17th Mar 2021, 2:01 PM
Shobhit :)
Shobhit :) - avatar
+ 1
SIMPLEST SOLUTION: if(international==false){ //USA if(amount<75) return amount*0.1 return 0.0 } else { if(amount*0.15<50) return amount*0.15 return 50.0 }
10th Oct 2021, 1:49 PM
Paul
Paul - avatar
+ 1
//This works perfectly fun shippingCost(amount: Double, international: Boolean): Double { var cost=0.0 if(international){ //case internationl cost= if(amount*0.15>50)50.0 else amount*0.15 }else{ //Case Us cost= if (amount >=75)0.0 else amount*0.10 } return cost } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
31st Dec 2021, 3:36 PM
Negar Yeganeh
Negar Yeganeh - avatar
0
Muhammadaziz Ravshanbekov, even I don't know how to do none are helping..
4th Mar 2021, 3:05 PM
Shobhit :)
Shobhit :) - avatar
0
Thank you so much for your time, Namrata Dattani, but I've done the task.
8th Mar 2021, 12:27 PM
Shobhit :)
Shobhit :) - avatar
0
Paramjit Singh Rana the code is running fine. Put a double value in the first line of the input and a boolean value in second line than hit run. Thank you Shobhit Pandey...you're welcome. I'm happy I could help out. 🙂
17th Mar 2021, 6:15 PM
Trinity ⚜
Trinity ⚜ - avatar
0
fun shippingCost(amount: Double, international: Boolean): Double { var total : Double = 0.0 if (international == false){ if(amount >= 75.0){ total = 0.0 }else { total = amount * 0.1 } } else { if(amount * 0.15 < 50){ total = amount * 0.15 } else { total = 50.0 } } return total } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
15th Apr 2022, 11:17 AM
Abbas Mahdiyeh
Abbas Mahdiyeh - avatar
0
fun shippingCost(amount: Double, international: Boolean): Double { if(international==false){ if(amount >=75.0) { return 0.0 } else { return amount*0.1 } } else { return if(amount*0.15 >50.0){ return 50.0 } else { amount *0.15 } } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
16th Sep 2022, 1:49 PM
Kanisak Shakya
Kanisak Shakya - avatar
0
short and good! fun shippingCost(amount: Double, international: Boolean): Double { if(international==false){ if(amount >=75.0){ return 0.0} else{ return amount*0.1 }} else {return if(amount*0.15 >50.0){ return 50.0} else{amount *0.15 }}}fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international))}
5th Oct 2022, 3:49 PM
Marcel Krättli
0
fun main(args: Array<String>) { println("Enter amount") val total = readLine()!!.toDouble() println("true or false") val international = readLine()!!.toBoolean() println(shippingCost(total, international)) } fun shippingCost(amount: Double, international: Boolean): Any { val r = when { international == false && amount >= 75 -> return 0.0 international == false && amount < 75 -> return amount * 0.10 (international == true) && (amount * 0.15 > 50) -> return 50.0 international == true -> return amount*0.15 else -> println("Error") } return r }
31st May 2023, 9:44 AM
Hossam Eldin Aref
0
fun shippingCost(amount: Double, international: Boolean): Double { var cost = 0.0 if (international) { cost = if (amount * 0.15 > 50) 50.0 else amount * 0.15 } else if (!international) { cost = if (amount > 75) 0.0 else amount * 0.1 } return cost } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
4th Jun 2023, 3:29 PM
Xolmirzayev Shaxriyor
Xolmirzayev Shaxriyor - avatar