Last active
November 28, 2017 22:52
-
-
Save omayib/8ba095a5ac9b4e0c25e6b789e08028b0 to your computer and use it in GitHub Desktop.
The real world implementation of strategy pattern in Kotlin
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
/** | |
* Created by omayib on 22/05/17. | |
*/ | |
fun main(args: Array<String>){ | |
val tShirtUbuntu = TShirt(70000,2) | |
val tShirtAndroid = TShirt(50000,4) | |
val tShirtCoder = TShirt(80000,3) | |
val bill = Bill() | |
bill.add(tShirtUbuntu) | |
bill.add(tShirtAndroid) | |
bill.add(tShirtCoder) | |
val priceUsingDefaultCoupon = bill.calculate(DefaultCoupon()) | |
val priceUsingSpecialCoupon = bill.calculate(SpecialCoupon()) | |
val priceUsingNewYearCoupon = bill.calculate(NewYearCoupon()) | |
val priceUsingChristmasCoupon = bill.calculate(ChristmasCoupon()) | |
println(String.format("the total price is $priceUsingDefaultCoupon")) | |
println(String.format("the total price with special discount is $priceUsingSpecialCoupon")) | |
println(String.format("the total price with new year discount is $priceUsingNewYearCoupon")) | |
println(String.format("the total price with new year discount is $priceUsingChristmasCoupon")) | |
} | |
/** | |
* TShirt is an item used for purchase simulation | |
* */ | |
data class TShirt(val price: Int, val numberOfCloth:Int) | |
/** | |
* The Bill will be used to hold collection of t-shirts to be purchased | |
* */ | |
class Bill{ | |
private var collection = ArrayList<TShirt>() | |
fun add(tShirt: TShirt){ | |
collection.add(tShirt) | |
} | |
fun calculate(pricingMethod: Pricing): Int{ | |
val sum = collection.map { it.price * it.numberOfCloth }.sum() | |
return pricingMethod.calculate(sum) | |
} | |
} | |
interface Pricing{ | |
fun calculate(sum: Int): Int | |
} | |
/** | |
* The default price calculation without any discount | |
* */ | |
class DefaultCoupon : Pricing { | |
override fun calculate(sum: Int): Int { | |
return sum | |
} | |
} | |
/** | |
* The total price is calculated by give a discount IDR 10.000 if purchase more than IDR 100.000 | |
* and discount IDR 25.000 if purchase more than IDR 200.000 | |
* */ | |
class SpecialCoupon : Pricing{ | |
override fun calculate(sum: Int): Int { | |
var totalPrice = sum | |
if (sum in 100001..200000){ | |
totalPrice -= 10000 | |
} else if (totalPrice > 200000){ | |
totalPrice -= 25000 | |
} | |
return totalPrice | |
} | |
} | |
/** | |
* The total price is calculated by give a discount 25% sale | |
* */ | |
class NewYearCoupon : Pricing{ | |
override fun calculate(sum: Int): Int { | |
return sum*75/100 | |
} | |
} | |
/** | |
* The total price is calculated by give a discount IDR 50.000 | |
* when total prices is equals IDR 300.000 | |
* */ | |
class ChristmasCoupon : Pricing{ | |
override fun calculate(sum: Int): Int { | |
return if (sum == 300000) sum-50000 else sum | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment