-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CompanyEmployee class and update method names in Employee interface
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
Showing
4 changed files
with
44 additions
and
5 deletions.
There are no files selected for viewing
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,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..."); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -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()); | ||
} | ||
} |
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,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)); | ||
} | ||
} |
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