Created
June 6, 2018 12:35
-
-
Save Noy/ad0c7a9f402e8a7e99ebc044d322462f 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 mongo | |
data class Address(var city: String, var country: String) | |
data class Review(var userName: String, var rating: Double) { | |
init { | |
if ((rating < 0) || rating >10) { | |
throw RatingException("Ratings need to be between 0 and 10") | |
} | |
} | |
} | |
data class RatingException(override val message: String) : Throwable() | |
data class Hotel(var id: Int, var name: String, var price: Int, var address: Address, var reviews: List<Review>) | |
interface HotelRepo { | |
fun findHotelById(id: Int) : Hotel? | |
fun findHotelByPricePerNightLessThan(price: Int) : List<Hotel> | |
fun findHotelByCity(city: String) : List<Hotel> | |
} | |
class Data(private val hotel: List<Hotel>) : HotelRepo { | |
override fun findHotelById(id: Int): Hotel? { | |
hotel.forEach { h -> | |
if (h.id == id) { | |
return h | |
} | |
} | |
return null | |
} | |
override fun findHotelByPricePerNightLessThan(price: Int): List<Hotel> { | |
val hotels: MutableList<Hotel> = mutableListOf() | |
hotel.forEach { h -> | |
if (h.price <= price) { | |
hotels.add(h) | |
} | |
} | |
return hotels | |
} | |
override fun findHotelByCity(city: String): List<Hotel> { | |
val hotels: MutableList<Hotel> = mutableListOf() | |
hotel.forEach { h -> | |
if (h.address.city == city) { | |
hotels.add(h) | |
} | |
} | |
return hotels | |
} | |
} | |
fun main(args: Array<String>) { | |
val hilton = Hotel(1, "Hilton", 200, | |
Address("London", "UK"), | |
arrayListOf(Review("jason", 82.2))) | |
val marriott = Hotel(2, "Marriott", 100, | |
Address("Jacksonville", "US"), | |
arrayListOf(Review("monroe", 9.5))) | |
val ramada = Hotel(3, "Ramada", 90, | |
Address("London", "UK"), | |
arrayListOf(Review("farmer john", 9.5))) | |
val allHotels: List<Hotel> = listOf(hilton, marriott, ramada) | |
val data = Data(allHotels) | |
println(data.findHotelByCity("Jacksonville")) | |
data.findHotelByPricePerNightLessThan(100) | |
.forEach { | |
println("Hotel: ${it.name} (in ${it.address.city}) costs $${it.price}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment