-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cf4b74
commit 8a283c6
Showing
6 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
group = 'com.kodilla' | ||
version = '1.0-SNAPSHOT' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testImplementation platform('org.junit:junit-bom:5.10.0') | ||
testImplementation 'org.junit.jupiter:junit-jupiter' | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.kodilla.stream; | ||
|
||
import java.util.Objects; | ||
|
||
public class User { | ||
private String username; | ||
private int age; | ||
private int numberOfPost; | ||
private String group; | ||
|
||
public User(String username, int age, int numberOfPost, String group) { | ||
this.username = username; | ||
this.age = age; | ||
this.numberOfPost = numberOfPost; | ||
this.group = group; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public int getNumberOfPost() { | ||
return numberOfPost; | ||
} | ||
|
||
public String getGroup() { | ||
return group; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
User user = (User) o; | ||
return age == user.age && numberOfPost == user.numberOfPost && Objects.equals(username, user.username) && Objects.equals(group, user.group); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(username, age, numberOfPost, group); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
kodilla-stream/src/main/java/com/kodilla/stream/UsersManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.kodilla.stream; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class UsersManager { | ||
public static void main(String[] args) { | ||
List<String> chemistGroupUsernames = filterChemistGroupUsernames(); | ||
System.out.println(chemistGroupUsernames); | ||
} | ||
|
||
public static List<String> filterChemistGroupUsernames() { | ||
List<String> usernames = UsersRepository.getUsersList() | ||
.stream() | ||
.filter(user -> user.getGroup().equals("Chemists")) | ||
.map(UsersManager::getUserName) | ||
.collect(Collectors.toList()); | ||
|
||
return usernames; | ||
} | ||
|
||
public static List<User> filterAboveGivenAge(int givenAge) { | ||
return UsersRepository.getUsersList() | ||
.stream() | ||
.filter(user -> user.getAge() > givenAge) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static List<User> filterBelowPostLimit(int postLimit) { | ||
return UsersRepository.getUsersList() | ||
.stream() | ||
.filter(user -> user.getNumberOfPost() < postLimit) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static String getUserName(User user) { | ||
return user.getUsername(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
kodilla-stream/src/main/java/com/kodilla/stream/UsersRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.kodilla.stream; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class UsersRepository { | ||
public static List<User> getUsersList() { | ||
List<User> users = new ArrayList<>(); | ||
users.add(new User("Walter White", 50, 7, "Chemists")); | ||
users.add(new User("Jessie Pinkman", 25, 4648, "Sales")); | ||
users.add(new User("Tuco Salamanca", 34, 116, "Manager")); | ||
users.add(new User("Gus Fring", 49, 0, "Board")); | ||
users.add(new User("Gale Boetticher", 44, 2, "Chemists")); | ||
users.add(new User("Mike Ehrmantraut", 57, 0, "Security")); | ||
return users; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
kodilla-stream/src/test/java/com/kodilla/stream/UsersManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kodilla.stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class UsersManagerTest { | ||
@Test | ||
public void testFilterChemistGroupUsernames() { | ||
List<String> actualChemists = UsersManager.filterChemistGroupUsernames(); | ||
List<String> expectedChemists = new ArrayList<>(Arrays.asList("Gale Boetticher", "Walter White")); | ||
|
||
assertTrue(expectedChemists.containsAll(actualChemists)); | ||
assertTrue(actualChemists.containsAll(expectedChemists)); | ||
assertEquals(actualChemists.size(), expectedChemists.size()); | ||
} | ||
|
||
@Test | ||
public void testFilterAboveGivenAge() { | ||
List<User> usersAboveGivenAge = UsersManager.filterAboveGivenAge(35); | ||
|
||
assertTrue(usersAboveGivenAge.stream().allMatch(user -> user.getAge() > 35)); | ||
} | ||
|
||
@Test | ||
public void testFilterBelowPostLimit() { | ||
List<User> usersBelowPostLimit = UsersManager.filterBelowPostLimit(100); | ||
|
||
assertTrue(usersBelowPostLimit.stream().allMatch(user -> user.getNumberOfPost() < 100)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters