Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Last active December 27, 2020 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vamsitallapudi/7ce4c8ccf2d08d35b334c5682f2d406f to your computer and use it in GitHub Desktop.
Save vamsitallapudi/7ce4c8ccf2d08d35b334c5682f2d406f to your computer and use it in GitHub Desktop.
package main.leetcode.kotlin
class Mariott {
private val basePrice = 2000
private val tax = 500
fun getPrice(): Int {
return basePrice + tax
}
}
class Taj {
private val basePrice = 3000
fun getPrice(): Int {
return basePrice
}
}
class PriceFactory {
fun calculatePrices(hotels : List<Any>) : Int {
var price = 0
hotels.forEach {
price += when (it) {
is Mariott -> {
it.getPrice()
}
is Taj -> {
it.getPrice()
}
else -> {
throw RuntimeException("Hotel Not Listed.")
}
}
}
return price
}
}
fun main() {
print(PriceFactory().calculatePrices(listOf(Mariott(), Taj())))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment