Skip to content

Commit

Permalink
stream module
Browse files Browse the repository at this point in the history
  • Loading branch information
kacper-cholewinski committed Dec 14, 2024
1 parent 5cf4b74 commit 8a283c6
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
19 changes: 19 additions & 0 deletions kodilla-stream/build.gradle
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()
}
46 changes: 46 additions & 0 deletions kodilla-stream/src/main/java/com/kodilla/stream/User.java
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 kodilla-stream/src/main/java/com/kodilla/stream/UsersManager.java
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();
}
}
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;
}
}
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));
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ include 'kodilla-basic-tests'
include 'kodilla-collections'
include 'kodilla-collections'
include 'kodilla-collections-advanced'
include 'kodilla-stream'

0 comments on commit 8a283c6

Please sign in to comment.