Range validation
1. Range validation
Let's validate ranges!2. Chocolate sales dataset
Throughout the chapter, we'll work with subsets of a chocolate sales dataset. We may modify certain columns like amount and date to illustrate data transformations.3. Why we need range validation
Range validation ensures our applications work with accurate information. In our chocolate sales dataset, we've extracted two columns to demonstrate range validation: sales amounts and dates. The `Arrays.asList()` method creates fixed-size lists from our data. We'll use these lists throughout our examples to show different validation techniques.4. Finding range boundaries
Before setting validation ranges, it's helpful to analyze the actual data distribution. The `Collections` utility class provides `min()` and `max()` methods to find extreme values for various datatypes in the `Collections` framework. While the `DescriptiveStatistics` package from earlier provides comprehensive statistical analysis, here we use simpler `Collections` methods since we only need basic min/max values.5. Setting a valid range
Let's start with a simple approach to range validation using basic comparison operators. We set `lowerThreshold` and `upperThreshold` for valid sales amounts. The `for` loop iterates through our sales amounts, using `>=` and `<=` operators to check if each amount falls within our defined range.6. Setting a valid range: outputs
We print all validation results here for learning purposes, though in practice we typically only log values that fail validation.7. Defining range categories
Now we'll use the `Range` class to create more sophisticated validation categories. The `Range.between()` method creates inclusive ranges that we can use to categorize sales amounts as `lowSales`, `mediumSales`, and `highSales`.8. Checking range categories
The `for` loop over `salesAmounts` checks each category. The `contains()` method provides a cleaner way to check if a value falls within a range compared to using comparison operators. This approach makes it easy to categorize values into multiple ranges.9. Checking range categories: outputs
The outputs show how each amount falls into a sales range category. Now that we understand our numeric ranges, let's look at date validation.10. Validating dates
Date validation ensures transactions fall within expected time periods. We use `LocalDate` for date handling and `DateTimeFormatter` for parsing dates in our format. The pattern "d-MMM-yy" specifies day of month (d), abbreviated month name (MMM), and two-digit year (yy), matching our input format like "4-Jan-22". The `for` loop reads in each date in `saleDates` using `LocalDate.parse`. The `isAfter()` and `isBefore()` methods allow us to check if a date occurs after `startDate` and before `endDate`.11. Validating dates: outputs
By iterating through our list of sale dates, we can validate that all transactions occurred within 2022.12. Putting it all together
We've explored range validation using our chocolate sales data. `Collections.min/max()` help analyze data distributions to set boundaries. `Range.between()` creates categories like low or high sales, while `contains()` checks if values fall within these ranges. For dates, `LocalDate`'s `isAfter()` and `isBefore()` ensure transactions occur within valid periods. These tools work together to maintain data integrity by catching out-of-range values.13. Let's practice!
Now it's your turn!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.