Rilevare numeri mancanti
Vuoi verificare se nelle vendite della tua caffetteria mancano dei valori, come la quantità e l'importo. Valida una vendita di esempio controllando i null.
I pacchetti necessari, come Objects e Optional, sono già stati importati per te.
Questo esercizio fa parte del corso
Pulizia dei dati in Java
Istruzioni dell'esercizio
- Verifica se la quantità della vendita è null.
- Verifica se l'importo della vendita è null.
- Imposta un valore predefinito pari a 1 per la quantità della vendita se è null.
esercizio interattivo pratico
Prova questo esercizio completando questo codice di esempio.
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);
}
}