Skip to content

Commit 9daa314

Browse files
committed
Category Enum for category of Car
1 parent b23d843 commit 9daa314

File tree

7 files changed

+50
-18
lines changed

7 files changed

+50
-18
lines changed

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/App.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public static void main(String[] args) {
5959
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
6060
LOGGER.info(modelsFunctional.toString());
6161

62-
Map<String, List<Car>> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
62+
Map<Category, List<Car>> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
6363
LOGGER.info(groupingByCategoryImperative.toString());
6464

65-
Map<String, List<Car>> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
65+
Map<Category, List<Car>> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
6666
LOGGER.info(groupingByCategoryFunctional.toString());
6767

6868
Person john = new Person(cars);

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@
2323
package com.iluwatar.collectionpipeline;
2424

2525
/**
26-
* A Car class that has the properties of make, model, and year.
26+
* A Car class that has the properties of make, model, year and category.
2727
*/
2828
public class Car {
2929
private String make;
3030
private String model;
3131
private int year;
32-
private String category;
32+
private Category category;
3333

3434
/**
3535
* Constructor to create an instance of car.
3636
* @param make the make of the car
3737
* @param model the model of the car
3838
* @param yearOfMake the year of built of the car
39+
* @param category the {@link Category} of the car
3940
*/
40-
public Car(String make, String model, int yearOfMake, String category) {
41+
public Car(String make, String model, int yearOfMake, Category category) {
4142
this.make = make;
4243
this.model = model;
4344
this.year = yearOfMake;
@@ -56,7 +57,7 @@ public int getYear() {
5657
return year;
5758
}
5859

59-
public String getCategory() {
60+
public Category getCategory() {
6061
return category;
6162
}
6263
}

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/CarFactory.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ private CarFactory() {
3737
* @return {@link List} of {@link Car}
3838
*/
3939
public static List<Car> createCars() {
40-
return Arrays.asList(new Car("Jeep", "Wrangler", 2011, "Jeep"), new Car("Jeep", "Comanche", 1990, "Jeep"),
41-
new Car("Dodge", "Avenger", 2010, "Sedan"),
42-
new Car("Buick", "Cascada", 2016, "Convertible"),
43-
new Car("Ford", "Focus", 2012, "Sedan"),
44-
new Car("Chevrolet", "Geo Metro", 1992, "Convertible"));
40+
return Arrays.asList(new Car("Jeep", "Wrangler", 2011, Category.JEEP),
41+
new Car("Jeep", "Comanche", 1990, Category.JEEP),
42+
new Car("Dodge", "Avenger", 2010, Category.SEDAN),
43+
new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
44+
new Car("Ford", "Focus", 2012, Category.SEDAN),
45+
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE));
4546
}
4647
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.collectionpipeline;
24+
25+
/**
26+
* Enum for the category of car
27+
*/
28+
public enum Category {
29+
JEEP, SEDAN, CONVERTIBLE
30+
}

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/FunctionalProgramming.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static List<String> getModelsAfter2000(List<Car> cars) {
6666
* @param cars {@link List} of {@link Car} to be used for grouping
6767
* @return {@link Map} with category as key and cars belonging to that category as value
6868
*/
69-
public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
69+
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
7070
return cars.stream().collect(Collectors.groupingBy(Car::getCategory));
7171
}
7272

@@ -78,7 +78,7 @@ public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars)
7878
*/
7979
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
8080
return persons.stream().map(Person::getCars).flatMap(List::stream)
81-
.filter(car -> "Sedan".equals(car.getCategory()))
81+
.filter(car -> Category.SEDAN.equals(car.getCategory()))
8282
.sorted(Comparator.comparing(Car::getYear)).collect(Collectors.toList());
8383
}
8484
}

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/ImperativeProgramming.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public int compare(Car car1, Car car2) {
8686
* @param cars {@link List} of {@link Car} to be used for grouping
8787
* @return {@link Map} with category as key and cars belonging to that category as value
8888
*/
89-
public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
90-
Map<String, List<Car>> groupingByCategory = new HashMap<>();
89+
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
90+
Map<Category, List<Car>> groupingByCategory = new HashMap<>();
9191
for (Car car: cars) {
9292
if (groupingByCategory.containsKey(car.getCategory())) {
9393
groupingByCategory.get(car.getCategory()).add(car);
@@ -114,7 +114,7 @@ public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
114114

115115
List<Car> sedanCars = new ArrayList<>();
116116
for (Car car: cars) {
117-
if ("Sedan".equals(car.getCategory())) {
117+
if (Category.SEDAN.equals(car.getCategory())) {
118118
sedanCars.add(car);
119119
}
120120
}

collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public void testGetModelsAfter2000UsingPipeline() {
5151

5252
@Test
5353
public void testGetGroupingOfCarsByCategory() {
54-
Map<String, List<Car>> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
55-
Map<String, List<Car>> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
54+
Map<Category, List<Car>> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
55+
Map<Category, List<Car>> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
5656
assertEquals(modelsFunctional, modelsImperative);
5757
}
5858

0 commit comments

Comments
 (0)