-
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 UsePerson functionality and refactored existing code
This commit modifies the UsePerson.java file to provide a more streamlined approach to converting names to objects. It modifies the main method to incorporate separate helper methods for different approaches: Java 7 convention, streaming, and constructor referencing. It also returns result lists or arrays instead of printing them. Additionally, we introduce the new UsePersonTest.java file containing unit tests for the introduced methods in UsePerson.java file. It also tests for more advanced cases like usage of parallel streams and three-arg-collect handling. Changes have also been made to the CompanyEmployee.java file to implement Company and Employee interfaces with a test case in CompanyEmployeeTest.java file. In Person.java, added a new comment for improved readability of the code. These new changes result in a reduction of clutter in the main method, and cultivate easier testing and verification of individual functionalities.
- Loading branch information
Showing
6 changed files
with
169 additions
and
40 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,32 @@ | ||
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; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return Employee.super.getName() + " works for " + | ||
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 $$..."); | ||
} | ||
} |
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
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
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
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
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,82 @@ | ||
package lambdas; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class UsePersonTest { | ||
private final UsePerson usePerson = new UsePerson(); | ||
|
||
@Test | ||
void convertNamesToPeople_java7andEarlier() { | ||
List<Person> beatles = usePerson.convertNamesToPeopleJava7(); | ||
assertThat(beatles).contains( | ||
new Person("John"), | ||
new Person("Paul"), | ||
new Person("George"), | ||
new Person("Ringo")); | ||
} | ||
|
||
@Test | ||
void convertNamesToPeople_streams() { | ||
List<Person> beatles = usePerson.convertNamesToPeopleStreams(); | ||
assertThat(beatles).contains( | ||
new Person("John"), | ||
new Person("Paul"), | ||
new Person("George"), | ||
new Person("Ringo")); | ||
} | ||
|
||
@Test | ||
void convertNamesToPeople_ctrRef() { | ||
List<Person> beatles = usePerson.convertNamesUsingCtorRef(); | ||
assertThat(beatles).contains( | ||
new Person("John"), | ||
new Person("Paul"), | ||
new Person("George"), | ||
new Person("Ringo")); | ||
} | ||
|
||
@Test | ||
void convertNamesToPersonArray() { | ||
Person[] beatles = usePerson.convertNamesToPersonArray(); | ||
assertThat(beatles).contains( | ||
new Person("John"), | ||
new Person("Paul"), | ||
new Person("George"), | ||
new Person("Ringo")); | ||
} | ||
|
||
@Test | ||
void convertFullNamesToPeople() { | ||
List<Person> beatles = usePerson.convertFullNamesToPerson(); | ||
assertThat(beatles).contains( | ||
new Person("John", "Lennon"), | ||
new Person("Paul", "McCartney"), | ||
new Person("George", "Harrison"), | ||
new Person("Ringo", "Starr")); | ||
} | ||
|
||
@Test | ||
void threeArgCollect() { | ||
List<Person> beatles = usePerson.threeArgCollect(); | ||
assertThat(beatles).contains( | ||
new Person("John"), | ||
new Person("Paul"), | ||
new Person("George"), | ||
new Person("Ringo")); | ||
} | ||
|
||
@Test | ||
void threeArgCollect_methodRefs() { | ||
List<Person> beatles = usePerson.threeArgCollectMethodRefs(); | ||
assertThat(beatles).contains( | ||
new Person("John"), | ||
new Person("Paul"), | ||
new Person("George"), | ||
new Person("Ringo")); | ||
} | ||
|
||
} |