Last active
December 27, 2020 12:46
-
-
Save vamsitallapudi/7ce4c8ccf2d08d35b334c5682f2d406f to your computer and use it in GitHub Desktop.
This file contains 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
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