欠損数値の検出
コーヒーショップの売上データに、販売数量や金額などの欠損値がないかを確認します。サンプルの売上データを使って、null チェックを行いましょう。
Objects や Optional など、必要なパッケージはあらかじめインポートされています。
この演習はコースの一部です
Java によるデータクリーニング
演習の手順
- 販売数量が null かどうかを確認してください。
- 販売金額が null かどうかを確認してください。
- 販売数量が null の場合、デフォルト値として 1 を設定してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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);
}
}