Created
September 19, 2021 04:32
-
-
Save alfianyusufabdullah/78286cdcac02b190bbdf74ebf6f3465b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class Shipping { | |
JNE, JNT | |
} | |
class Product() { | |
fun calculateShippingPrice(price: Int, shipping: Shipping): Int{ | |
return when(shipping){ | |
Shipping.JNE -> 12 * price | |
Shipping.JNT -> 20 * price | |
else -> 0 | |
} | |
} | |
} | |
fun main() { | |
val product = Product() | |
val price = product.calculateShippingPrice(10_000, Shipping.JNE) | |
println(price) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Shipping { | |
abstract fun calculateShippingPrice(price: Int): Int | |
} | |
class JNE: Shipping() { | |
override fun calculateShippingPrice(price: Int): Int { | |
return 12 * price | |
} | |
} | |
class JNT: Shipping() { | |
override fun calculateShippingPrice(price: Int): Int { | |
return 20 * price | |
} | |
} | |
class Product() { | |
fun calculateShippingPrice(price: Int, shipping: Shipping): Int{ | |
return shipping.calculateShippingPrice(price) | |
} | |
} | |
fun main() { | |
val product = Product() | |
val price = product.calculateShippingPrice(10_000, JNE()) | |
println(price) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment