1
resposta

Desafio3?

/*Foi desafiador realizar essa atividade, porem consegui, mas com ajuda de pesquisa e verificando código já pronto 
pude realizar a atividade e gostei do desafio.*/

//=============================
package desafiogson.parte1.trycatch;

import java.util.Scanner;

public class Desafio3Acesso {


    public static void Acesso() {
        Scanner sc = new Scanner(System.in);
        String senha = "rafa1234";

        System.out.print("Informe a senha: ");
        String senhaDigitada = sc.nextLine();

        while (!senhaDigitada.equals(senha)) {
            System.out.println("Senha incorreta. Tente novamente.");
            System.out.print("Informe a senha: ");
            senhaDigitada = sc.nextLine();
        }

        System.out.println("Acesso permitido!!!");


    }


}
//===================================================
package desafiogson.parte1.trycatch;

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 Desafio3Class extends Desafio3Acesso {

    public static void verifica(){
        Scanner sc = new Scanner(System.in);
        Acesso();
        System.out.println("================================================================");
        System.out.println("Informe o nome do usuario do Github : ");
        String nome = sc.nextLine().trim();
        String u = "https://api.github.com/users/" + nome;
        try {
            HttpClient usuario = HttpClient.newHttpClient();
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(u))
                    .header("Accept", "application/vnd.github.v3+json")
                    .build();
            HttpResponse<String> response = usuario
                    .send(request, HttpResponse.BodyHandlers.ofString());
            if (response.statusCode() == 404){
                throw new Desafio3("Usuario não llocalizado ..");
            }
            String json = response.body();
            System.out.println(json);
        }catch (IOException | InterruptedException e){
            System.out.println("Ops !!\nHouve um erro durante a consulta a API do Github..");
            e.printStackTrace();
        }catch (Desafio3 ex){
            System.out.println(ex.getMessage());
        }finally {
            System.out.println("Obrigado ate a proxima!!");
            sc.close();
        }

    }
}
//===================================

package desafiogson.parte1.trycatch;

public class Desafio3Main extends Desafio3Class{
    public static void main(String[] args) {
        verifica();
    }
}
//===============Resultado===========================


Informe a senha: rafa1234
Acesso permitido!!!
================================================================
Informe o nome do usuario do Github : 
Github
{"login":"github","id":9919,"node_id":"MDEyOk9yZ2FuaXphdGlvbjk5MTk=","avatar_url":"https://avatars.githubusercontent.com/u/9919?v=4","gravatar_id":"","url":"https://api.github.com/users/github","html_url":"https://github.com/github","followers_url":"https://api.github.com/users/github/followers","following_url":"https://api.github.com/users/github/following{/other_user}","gists_url":"https://api.github.com/users/github/gists{/gist_id}","starred_url":"https://api.github.com/users/github/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/github/subscriptions","organizations_url":"https://api.github.com/users/github/orgs","repos_url":"https://api.github.com/users/github/repos","events_url":"https://api.github.com/users/github/events{/privacy}","received_events_url":"https://api.github.com/users/github/received_events","type":"Organization","site_admin":false,"name":"GitHub","company":null,"blog":"https://github.com/about","location":"San Francisco, CA","email":null,"hireable":null,"bio":"How people build software.","twitter_username":null,"public_repos":490,"public_gists":0,"followers":36532,"following":0,"created_at":"2008-05-11T04:37:31Z","updated_at":"2022-11-29T19:44:55Z"}
Obrigado ate a proxima!!

Process finished with exit code 0


1 resposta

Oi Rafael, tudo bem?

Seu código tá bem estruturado e funcional. Você tá lidando adequadamente com exceções e interagindo bem com o usuário.

Muito obrigada por compartilhar com a gente!

Um abraço.