diff --git a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java index b990f9dff..ffd52cca3 100644 --- a/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java +++ b/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java @@ -45,6 +45,52 @@ public class Car { this.category = category; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((category == null) ? 0 : category.hashCode()); + result = prime * result + ((make == null) ? 0 : make.hashCode()); + result = prime * result + ((model == null) ? 0 : model.hashCode()); + result = prime * result + year; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Car other = (Car) obj; + if (category != other.category) { + return false; + } + if (make == null) { + if (other.make != null) { + return false; + } + } else if (!make.equals(other.make)) { + return false; + } + if (model == null) { + if (other.model != null) { + return false; + } + } else if (!model.equals(other.model)) { + return false; + } + if (year != other.year) { + return false; + } + return true; + } + public String getMake() { return make; } diff --git a/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java b/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java index c68521797..5a6821641 100644 --- a/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java +++ b/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java @@ -25,6 +25,7 @@ package com.iluwatar.collectionpipeline; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -40,27 +41,39 @@ public class AppTest { @Test public void testGetModelsAfter2000UsingFor() { List models = ImperativeProgramming.getModelsAfter2000(cars); - assertEquals(models, Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada")); + assertEquals(Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada"), models); } @Test public void testGetModelsAfter2000UsingPipeline() { List models = FunctionalProgramming.getModelsAfter2000(cars); - assertEquals(models, Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada")); + assertEquals(Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada"), models); } @Test public void testGetGroupingOfCarsByCategory() { + Map> modelsExpected = new HashMap<>(); + modelsExpected.put(Category.CONVERTIBLE, Arrays.asList(new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE), + new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE))); + modelsExpected.put(Category.SEDAN, Arrays.asList(new Car("Dodge", "Avenger", 2010, Category.SEDAN), + new Car("Ford", "Focus", 2012, Category.SEDAN))); + modelsExpected.put(Category.JEEP, Arrays.asList(new Car("Jeep", "Wrangler", 2011, Category.JEEP), + new Car("Jeep", "Comanche", 1990, Category.JEEP))); Map> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars); Map> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars); - assertEquals(modelsFunctional, modelsImperative); + System.out.println("Category " + modelsFunctional); + assertEquals(modelsExpected, modelsFunctional); + assertEquals(modelsExpected, modelsImperative); } @Test public void testGetSedanCarsOwnedSortedByDate() { Person john = new Person(cars); + List modelsExpected = Arrays.asList(new Car("Dodge", "Avenger", 2010, Category.SEDAN), + new Car("Ford", "Focus", 2012, Category.SEDAN)); List modelsFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john)); List modelsImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john)); - assertEquals(modelsFunctional, modelsImperative); + assertEquals(modelsExpected, modelsFunctional); + assertEquals(modelsExpected, modelsImperative); } }