始める無料で始める

アノテーションを使ったビデオゲームの売上検証

ビデオゲームの売上データにミスがないかを検証したいと思います。以下にサンプルデータを示します。VideoGameSale クラスの属性にアノテーションを設定して、空フィールドのチェックと各フィールドの値の検証を行いましょう。

name platform year sales
Wii Sports Wii 2006 41.49
Super Mario Bros. NES 1985 29.08
Mario Kart Wii Wii 2008 15.85

jakarta.validation.constraints パッケージはあらかじめインポートされています。

この演習はコースの一部です

Java によるデータクリーニング

コースを見る

演習の手順

  • nameplatform が空でないことを確認するアノテーションを追加してください。
  • year が 1960 以上であることを確認してください。
  • sales が正の値であることを検証してください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

public class VideoGameSale {
    // Check that name is not empty
    @____(message = "Name cannot be empty")
    private String name;

    // Check that platform is not empty
    @____(message = "Platform cannot be empty")
    private String platform;

    // Check that year is at least 1960
    ____(value = ____, message = "Year must be greater than 1960")
    private Integer year;

    // Check that sales is positive
    ____(value = ____, message = "Sales amount must be positive")
    private Double sales;
    
    public static void main(String[] args) {
        System.out.println("Nice job setting up the annotations!");
    }
}
コードを編集して実行