वैलिडेशन संदेश दिखाना
आपने वीडियो गेम सेल्स डेटा पर क्वालिटी कंट्रोल के रूप में annotations सेट किए हैं. अब गलतियों वाले डेटा पर वैलिडेशन चलाकर देखिए कि कौन से संदेश प्रिंट होते हैं. खराब डेटा में platform गायब है, year 1960 से पहले है, और sales निगेटिव हैं, जो annotations में दिए गए constraints का उल्लंघन करते हैं. वीडियो गेम सेल्स डेटा को वैलिडेट कीजिए और कोई भी उल्लंघन संदेश प्रिंट कीजिए.
jakarta पैकेज आपके लिए इम्पोर्ट कर दिए गए हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Java में डेटा क्लीनिंग
अभ्यास निर्देश
- जब डेटा किसी constraint का उल्लंघन करे, तो उल्लंघन संदेश प्रिंट करें.
validatorलागू करके वीडियो गेम सेल को वैलिडेट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
public class VideoGameSale {
@NotNull(message = "Name cannot be empty")
private String name;
@NotNull(message = "Platform cannot be empty")
private String platform;
@Min(value = 1960, message = "Year must be greater than 1960")
private Integer year;
@Min(value = 0, message = "Sales amount must be positive")
private Double sales;
public VideoGameSale(String name, String platform, Integer year, Double sales) {
this.name = name;
this.platform = platform;
this.year = year;
this.sales = sales;
}
public static void main(String[] args) {
VideoGameSale sale = new VideoGameSale(
"Super Mario Bros.", null, 1950,
-29.08);
Set> violations =
SalesValidator.validateSale(sale);
// Print the violation message
violations.forEach(violation ->
System.out.println(____.____()));
if (!violations.isEmpty()) throw new ConstraintViolationException(violations);
}
}
class SalesValidator {
private static final ValidatorFactory factory =
Validation.buildDefaultValidatorFactory();
private static final Validator validator = factory.getValidator();
public static Set> validateSale(VideoGameSale sale) {
// Validate the video game sale
return ____.____(sale);
}
}