-
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
8a283c6
commit 35f8e7c
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
kodilla-stream/src/main/java/com/kodilla/optional/homework/Application.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,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() | ||
); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
kodilla-stream/src/main/java/com/kodilla/optional/homework/Student.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,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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
kodilla-stream/src/main/java/com/kodilla/optional/homework/Teacher.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,13 @@ | ||
package com.kodilla.optional.homework; | ||
|
||
public class Teacher { | ||
private String name; | ||
|
||
public Teacher(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
kodilla-stream/src/main/java/com/kodilla/stream/homework/ForumStats.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,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())); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
kodilla-stream/src/test/java/com/kodilla/optional/ApplicationTest.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,11 @@ | ||
package com.kodilla.optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class ApplicationTest { | ||
|
||
@Test | ||
public void testCokolwiek() { | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
kodilla-stream/src/test/java/com/kodilla/stream/homework/ForumStatsTestSuite.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,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); | ||
} | ||
} |