ComenzarEmpieza gratis

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 ejercicio forma parte del curso

Cleaning Data in Java

Ver curso

Instrucciones del ejercicio

  • 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.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

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 y ejecutar código