diff --git a/kodilla-stream/build.gradle b/kodilla-stream/build.gradle new file mode 100644 index 0000000..50dae20 --- /dev/null +++ b/kodilla-stream/build.gradle @@ -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() +} \ No newline at end of file diff --git a/kodilla-stream/src/main/java/com/kodilla/stream/User.java b/kodilla-stream/src/main/java/com/kodilla/stream/User.java new file mode 100644 index 0000000..222d2f5 --- /dev/null +++ b/kodilla-stream/src/main/java/com/kodilla/stream/User.java @@ -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); + } +} diff --git a/kodilla-stream/src/main/java/com/kodilla/stream/UsersManager.java b/kodilla-stream/src/main/java/com/kodilla/stream/UsersManager.java new file mode 100644 index 0000000..6e7e905 --- /dev/null +++ b/kodilla-stream/src/main/java/com/kodilla/stream/UsersManager.java @@ -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 chemistGroupUsernames = filterChemistGroupUsernames(); + System.out.println(chemistGroupUsernames); + } + + public static List filterChemistGroupUsernames() { + List usernames = UsersRepository.getUsersList() + .stream() + .filter(user -> user.getGroup().equals("Chemists")) + .map(UsersManager::getUserName) + .collect(Collectors.toList()); + + return usernames; + } + + public static List filterAboveGivenAge(int givenAge) { + return UsersRepository.getUsersList() + .stream() + .filter(user -> user.getAge() > givenAge) + .collect(Collectors.toList()); + } + + public static List filterBelowPostLimit(int postLimit) { + return UsersRepository.getUsersList() + .stream() + .filter(user -> user.getNumberOfPost() < postLimit) + .collect(Collectors.toList()); + } + + public static String getUserName(User user) { + return user.getUsername(); + } +} diff --git a/kodilla-stream/src/main/java/com/kodilla/stream/UsersRepository.java b/kodilla-stream/src/main/java/com/kodilla/stream/UsersRepository.java new file mode 100644 index 0000000..cb686e7 --- /dev/null +++ b/kodilla-stream/src/main/java/com/kodilla/stream/UsersRepository.java @@ -0,0 +1,17 @@ +package com.kodilla.stream; + +import java.util.ArrayList; +import java.util.List; + +public class UsersRepository { + public static List getUsersList() { + List 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; + } +} \ No newline at end of file diff --git a/kodilla-stream/src/test/java/com/kodilla/stream/UsersManagerTest.java b/kodilla-stream/src/test/java/com/kodilla/stream/UsersManagerTest.java new file mode 100644 index 0000000..5ec1b2a --- /dev/null +++ b/kodilla-stream/src/test/java/com/kodilla/stream/UsersManagerTest.java @@ -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 actualChemists = UsersManager.filterChemistGroupUsernames(); + List 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 usersAboveGivenAge = UsersManager.filterAboveGivenAge(35); + + assertTrue(usersAboveGivenAge.stream().allMatch(user -> user.getAge() > 35)); + } + + @Test + public void testFilterBelowPostLimit() { + List usersBelowPostLimit = UsersManager.filterBelowPostLimit(100); + + assertTrue(usersBelowPostLimit.stream().allMatch(user -> user.getNumberOfPost() < 100)); + } +} diff --git a/settings.gradle b/settings.gradle index 80323d8..f3d0ce7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,4 +4,5 @@ include 'kodilla-basic-tests' include 'kodilla-collections' include 'kodilla-collections' include 'kodilla-collections-advanced' +include 'kodilla-stream'