Skip to content

Commit

Permalink
Add tests and implement CompanyEmployee class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kousen committed Sep 18, 2024
1 parent b4e217c commit cc33362
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/interfaces/CompanyEmployee.java
Original file line number Diff line number Diff line change
@@ -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 $$$");
}
}
7 changes: 0 additions & 7 deletions src/main/java/lazy/OptionalExceptionDemo.java

This file was deleted.

3 changes: 1 addition & 2 deletions src/main/java/streams/FlatMapDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
6 changes: 4 additions & 2 deletions src/test/java/interfaces/CompanyEmployeeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
24 changes: 24 additions & 0 deletions src/test/java/streams/SortListTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> sorted = list.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(sorted);
System.out.println(list);
}
}

0 comments on commit cc33362

Please sign in to comment.