Walidacja zakresów dat
Zakresy liczbowe ilości sprzedanych gier wideo masz już zwalidowane. Teraz czas sprawdzić zakresy dat premiery gier. Daty powinny mieścić się między 1.01.1985 a 31.12.2024. Poniżej znajdują się przykładowe daty wyodrębnione ze zbioru danych. Skonfiguruj sprawdzanie zakresów dat dla tych danych.
| Gra | Platforma | Data premiery | Ilość (w milionach sztuk) |
|---|---|---|---|
| Wii Sports | Wii | 19.11.2006 | 41,49 |
| Super Mario Bros. | NES | 13.11.1985 | 29,08 |
| Mario Kart Wii | Wii | 10.04.2008 | 15,85 |
Niezbędne pakiety, takie jak LocalDate i DateTimeFormatter, zostały już zaimportowane.
To ćwiczenie jest częścią kursu
Czyszczenie danych w Javie
Instrukcje do ćwiczenia
- Sprawdź, czy data premiery przypada po
startDate. - Sprawdź, czy data premiery przypada przed
endDate.
Interaktywne ćwiczenie praktyczne
Spróbuj tego ćwiczenia, uzupełniając ten przykładowy kod.
public class RangeValidation {
public static void main(String[] args) {
List releaseDates = Arrays.asList("11/19/2006", "11/13/1985", "4/10/2008");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/yyyy");
LocalDate startDate = LocalDate.of(1985, 1, 1);
LocalDate endDate = LocalDate.of(2024, 12, 31);
for (String dateStr : releaseDates) {
LocalDate releaseDate = LocalDate.parse(dateStr, formatter);
// Check that the release date occurs after startDate
boolean isValid = ____.____(startDate)
// Verify that the release date is before endDate
&& ____.____(endDate);
System.out.println(dateStr + " is valid: " + isValid);
}
}
}