package screenmatch.src;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
public class ExercicioBuscaAulaUm {
static Scanner leitura = new Scanner(System.in);
public static void main(String[] args) throws IOException, InterruptedException {
String menu = """
Escolha uma opção
1 - Pesquisar Livro
2 - Pesquisar cotação Criptomoeda
3- Consultar receita no meal.com
9- Sair
""";
int opcaoMenu = 0;
while (opcaoMenu != 9) {
System.out.println(menu);
opcaoMenu = leitura.nextInt();
switch (opcaoMenu) {
case 1:
pesquisarLivro();
break;
case 2:
pesquisarCotacao();
break;
case 3:
pesquisarReceitas();
break;
default:
System.out.println("Opção inválida!");
break;
}
}
}
public static void pesquisarLivro() throws IOException, InterruptedException {
String filtroBusca;
String endpoint;
System.out.println("Qual livro deseja pesquisar pesquisar?");
endpoint = "https://www.googleapis.com/books/v1/volumes?q=intitle:";
filtroBusca = leitura.next();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create( endpoint+filtroBusca))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
public static void pesquisarCotacao() throws IOException, InterruptedException {
System.out.println("Insert the name meal:");
String endpoint = "https://api.coingecko.com/api/v3/simple/price?";
System.out.println("Qual a Criptomoeda deseja pesquisar?");
String filtroCriptomoeda = leitura.next();
System.out.println("Qual tipo de moeda para verificar a cotação (BRL/USD/EUR)?");
String moedaCotacao = leitura.next();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpoint + "ids=" + filtroCriptomoeda + "&vs_currencies=" + moedaCotacao))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
public static void pesquisarReceitas() throws IOException, InterruptedException {
System.out.println("Insert the name meal:");
var filtroMeal = leitura.next();
String endpoint = "https://www.themealdb.com/api/json/v1/1/search.php?s=" + filtroMeal;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request;
request = HttpRequest.newBuilder()
.uri(URI.create(endpoint))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}