Annotation với doanh số trò chơi điện tử
Bạn muốn xác thực dữ liệu doanh số trò chơi điện tử để phát hiện lỗi. Một số dữ liệu mẫu được hiển thị bên dưới. Hãy thiết lập các thuộc tính của lớp VideoGameSale với các annotation để kiểm tra trường trống và đảm bảo các trường đáp ứng các giá trị nhất định.
| name | platform | year | sales |
|---|---|---|---|
| Wii Sports | Wii | 2006 | 41.49 |
| Super Mario Bros. | NES | 1985 | 29.08 |
| Mario Kart Wii | Wii | 2008 | 15.85 |
Gói jakarta.validation.constraints đã được nhập sẵn cho bạn.
Bài tập này là một phần của khóa học
Làm sạch dữ liệu bằng Java
Hướng dẫn bài tập
- Thêm annotation để kiểm tra
namevàplatformkhông rỗng. - Kiểm tra
yeartối thiểu là 1960. - Xác minh
saleslà số dương.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
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!");
}
}