Category Enum for category of Car
This commit is contained in:
parent
b23d8430af
commit
9daa3140e4
@ -59,10 +59,10 @@ public class App {
|
||||
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
|
||||
LOGGER.info(modelsFunctional.toString());
|
||||
|
||||
Map<String, List<Car>> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
||||
Map<Category, List<Car>> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
||||
LOGGER.info(groupingByCategoryImperative.toString());
|
||||
|
||||
Map<String, List<Car>> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
||||
Map<Category, List<Car>> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
||||
LOGGER.info(groupingByCategoryFunctional.toString());
|
||||
|
||||
Person john = new Person(cars);
|
||||
|
@ -23,21 +23,22 @@
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
/**
|
||||
* A Car class that has the properties of make, model, and year.
|
||||
* A Car class that has the properties of make, model, year and category.
|
||||
*/
|
||||
public class Car {
|
||||
private String make;
|
||||
private String model;
|
||||
private int year;
|
||||
private String category;
|
||||
private Category category;
|
||||
|
||||
/**
|
||||
* Constructor to create an instance of car.
|
||||
* @param make the make of the car
|
||||
* @param model the model of the car
|
||||
* @param yearOfMake the year of built of the car
|
||||
* @param category the {@link Category} of the car
|
||||
*/
|
||||
public Car(String make, String model, int yearOfMake, String category) {
|
||||
public Car(String make, String model, int yearOfMake, Category category) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
this.year = yearOfMake;
|
||||
@ -56,7 +57,7 @@ public class Car {
|
||||
return year;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
public Category getCategory() {
|
||||
return category;
|
||||
}
|
||||
}
|
@ -37,10 +37,11 @@ public class CarFactory {
|
||||
* @return {@link List} of {@link Car}
|
||||
*/
|
||||
public static List<Car> createCars() {
|
||||
return Arrays.asList(new Car("Jeep", "Wrangler", 2011, "Jeep"), new Car("Jeep", "Comanche", 1990, "Jeep"),
|
||||
new Car("Dodge", "Avenger", 2010, "Sedan"),
|
||||
new Car("Buick", "Cascada", 2016, "Convertible"),
|
||||
new Car("Ford", "Focus", 2012, "Sedan"),
|
||||
new Car("Chevrolet", "Geo Metro", 1992, "Convertible"));
|
||||
return Arrays.asList(new Car("Jeep", "Wrangler", 2011, Category.JEEP),
|
||||
new Car("Jeep", "Comanche", 1990, Category.JEEP),
|
||||
new Car("Dodge", "Avenger", 2010, Category.SEDAN),
|
||||
new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
|
||||
new Car("Ford", "Focus", 2012, Category.SEDAN),
|
||||
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE));
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.collectionpipeline;
|
||||
|
||||
/**
|
||||
* Enum for the category of car
|
||||
*/
|
||||
public enum Category {
|
||||
JEEP, SEDAN, CONVERTIBLE
|
||||
}
|
@ -66,7 +66,7 @@ public class FunctionalProgramming {
|
||||
* @param cars {@link List} of {@link Car} to be used for grouping
|
||||
* @return {@link Map} with category as key and cars belonging to that category as value
|
||||
*/
|
||||
public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
||||
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
||||
return cars.stream().collect(Collectors.groupingBy(Car::getCategory));
|
||||
}
|
||||
|
||||
@ -78,7 +78,7 @@ public class FunctionalProgramming {
|
||||
*/
|
||||
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
|
||||
return persons.stream().map(Person::getCars).flatMap(List::stream)
|
||||
.filter(car -> "Sedan".equals(car.getCategory()))
|
||||
.filter(car -> Category.SEDAN.equals(car.getCategory()))
|
||||
.sorted(Comparator.comparing(Car::getYear)).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
@ -86,8 +86,8 @@ public class ImperativeProgramming {
|
||||
* @param cars {@link List} of {@link Car} to be used for grouping
|
||||
* @return {@link Map} with category as key and cars belonging to that category as value
|
||||
*/
|
||||
public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
||||
Map<String, List<Car>> groupingByCategory = new HashMap<>();
|
||||
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
|
||||
Map<Category, List<Car>> groupingByCategory = new HashMap<>();
|
||||
for (Car car: cars) {
|
||||
if (groupingByCategory.containsKey(car.getCategory())) {
|
||||
groupingByCategory.get(car.getCategory()).add(car);
|
||||
@ -114,7 +114,7 @@ public class ImperativeProgramming {
|
||||
|
||||
List<Car> sedanCars = new ArrayList<>();
|
||||
for (Car car: cars) {
|
||||
if ("Sedan".equals(car.getCategory())) {
|
||||
if (Category.SEDAN.equals(car.getCategory())) {
|
||||
sedanCars.add(car);
|
||||
}
|
||||
}
|
||||
|
@ -51,8 +51,8 @@ public class AppTest {
|
||||
|
||||
@Test
|
||||
public void testGetGroupingOfCarsByCategory() {
|
||||
Map<String, List<Car>> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
||||
Map<String, List<Car>> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
||||
Map<Category, List<Car>> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
|
||||
Map<Category, List<Car>> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
|
||||
assertEquals(modelsFunctional, modelsImperative);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user