From cc3336272b1ec4044a98d25624294523307dd975 Mon Sep 17 00:00:00 2001 From: Ken Kousen Date: Wed, 18 Sep 2024 09:01:44 -0400 Subject: [PATCH] Add tests and implement CompanyEmployee class Removed OptionalExceptionDemo file and added new test cases for sorting a list and the CompanyEmployee class. Updated build.gradle to use the latest JUnit version. Additionally, fixed a minor issue in FlatMapDemo related to checking for empty orders. --- build.gradle | 2 +- src/main/java/interfaces/CompanyEmployee.java | 30 +++++++++++++++++++ src/main/java/lazy/OptionalExceptionDemo.java | 7 ----- src/main/java/streams/FlatMapDemo.java | 3 +- .../java/interfaces/CompanyEmployeeTest.java | 6 ++-- src/test/java/streams/SortListTest.java | 24 +++++++++++++++ 6 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 src/main/java/interfaces/CompanyEmployee.java delete mode 100644 src/main/java/lazy/OptionalExceptionDemo.java create mode 100644 src/test/java/streams/SortListTest.java diff --git a/build.gradle b/build.gradle index 22beb3b..c3387aa 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ repositories { } dependencies { - testImplementation('org.junit.jupiter:junit-jupiter:5.10.3') + testImplementation('org.junit.jupiter:junit-jupiter:5.11.0') testImplementation 'org.assertj:assertj-core:3.26.3' testRuntimeOnly("org.junit.platform:junit-platform-launcher") diff --git a/src/main/java/interfaces/CompanyEmployee.java b/src/main/java/interfaces/CompanyEmployee.java new file mode 100644 index 0000000..fc128c2 --- /dev/null +++ b/src/main/java/interfaces/CompanyEmployee.java @@ -0,0 +1,30 @@ +package interfaces; + +public class CompanyEmployee implements Company, Employee { + private final String first; + private final String last; + + public CompanyEmployee(String first, String last) { + this.first = first; + this.last = last; + } + + public String getName() { + return "%s works for %s".formatted(Employee.super.getName(), Company.super.getName()); + } + + @Override + public String getFirst() { + return first; + } + + @Override + public String getLast() { + return last; + } + + @Override + public void doWork() { + System.out.println("Converting caffeine into code for $$$"); + } +} diff --git a/src/main/java/lazy/OptionalExceptionDemo.java b/src/main/java/lazy/OptionalExceptionDemo.java deleted file mode 100644 index 079be78..0000000 --- a/src/main/java/lazy/OptionalExceptionDemo.java +++ /dev/null @@ -1,7 +0,0 @@ -package lazy; - -public class OptionalExceptionDemo { - public static void main(String[] args) { - - } -} diff --git a/src/main/java/streams/FlatMapDemo.java b/src/main/java/streams/FlatMapDemo.java index c8186dc..ae9539f 100644 --- a/src/main/java/streams/FlatMapDemo.java +++ b/src/main/java/streams/FlatMapDemo.java @@ -44,9 +44,8 @@ public static void main(String[] args) { // stream() on an empty collection already returns an empty stream customers.stream() .flatMap(customer -> - customer.getOrders().size() == 0 ? Stream.empty() : + customer.getOrders().isEmpty() ? Stream.empty() : customer.getOrders().stream()) .forEach(System.out::println); - } } diff --git a/src/test/java/interfaces/CompanyEmployeeTest.java b/src/test/java/interfaces/CompanyEmployeeTest.java index add8777..35e7539 100644 --- a/src/test/java/interfaces/CompanyEmployeeTest.java +++ b/src/test/java/interfaces/CompanyEmployeeTest.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + // Create a class called CompanyEmployee that implements both // the Company and Employee interfaces // Implement the necessary methods @@ -11,7 +13,7 @@ public class CompanyEmployeeTest { @Test public void getName() { -// CompanyEmployee emp = new CompanyEmployee("Peter", "Gibbons"); -// assertEquals("Peter Gibbons works for Initech", emp.getName()); + CompanyEmployee emp = new CompanyEmployee("Peter", "Gibbons"); + assertEquals("Peter Gibbons works for Initech", emp.getName()); } } \ No newline at end of file diff --git a/src/test/java/streams/SortListTest.java b/src/test/java/streams/SortListTest.java new file mode 100644 index 0000000..933eabe --- /dev/null +++ b/src/test/java/streams/SortListTest.java @@ -0,0 +1,24 @@ +package streams; + +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.stream.Collectors; + +public class SortListTest { + private final List list = + List.of("banana", "apple", "orange", "kiwi", "grape"); + + @Test + void sortList_collections() { + // throws UnsupportedOperationException because + // List.of() returns an unmodifiable list +// Collections.sort(list); +// System.out.println(list); + List sorted = list.stream() + .sorted() + .collect(Collectors.toList()); + System.out.println(sorted); + System.out.println(list); + } +}