Skip to content

Commit

Permalink
Add CompanyEmployee class and update method names in Employee interface
Browse files Browse the repository at this point in the history
Added a new CompanyEmployee.java class that implements the Company and Employee interfaces. This class utilizes the Java Record feature to provide auto-generated methods such as toString(), equals(), and hashCode(). The class is final and doesn't belong to any inheritance hierarchy. Employee interface methods were renamed to match the property names for more clarity, and their output was formatted differently.

A new class, IterateOverMap.java, for illustrating iteration over a Map using lambda expressions was also introduced.

Further, the unit tests in the CompanyEmployeeTest.java file were uncommented and adjusted to match the updated code. This helps to ensure that the project has effective test coverage at all times.
  • Loading branch information
kousen committed Nov 8, 2023
1 parent 0428794 commit a49c6a7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/main/java/interfaces/CompanyEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package interfaces;

// Records:
// - immutable data holders
// - final class and extend java.lang.Record, so can't be in an inheritance hierarchy
// - autogenerate toString(), equals(), and hashCode() methods
// - the "accessor" methods match the names of the properties (e.g. first() and last())
// - canonical constructor appears before the {}
public record CompanyEmployee(String first, String last) implements Company, Employee {

public String getName() {
return Employee.super.getName() + " works for " + Company.super.getName();
}

@Override
public void doWork() {
System.out.println("Preparing TPS reports for six different bosses...");
}

}
6 changes: 3 additions & 3 deletions src/main/java/interfaces/Employee.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package interfaces;

public interface Employee {
String getFirst();
String first();

String getLast();
String last();

void doWork();

default String getName() {
return String.format("%s %s", getFirst(), getLast());
return "%s %s".formatted(first(), last());
}
}
17 changes: 17 additions & 0 deletions src/main/java/lambdas/IterateOverMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lambdas;

import java.util.Map;

public class IterateOverMap {
public static void main(String[] args) {
Map<String, String> languageMap = Map.ofEntries(Map.entry("Java", "https://java.com"),
Map.entry("Kotlin", "https://kotlinlang.org"),
Map.entry("Scala", "https://scala-lang.org"),
Map.entry("Groovy", "https://groovy-lang.org"));

// Iterate over Map
// We provide just the BiConsumer
// The library applies it to each key/value pair
languageMap.forEach((key, value) -> System.out.println(key + " -> " + value));
}
}
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());
}
}

0 comments on commit a49c6a7

Please sign in to comment.