Skip to content

Commit

Permalink
Add new CompanyEmployee class and update tests for code clarity and…
Browse files Browse the repository at this point in the history
… accuracy

Introduced a new class, `CompanyEmployee`, which implements both `Company` and `Employee` interfaces for a more structured and object-oriented approach. Updated `LazyErrorMessageTest` and `CompanyEmployeeTest` to reflect these changes. Moreover, the `getErrorMessage` method in `LazyErrorMessageTest` now accepts a parameter to provide a more accurate error message, thus improving debugging and readability.
  • Loading branch information
kousen committed Oct 4, 2023
1 parent 21dec38 commit 5ff5ee4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
60 changes: 60 additions & 0 deletions src/main/java/interfaces/CompanyEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package interfaces;

import java.util.Objects;

// Java records became GA in Java 16
// - immutable data holders
// - autogenerate equals, hashCode, toString
// - primary constructor that appears before the braces, as in
// record CompanyEmployee(String first, String last) implements Company, Employee {}
// - can have other methods, but not modify the properties
// - property "getters" match the property names, as in first() and last()
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 getFirst() {
return first;
}

public String getLast() {
return last;
}

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

public void doWork() {
System.out.println("Converting caffeine into code for $$$");
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (CompanyEmployee) obj;
return Objects.equals(this.first, that.first) &&
Objects.equals(this.last, that.last);
}

@Override
public int hashCode() {
return Objects.hash(first, last);
}

@Override
public String toString() {
return "CompanyEmployee[" +
"first=" + first + ", " +
"last=" + last + ']';
}


}
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());
}
}
8 changes: 4 additions & 4 deletions src/test/java/lambdas/LazyErrorMessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

public class LazyErrorMessageTest {

private String getErrorMessage() {
private String getErrorMessage(boolean value) {
System.out.println("Generating error message...");
return "x should be true";
return value + " should be true";
}

@Test
void assertArgIsTrue() {
boolean x = true;
assertTrue(x, getErrorMessage()); // 2nd arg is a String
assertTrue(x, getErrorMessage(x)); // 2nd arg is a String
}

@Test
void assertArgIsTrue_lazyErrorMessage() {
boolean x = true;
assertTrue(x, () -> getErrorMessage()); // 2nd arg is a Supplier<String>
assertTrue(x, () -> getErrorMessage(x)); // 2nd arg is a Supplier<String>
}
}

0 comments on commit 5ff5ee4

Please sign in to comment.