Skip to content

Commit

Permalink
stream.homework module
Browse files Browse the repository at this point in the history
  • Loading branch information
kacper-cholewinski committed Dec 14, 2024
1 parent 8a283c6 commit 35f8e7c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kodilla.optional.homework;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class Application {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(Arrays.asList(
new Student("Scout Duke", new Teacher("Martha Nguyen")),
new Student("Paola Rice", null),
new Student("Laurel Reilly", new Teacher("Brooklyn Hopkins"))
));

for (Student student : students) {
System.out.println(
"student: "
+ student.getName()
+ " - teacher: "
+ Optional.ofNullable(student.getTeacher()).orElse(new Teacher("<undefined>")).getName()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kodilla.optional.homework;

public class Student {
private String name;
private Teacher teacher;

public Student(String name, Teacher teacher) {
this.name = name;
this.teacher = teacher;
}

public String getName() {
return name;
}

public Teacher getTeacher() {
return teacher;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kodilla.optional.homework;

public class Teacher {
private String name;

public Teacher(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.kodilla.stream.homework;

import com.kodilla.stream.User;
import com.kodilla.stream.UsersRepository;

import java.util.List;

public class ForumStats {
public static double getAverageNumberOfPostsForOldUsers(List<User> users) {
return users
.stream()
.filter(user -> user.getAge() >= 40)
.mapToInt(user -> user.getNumberOfPost())
.average()
.orElse(-1);
}

public static double getAverageNumberOfPostsForYoungUsers(List<User> users) {
return users
.stream()
.filter(user -> user.getAge() < 40)
.mapToInt(user -> user.getNumberOfPost())
.average()
.orElse(-1);
}

public static void main(String[] args) {
System.out.println("old users (>=40): " + getAverageNumberOfPostsForOldUsers(UsersRepository.getUsersList()));
System.out.println("young users (<40): " + getAverageNumberOfPostsForYoungUsers(UsersRepository.getUsersList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kodilla.optional;

import org.junit.jupiter.api.Test;

public class ApplicationTest {

@Test
public void testCokolwiek() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.kodilla.stream.homework;

import com.kodilla.stream.User;
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 ForumStatsTestSuite {
@Test
public void testGetAverageNumberOfPostsForOldUsers() {
List<User> users = new ArrayList<>(Arrays.asList(
new User("Khalil Bridges", 31, 215, "A"),
new User("Thiago Shepherd", 17, 385, "B"),
new User("Kasen Rasmussen", 58, 2, "C"),
new User("Emery McKenzie", 47, 98, "A")
));

assertEquals(50., ForumStats.getAverageNumberOfPostsForOldUsers(users), 0.001);
}

@Test
public void testGetAverageNumberOfPostsForNewUsers() {
List<User> users = new ArrayList<>(Arrays.asList(
new User("Khalil Bridges", 31, 215, "A"),
new User("Thiago Shepherd", 17, 385, "B"),
new User("Kasen Rasmussen", 58, 2, "C"),
new User("Emery McKenzie", 47, 98, "A")
));

assertEquals(300., ForumStats.getAverageNumberOfPostsForYoungUsers(users), 0.001);
}
}

0 comments on commit 35f8e7c

Please sign in to comment.