Last active
November 15, 2022 04:46
-
-
Save djailtim/d94fc987b4b8cd42ced5c29c4e2286c3 to your computer and use it in GitHub Desktop.
Exercícios Aula 03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| public class Question1 { | |
| public static void main(String[] args) throws IOException { | |
| BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); | |
| String age; | |
| System.out.println("**********\tSistema Verificador da Idade do Eleitor\t**********"); | |
| try { | |
| System.out.print("=> Informe sua idade: "); | |
| age = input.readLine(); | |
| verifyYouAreAVoter(Integer.parseInt(age)); | |
| } catch (NumberFormatException error) { | |
| do { | |
| System.out.print("Erro, digite uma idade válida: "); | |
| age = input.readLine(); | |
| } while (!isNumber(age)); | |
| verifyYouAreAVoter(Integer.parseInt(age)); | |
| } catch (Exception error) { | |
| System.out.println("Erro. Finalizando aplicação! - " + error); | |
| } | |
| } | |
| public static void verifyYouAreAVoter (int age) { | |
| if (age >= 18 && age <= 70 ) { | |
| System.out.println("Voto obrigatório!"); | |
| } else if (age >= 16) { | |
| System.out.println("Voto facultativo!"); | |
| } else { | |
| System.out.println("Sem direito a votar!"); | |
| } | |
| } | |
| public static boolean isNumber(String number) { | |
| return number != null && number.matches("[0-9]*"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.util.Scanner; | |
| public class Question2 { | |
| public static void main(String[] args) throws IOException { | |
| Scanner input = new Scanner(System.in); | |
| String age; | |
| System.out.println("**********\tSistema Verificador da Idade do Eleitor\t**********"); | |
| try { | |
| System.out.print("=> Informe sua idade: "); | |
| age = input.nextLine(); | |
| System.out.println(verifyYouAreAVoter(Integer.parseInt(age))); | |
| } catch (NumberFormatException error) { | |
| do { | |
| System.out.print("Erro, digite uma idade válida: "); | |
| age = input.nextLine(); | |
| } while (!isNumber(age)); | |
| System.out.println(verifyYouAreAVoter(Integer.parseInt(age))); | |
| } catch (Exception error) { | |
| System.out.println("Erro. Finalizando aplicação! - "+ error); | |
| } | |
| } | |
| public static String verifyYouAreAVoter (int age) { | |
| return (age >= 18 && age <= 70 ) ? "Voto obrigatório!" : | |
| (age >= 16) ? "Voto facultativo!" : | |
| "Sem direito a votar!"; | |
| } | |
| public static boolean isNumber(String number) { | |
| return number != null && number.matches("[0-9]*"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class Question3 { | |
| static Drink[] drinks; | |
| public static void main(String[] args) { | |
| Scanner input = new Scanner(System.in); | |
| String choose; | |
| boolean newDrink = true; | |
| while (newDrink) { | |
| System.out.println("**********\tMáquina de Bebidas\t**********"); | |
| System.out.println("==>\tLista de Bebidas\t<=="); | |
| Question3.listOfDrinks(); | |
| try { | |
| System.out.print("\n==>\tEscolha uma bebida (digite número): "); | |
| choose = input.next(); | |
| Question3.chooseDrink(Integer.parseInt(choose)); | |
| } catch (NumberFormatException error) { | |
| do { | |
| System.out.print("Erro, digite um número válido: "); | |
| choose = input.next(); | |
| } while (!isNumber(choose)); | |
| Question3.chooseDrink(Integer.parseInt(choose)); | |
| } catch (Exception error) { | |
| System.out.println("Erro. Finalizando aplicação! - " + error); | |
| } | |
| newDrink = isNewDrink(newDrink); | |
| }; | |
| System.out.println("\n*****\tObrigado por utilizar nosso sistema!\t*****"); | |
| } | |
| private static boolean isNewDrink(boolean newDrink) { | |
| Scanner input = new Scanner(System.in); | |
| String newChoose; | |
| System.out.println("\n==>\tDeseja escolher outro produto? (S: Sim - N: Não)\t<=="); | |
| newChoose = input.next(); | |
| while (!newChoose.equalsIgnoreCase("s") && !newChoose.equalsIgnoreCase("n")) { | |
| System.out.println("==>\tOpção inválida\t<=="); | |
| System.out.println("\n==>\tDeseja escolher outro produto? (S: Sim - N: Não)\t<=="); | |
| newChoose = input.next(); | |
| } | |
| if (newChoose.equalsIgnoreCase("n")) newDrink = false; | |
| return newDrink; | |
| } | |
| public static void listOfDrinks() { | |
| drinks = new Drink[] { | |
| new Drink("Coca-Cola", 5.0), | |
| new Drink("Coca-Cola 0", 4.5), | |
| new Drink("Pepsi", 4.4), | |
| new Drink("Guaraná Antarctica", 3.5), | |
| new Drink("Fanta Laranja", 4.23), | |
| new Drink("Água", 2.5) | |
| }; | |
| for (int i = 0; i < drinks.length; i++) { | |
| System.out.println(drinks[i].toString(i+1)); | |
| } | |
| } | |
| public static void chooseDrink(int choose) { | |
| switch (choose) { | |
| case 1 -> System.out.println(drinks[0].toString(1)); | |
| case 2 -> System.out.println(drinks[1].toString(2)); | |
| case 3 -> System.out.println(drinks[2].toString(3)); | |
| case 4 -> System.out.println(drinks[3].toString(4)); | |
| case 5 -> System.out.println(drinks[4].toString(5)); | |
| case 6 -> System.out.println(drinks[5].toString(6)); | |
| default -> System.out.println("Opção inválida!"); | |
| } | |
| } | |
| public static boolean isNumber(String number) { | |
| return number != null && number.matches("[0-9]*"); | |
| } | |
| } | |
| class Drink { | |
| private String name; | |
| private Double price; | |
| public Drink(String name, Double price) { | |
| this.name = name; | |
| this.price = price; | |
| } | |
| public String toString(int index) { | |
| return index + " - " + name + " - R$ " + String.format("%.2f", price); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class Question4 { | |
| static SoftDrink[] drinks; | |
| public static void main(String[] args) { | |
| Scanner input = new Scanner(System.in); | |
| String choose; | |
| boolean newDrink = true; | |
| while (newDrink) { | |
| System.out.println("**********\tMáquina de Bebidas\t**********"); | |
| System.out.println("==>\tLista de Bebidas\t<=="); | |
| Question4.listOfDrinks(); | |
| try { | |
| System.out.print("\n==>\tEscolha uma bebida (digite número): "); | |
| choose = input.next(); | |
| Question4.chooseDrink(Integer.parseInt(choose)); | |
| } catch (NumberFormatException error) { | |
| do { | |
| System.out.print("Erro, digite um número válido: "); | |
| choose = input.next(); | |
| } while (!isNumber(choose)); | |
| Question4.chooseDrink(Integer.parseInt(choose)); | |
| } catch (Exception error) { | |
| System.out.println("Erro. Finalizando aplicação! - " + error); | |
| } | |
| newDrink = isNewDrink(newDrink); | |
| }; | |
| System.out.println("\n*****\tObrigado por utilizar nosso sistema!\t*****"); | |
| } | |
| private static boolean isNewDrink(boolean newDrink) { | |
| Scanner input = new Scanner(System.in); | |
| String newChoose; | |
| System.out.println("\n==>\tDeseja escolher outro produto? (S: Sim - N: Não)\t<=="); | |
| newChoose = input.next(); | |
| while (!newChoose.equalsIgnoreCase("s") && !newChoose.equalsIgnoreCase("n")) { | |
| System.out.println("==>\tOpção inválida\t<=="); | |
| System.out.println("\n==>\tDeseja escolher outro produto? (S: Sim - N: Não)\t<=="); | |
| newChoose = input.next(); | |
| } | |
| if (newChoose.equalsIgnoreCase("n")) newDrink = false; | |
| return newDrink; | |
| } | |
| public static void listOfDrinks() { | |
| drinks = new SoftDrink[] { | |
| new SoftDrink("Coca-Cola", 5.0), | |
| new SoftDrink("Coca-Cola 0", 4.5), | |
| new SoftDrink("Pepsi", 4.4), | |
| new SoftDrink("Guaraná Antarctica", 3.5), | |
| new SoftDrink("Fanta Laranja", 4.23), | |
| new SoftDrink("Água", 2.5) | |
| }; | |
| for (int i = 0; i < drinks.length; i++) { | |
| System.out.println(drinks[i].toString(i+1)); | |
| } | |
| } | |
| public static void chooseDrink(int choose) { | |
| if (choose == 1) System.out.println(drinks[0].toString(1)); | |
| else if (choose == 2) System.out.println(drinks[1].toString(2)); | |
| else if (choose == 3) System.out.println(drinks[2].toString(3)); | |
| else if (choose == 4) System.out.println(drinks[3].toString(4)); | |
| else if (choose == 5) System.out.println(drinks[4].toString(5)); | |
| else if (choose == 6) System.out.println(drinks[5].toString(6)); | |
| else System.out.println("Opção inválida!"); | |
| } | |
| public static boolean isNumber(String number) { | |
| return number != null && number.matches("[0-9]*"); | |
| } | |
| } | |
| class SoftDrink { | |
| private String name; | |
| private Double price; | |
| public SoftDrink(String name, Double price) { | |
| this.name = name; | |
| this.price = price; | |
| } | |
| public String toString(int index) { | |
| return index + " - " + name + " - R$ " + String.format("%.2f", price); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment