Giovanni Rozza
Giovanni Rozza26/03/2023 20:35
Compartilhe

Minhas soluções desafio final bootcamp Banco Pan

  • #Spring Boot / Spring Framework
  • #Java

Boa noite pessoal, quero compartilhar aqui minhas soluções para os desafios finais do bootcamp Banco Pan, procurei aplicar sempre que possível o uso de Collections (usei bastante o HashMap), e ficar de olho no tempo de execução (evitar for next dentro de for next).


 // ********** ANIMAL *************
 Scanner sc = new Scanner(System.in);
 Map<String, String> lista = new HashMap<String, String>();
 lista.put("vertebrado.ave.carnivoro", "aguia");
 lista.put("vertebrado.ave.onivoro", "pomba");
 lista.put("vertebrado.mamifero.onivoro", "homem");
 lista.put("vertebrado.mamifero.herbivoro", "vaca");
 lista.put("invertebrado.inseto.hematofago", "pulga");
 lista.put("invertebrado.inseto.herbivoro", "lagarta");
 lista.put("invertebrado.anelideo.hematofago", "sanguessuga");
 lista.put("invertebrado.anelideo.onivoro", "minhoca");
 String AN1, AN2, AN3;
 AN1 = sc.nextLine();
 AN2 = sc.nextLine();
 AN3 = sc.nextLine();
 String key = AN1.trim().toLowerCase() + '.' + AN2.trim().toLowerCase() + '.' + AN3.trim().toLowerCase();
 System.out.println(lista.get(key));
 sc.close();


 // ********** QUITANDA DO SEU ZÉ *************
 Scanner input = new Scanner(System.in);
 int morangos = input.nextInt();
 int macas = input.nextInt();
 input.close();
 Double custoTotal = morangos * (morangos > 5 ? 2.2 : 2.5) + macas * (macas > 5 ? 1.5 : 1.8);
 System.out.println(morangos + macas > 8 || custoTotal > 25.00 ? custoTotal * 0.9 : custoTotal);


 // ********** DESAFIO TRIANGULO ********** 
 Scanner leitor = new Scanner(System.in);
 double A = leitor.nextDouble();
 double B = leitor.nextDouble();
 double C = leitor.nextDouble();
 double maior;
 double soma;
 boolean triangulo;

 // Só irá existir um triângulo se, somente se, os seus lados obedeceram à
 // seguinte regra: um de seus lados deve ser maior que o valor absoluto (módulo)
 // da diferença dos outros dois lados e menor que a soma dos outros dois lados.
 if (A > 0 && B > 0 && C > 0)
 {
 Double array[] = new Double[] { A, B, C };
 Arrays.sort(array);
 if (array[0] > Math.abs(array[1] - array[2]) && array[0] < array[1] + array[2])
 // triangulo
 {
 System.out.printf("Perimetro = %.1f", A + B + C);
 } else
 {
 System.out.printf("Area = %.1f", (A + B) * C / 2);
 }
 }
 leitor.close();


 // ********** DESAFIO ESPACOS EM BRANCO E VOGAIS ********** 
 Scanner sc = new Scanner(System.in);
 String str = sc.nextLine();
 String[] strSplit = str.split(" ");
 // char[] arrVogais = { 'a', 'e', 'i', 'o', 'u' };
 int espacosEmBranco = strSplit.length - 1, quantVogais = 0;
 Map<String, Integer> mapVogais = new HashMap<String, Integer>();
 mapVogais.put(String.valueOf('a'), 0);
 mapVogais.put(String.valueOf('e'), 0);
 mapVogais.put(String.valueOf('i'), 0);
 mapVogais.put(String.valueOf('o'), 0);
 mapVogais.put(String.valueOf('u'), 0);
 for (String item : strSplit)
 {
 item = item.toLowerCase();
 for (char ch : item.toCharArray())
 {
 if (mapVogais.containsKey(String.valueOf(ch)))
 {
 mapVogais.put(String.valueOf(ch), mapVogais.get(String.valueOf(ch)) + 1);
 }
 }
 }
 quantVogais = mapVogais.get(String.valueOf("a")) + mapVogais.get(String.valueOf("e"))
 + mapVogais.get(String.valueOf("i")) + mapVogais.get(String.valueOf("o"))
 + mapVogais.get(String.valueOf("u"));
 System.out.println("Espacos em branco: " + espacosEmBranco + " Vogais: " + quantVogais);
 sc.close();


 // ********** IMPOSTO DE RENDA ********** 
 Scanner leitor = new Scanner(System.in);
 double renda = leitor.nextDouble();
 double imposto = 0;
 if (renda > 4500.0)
 {
 imposto = (3000.0 - 2000.01) * 0.08 + (4500.0 - 3000.01) * 0.18 + (renda - 4500.0) * 0.28;
 } else if (renda > 3000.0)
 {
 imposto = (3000.0 - 2000.01) * 0.08 + (renda - 3000.01) * 0.18;
 } else if (renda > 2000.0)
 {
 imposto = (renda - 2000.01) * 0.08;
 }
 if (imposto == 0)
 {
 System.out.printf("Isento");
 } else
 {
 System.out.printf("R$ %.2f", imposto);
 }
 leitor.close();
  }
Compartilhe
Comentários (2)
Giovanni Rozza
Giovanni Rozza - 27/04/2023 15:44

mas vc está assumindo que os lados estão já ordenados por tamanho, eu não assumi essa premissa.

UM

Ulisses Melo - 09/04/2023 17:45

O Desafio do Triângulo era bem mais simples, tipo


boolean  triangulo = (A + B > C) && (A + C > B) && (B + C > A);  


 if (triangulo){

   soma = A + B + C;

   System.out.printf( "Perimetro = " + "%.1f", soma);

  

  } else{

   soma = ((A + B) * C)/2;

   System.out.printf( "Area = " + "%.1f", soma);

  }


mas está valendo, bom ver a ideia de outros devs