Day 2 (filtering melons of a certain weight)

While Mark was satisfied with the result, he requested another filter to obtain melons of a certain weight (for example, all the melons that are 1,200 grams). We've just implemented a filter like this for melon types, and so we can come up with a new static method for melons of a certain weight, as follows:

public static List<Melon> filterByWeight(
List<Melon> melons, int weight) {

List<Melon> result = new ArrayList<>();

for (Melon melon: melons) {
if (melon != null && melon.getWeight() == weight) {
result.add(melon);
}
}

return result;
}

This is similar to filterByType(), except it has a different condition/filter. As developers, we are starting to understand that, if we continue like this, then the Filters class will end up with a lot of methods that simply repeat the code and use a different condition. We are very close to a boilerplate code case here.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.198.81