dio-java icon indicating copy to clipboard operation
dio-java copied to clipboard

Análise de Números

Open 8rux40 opened this issue 3 years ago • 1 comments

Desafio

Você deve fazer a leitura de 5 valores inteiros. Em seguida mostre quantos valores informados são pares, quantos valores informados são ímpares, quantos valores informados são positivos e quantos valores informados são negativos.

Entrada

Você receberá 5 valores inteiros.

Saída

Exiba a mensagem conforme o exemplo de saída abaixo, sendo uma mensagem por linha e não esquecendo o final de linha após cada uma.

Exemplos de Entrada Exemplos de Saída
-5 3 valor(es) par(es)
0 2 valor(es) impar(es)
-3 1 valor(es) positivo(s)
-4 3 valor(es) negativo(s)
12

8rux40 avatar May 04 '21 18:05 8rux40

import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; import java.util.stream.IntStream;

public class AnaliseDeNumeros { public static final int NUMBER_OF_ENTRIES = 5; private final List<Integer> numbers; private int pair = 0, negative = 0, positive = 0;

public AnaliseDeNumeros(List<Integer> numbers) {
    this.numbers = numbers;
    analyzeNumbers();
}

public static void main(String[] args) {
    AnaliseDeNumeros numberAnalysis = readEntries();
    System.out.printf(
            "%d valor(es) par(es)\n%d valor(es) impar(es)\n%d valor(es) positivo(s)\n%d valor(es) negativo(s)",
            numberAnalysis.getPairs(),
            numberAnalysis.getOdd(),
            numberAnalysis.getPositives(),
            numberAnalysis.getNegatives()
    );
}

private static AnaliseDeNumeros readEntries() {
    Scanner scanner = new Scanner(System.in);
    return new AnaliseDeNumeros(IntStream.rangeClosed(1, NUMBER_OF_ENTRIES)
            .mapToObj(value -> scanner.nextInt())
            .collect(Collectors.toCollection(ArrayList::new)));
}

public int getOdd() {
    return NUMBER_OF_ENTRIES - this.pair;
}

public int getPositives() {
    return this.positive;
}

public int getNegatives() {
    return this.negative;
}

public int getPairs() {
    return this.pair;
}

private void analyzeNumbers() {
    this.numbers.forEach(number -> {
        this.pair += numberIsPair(number) ? 1 : 0;
        this.negative += numberIsNegative(number) ? 1 : 0;
        this.positive += numberIsPositive(number) ? 1 : 0;
    });
}

private static boolean numberIsPair(int number) {
    return number % 2 == 0;
}

private static boolean numberIsPositive(int number) {
    return number > 0;
}

private static boolean numberIsNegative(int number) {
    return number < 0;
}

}

alexrogeriodj avatar Nov 29 '21 14:11 alexrogeriodj