Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9daa314

Browse files
committed
Category Enum for category of Car
1 parentb23d843 commit9daa314

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
Personjohn =newPerson(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
packagecom.iluwatar.collectionpipeline;
2424

2525
/**
26-
* A Car class that has the properties of make, model, andyear.
26+
* A Car class that has the properties of make, model,yearandcategory.
2727
*/
2828
publicclassCar {
2929
privateStringmake;
3030
privateStringmodel;
3131
privateintyear;
32-
privateStringcategory;
32+
privateCategorycategory;
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-
publicCar(Stringmake,Stringmodel,intyearOfMake,Stringcategory) {
41+
publicCar(Stringmake,Stringmodel,intyearOfMake,Categorycategory) {
4142
this.make =make;
4243
this.model =model;
4344
this.year =yearOfMake;
@@ -56,7 +57,7 @@ public int getYear() {
5657
returnyear;
5758
}
5859

59-
publicStringgetCategory() {
60+
publicCategorygetCategory() {
6061
returncategory;
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
publicstaticList<Car>createCars() {
40-
returnArrays.asList(newCar("Jeep","Wrangler",2011,"Jeep"),newCar("Jeep","Comanche",1990,"Jeep"),
41-
newCar("Dodge","Avenger",2010,"Sedan"),
42-
newCar("Buick","Cascada",2016,"Convertible"),
43-
newCar("Ford","Focus",2012,"Sedan"),
44-
newCar("Chevrolet","Geo Metro",1992,"Convertible"));
40+
returnArrays.asList(newCar("Jeep","Wrangler",2011,Category.JEEP),
41+
newCar("Jeep","Comanche",1990,Category.JEEP),
42+
newCar("Dodge","Avenger",2010,Category.SEDAN),
43+
newCar("Buick","Cascada",2016,Category.CONVERTIBLE),
44+
newCar("Ford","Focus",2012,Category.SEDAN),
45+
newCar("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+
packagecom.iluwatar.collectionpipeline;
24+
25+
/**
26+
* Enum for the category of car
27+
*/
28+
publicenumCategory {
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-
publicstaticMap<String,List<Car>>getGroupingOfCarsByCategory(List<Car>cars) {
69+
publicstaticMap<Category,List<Car>>getGroupingOfCarsByCategory(List<Car>cars) {
7070
returncars.stream().collect(Collectors.groupingBy(Car::getCategory));
7171
}
7272

@@ -78,7 +78,7 @@ public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars)
7878
*/
7979
publicstaticList<Car>getSedanCarsOwnedSortedByDate(List<Person>persons) {
8080
returnpersons.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-
publicstaticMap<String,List<Car>>getGroupingOfCarsByCategory(List<Car>cars) {
90-
Map<String,List<Car>>groupingByCategory =newHashMap<>();
89+
publicstaticMap<Category,List<Car>>getGroupingOfCarsByCategory(List<Car>cars) {
90+
Map<Category,List<Car>>groupingByCategory =newHashMap<>();
9191
for (Carcar: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 =newArrayList<>();
116116
for (Carcar: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
publicvoidtestGetGroupingOfCarsByCategory() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp