Skip to content

Conversation

@VitorlFigueiredo
Copy link

Customized function to receive Restaurants and Cafeterias and print them, using the concept of polymorphism.

Vitor Lopes and others added 3 commits February 29, 2024 01:27
…re as classes Restaurants e CoffeeShop, usando o conceito de polimorfismo para resolver o problema. Criei também uma função prettyPrint que verifica qual classe é, e exibi na tela conforme pedido.

open class CommercialPlace(
val name: String,
val address: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address não é um objeto?


val gson = Gson()

open class CommercialPlace(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pq não usou uma Interface ou abstract Class? Dá uma estudada nisso

Comment on lines +40 to +48
fun prettyPrint(commercialPlace: List<CommercialPlace>) {
commercialPlace.forEach { commercialPlace ->
when (commercialPlace) {
is CoffeeShop -> println("Name: ${commercialPlace.name}, Rating: ${commercialPlace.rating}, Address: ${commercialPlace.address}")
is Restaurants -> println("Name: ${commercialPlace.name}, Address: ${commercialPlace.address} Rating: ${commercialPlace.rating},")
}

println()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essa função faz parte de alguma classe? Não... isso não é assim, estuda mais polimorfismo

val json = jsonFile.readText()
//val listType = object : TypeToken<List<CommercialPlace>>() {}.type
//val commercialPlace: List<CommercialPlace> = gson.fromJson(json, listType)
val commercialPlace: List<CommercialPlace> = Gson().fromJson(json, Array<CommercialPlace>::class.java).toList()
Copy link
Contributor

@roubertedgar roubertedgar Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Como que vc sabe que um ComercialPlace é café ou restaurante no Json? Não estou vendo nada no jSon me contando isso.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants