用注解验证电子游戏销量
您希望校验电子游戏销量数据,以发现数据中的错误。下面给出了一些示例数据。请为 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!");
}
}