Comece agoraComece grátis

Detecting missing numbers

You want to check if sales from your coffee shop are missing any values, like the sales quantity and amount. Validate a sample sale by checking for nulls.

The necessary packages such as Objects, and Optional have been imported for you.

Este exercicio faz parte do curso

Cleaning Data in Java

Ver curso

Instruções do exercicio

  • Check if the sale quantity is null.
  • Check if the sale amount is null.
  • Set a default value of 1 for sale quantity if it is null.

exercicio interativo prático

Tente este exercicio completando este código de exemplo.

public class CoffeeSalesExample {
    private record CoffeeSales(LocalDate date, String coffeeName,
            String paymentMethod, Integer quantity, Double amount) {
    }

    public static void main(String[] args) {
		CoffeeSales sale = new CoffeeSales(LocalDate.of(2024, 3, 1), "Latte", "cash", null, null);

		// Check if the sale quantity is null
        if (Objects.____(sale.____())) {
        	System.out.println("Missing quantity");
        }
    	
        // Check if the sale amount is null
        if (Objects.____(sale.____())) {
        	System.out.println("Missing amount");
        }
    	
        // Set a default value of 1 for sale quantity
        int defaultQuantity = Optional.____(sale.quantity()).____(1);
        System.out.println("Default quantity: " + defaultQuantity);
    }
}
Editar e Executar Código