Adnotacje dla danych sprzedaży gier wideo
Chcesz zwalidować dane sprzedaży gier wideo, aby wykryć ewentualne błędy. Przykładowe dane przedstawiono poniżej. Skonfiguruj atrybuty klasy VideoGameSale za pomocą adnotacji, które sprawdzą, czy pola nie są puste, oraz czy spełniają określone warunki.
| name | platform | year | sales |
|---|---|---|---|
| Wii Sports | Wii | 2006 | 41.49 |
| Super Mario Bros. | NES | 1985 | 29.08 |
| Mario Kart Wii | Wii | 2008 | 15.85 |
Pakiet jakarta.validation.constraints został już zaimportowany.
To ćwiczenie jest częścią kursu
Czyszczenie danych w Javie
Instrukcje do ćwiczenia
- Dodaj adnotacje sprawdzające, czy pola
nameiplatformnie są puste. - Sprawdź, czy
yearwynosi co najmniej 1960. - Zweryfikuj, czy
salesjest wartością dodatnią.
Interaktywne ćwiczenie praktyczne
Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.
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!");
}
}