-
Notifications
You must be signed in to change notification settings - Fork 0
Exercise 2 - Json, Polymorphism, List #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: exercise-2-poli
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,65 @@ | ||
| package org.example | ||
| import com.google.gson.Gson | ||
| import com.google.gson.reflect.TypeToken | ||
| import java.io.File | ||
|
|
||
|
|
||
| val gson = Gson() | ||
|
|
||
| open class CommercialPlace( | ||
| val name: String, | ||
| val address: String, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Address não é um objeto? |
||
| val rating: Double | ||
| ) | ||
| class CoffeeShop( | ||
| name: String, | ||
| address: String, | ||
| rating: Double | ||
| ) : CommercialPlace(name, address, rating) | ||
|
|
||
| class Restaurants( | ||
| name: String, | ||
| address: String, | ||
| rating: Double, | ||
| val itinerary: List<Itinerary> | ||
| ) : CommercialPlace(name, address, rating) | ||
| data class Address( | ||
| val street: String, | ||
| val number: String, | ||
| val cep: String, | ||
| val city: String, | ||
| val state: String, | ||
| val country: String | ||
| ) | ||
|
|
||
| data class Itinerary( | ||
| val day: String, | ||
| val startTime: String, | ||
| val endTime: String | ||
| ) | ||
| 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() | ||
| } | ||
|
Comment on lines
+40
to
+48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| fun main() { | ||
| println("Hello World!") | ||
| val jsonFile = File("src\\main\\kotlin\\dataRestaurants.json") | ||
| 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() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| prettyPrint(commercialPlace) | ||
| } | ||
|
|
||
| /** | ||
| * Criar uma classe Kotlin representando um objeto restaurante | ||
| * O restaurante contem os seguintes campos | ||
| * Nome | ||
| * Endreço -> é um objecto com os seguintes campos (Rua: Texto, Numero: Texto, CEP: Texto, Cidade : Texto, Estado : Texto, Pais: Texto) | ||
| * Entinerário: É um objeto que vai ter uma lista de working hours | ||
| * Working hours é um objeto com os campos (Dia: Day, Horario Início: String, Horário Fim: String) .. Sobre o dia/ Day, tenta fazer o melhor, mas vamo achar a melhor maneira juntos se precisar | ||
| * Rating (Avaliação) É um campo double de 0 a 5 estrelas | ||
| * | ||
| * NÃO VAMOS INSTANCIAR ESSE OBJETO RESTAURANTE, ou seja, não vamos construir ele na mão, vamos criar um arquivo JSON | ||
| * Esse arquivo vai representar o objeto Restaurante com todos os valores definidos | ||
| * | ||
| * Então eis aqui a tarefa... | ||
| * Ler o arquivo .json | ||
| * Chamar o parser de jSOn para criar o objeto restaurante a partir do arquivo json | ||
| * Imprimir o objeto restaurante -> print(restaurante) | ||
| * | ||
| * O parser de JSON a ser usado é o GSON, sua dependencia deve ser adicionada no arquivo build.gradle.kts... aprenda :sunglass: | ||
| */ | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| [ | ||
| { | ||
| "name": "Ah!Butequeria", | ||
| "rating": 4.9, | ||
| "address": { | ||
| "street": "Rua Paulo Frontim", | ||
| "number": "240", | ||
| "cep": "30430012", | ||
| "city": "Curvelo", | ||
| "state": "Minas Gerais", | ||
| "country": "Brazil" | ||
| }, | ||
| "itinerary": [ | ||
| { | ||
| "day": "Monday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Tuesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Wednesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Thursday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Friday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| } | ||
| ] | ||
| }, | ||
|
|
||
| { | ||
| "name": "Emporio Donna Thê", | ||
| "rating": 5.0, | ||
| "address": { | ||
| "street": "Praça José Júlio Mascarenhas", | ||
| "number": "55", | ||
| "cep": "30430012", | ||
| "city": "Curvelo", | ||
| "state": "Minas Gerais", | ||
| "country": "Brazil" | ||
| }, | ||
| "itinerary": [ | ||
| { | ||
| "day": "Monday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Tuesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Wednesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Thursday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Friday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "name": "Canela Amarela", | ||
| "rating": 4.7, | ||
| "address": { | ||
| "street": "Praça Leonardo Gutierrez", | ||
| "number": "129", | ||
| "cep": "30430012", | ||
| "city": "Belo Horizonte", | ||
| "state": "Minas Gerais", | ||
| "country": "Brazil" | ||
| }, | ||
| "itinerary": [ | ||
| { | ||
| "day": "Monday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Tuesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Wednesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Thursday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Friday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "name": "EdCoffee", | ||
| "rating": 5.0, | ||
| "address": { | ||
| "street": "Rua Dante", | ||
| "number": "420", | ||
| "cep": "30430012", | ||
| "city": "Belo Horizonte", | ||
| "state": "Minas Gerais", | ||
| "country": "Brazil" | ||
| }, | ||
| "itinerary": [ | ||
| { | ||
| "day": "Monday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Tuesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Wednesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Thursday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Friday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "name": "Cafeteria dúViton", | ||
| "rating": 4.9, | ||
| "address": { | ||
| "street": "Rua Estácio de Sá", | ||
| "number": "196", | ||
| "cep": "30430012", | ||
| "city": "Belo Horizonte", | ||
| "state": "Minas Gerais", | ||
| "country": "Brazil" | ||
| }, | ||
| "itinerary": [ | ||
| { | ||
| "day": "Monday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Tuesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Wednesday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Thursday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| }, | ||
| { | ||
| "day": "Friday", | ||
| "startTime": "9:00 AM", | ||
| "endTime": "6:00 PM" | ||
| } | ||
| ] | ||
| } | ||
| ] |
There was a problem hiding this comment.
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