Skip to content

Exercise 2 - Json, Polymorphism, List#3

Open
VitorlFigueiredo wants to merge 3 commits intoexercise-2-polifrom
vitaoDaMassa
Open

Exercise 2 - Json, Polymorphism, List#3
VitorlFigueiredo wants to merge 3 commits intoexercise-2-polifrom
vitaoDaMassa

Conversation

@VitorlFigueiredo
Copy link
Copy Markdown

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.
Comment thread src/main/kotlin/Main.kt

open class CommercialPlace(
val name: String,
val address: String,
Copy link
Copy Markdown
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?

Comment thread src/main/kotlin/Main.kt

val gson = Gson()

open class CommercialPlace(
Copy link
Copy Markdown
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 thread src/main/kotlin/Main.kt
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
Copy Markdown
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

Comment thread src/main/kotlin/Main.kt
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
Copy Markdown
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.

2 participants