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.
This exercise is part of the course
Cleaning Data in Java
Exercise instructions
- 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.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import java.time.LocalDate;
import java.util.Objects;
import java.util.Optional;
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);
}
}