Skip to content

Instantly share code, notes, and snippets.

@alfianyusufabdullah
Created September 19, 2021 04:32
Show Gist options
  • Save alfianyusufabdullah/78286cdcac02b190bbdf74ebf6f3465b to your computer and use it in GitHub Desktop.
Save alfianyusufabdullah/78286cdcac02b190bbdf74ebf6f3465b to your computer and use it in GitHub Desktop.
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)
}
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