MulaiMulai sekarang secara 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.

Latihan ini adalah bagian dari kursus

Cleaning Data in Java

Lihat Kursus

Petunjuk latihan

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

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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);
    }
}
Edit dan Jalankan Kode