用註解驗證電玩遊戲銷售資料
你想要驗證電玩遊戲的銷售資料,以找出資料中的錯誤。以下是一些範例資料。請在 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 進行資料清理
練習說明
- 加上註解檢查
name和platform不是空的。 - 檢查
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!");
}
}