From f0b0029f8fc4507d8d56e9b98782735ee7692ae4 Mon Sep 17 00:00:00 2001 From: Ken Kousen Date: Wed, 11 Dec 2024 14:40:20 -0500 Subject: [PATCH] Refactor and streamline code, add CompanyEmployee feature Updated stream operations for improved readability and efficiency, including refactoring looping constructs and comparisons. Simplified `equals` in `Person` and introduced new `CompanyEmployee` class implementing multiple interfaces, alongside its corresponding test. Removed unused logger from `LazyStreams` for cleanup. --- src/main/java/interfaces/CompanyEmployee.java | 30 +++++++++++++++++++ src/main/java/lambdas/Person.java | 5 +--- src/main/java/lambdas/UsePerson.java | 9 +++--- src/main/java/lazy/LazyStreams.java | 3 -- .../refactoring/after/LoopsSortsAndIfs.java | 28 +++++------------ .../java/interfaces/CompanyEmployeeTest.java | 6 ++-- 6 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 src/main/java/interfaces/CompanyEmployee.java diff --git a/src/main/java/interfaces/CompanyEmployee.java b/src/main/java/interfaces/CompanyEmployee.java new file mode 100644 index 0000000..877c5b2 --- /dev/null +++ b/src/main/java/interfaces/CompanyEmployee.java @@ -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 String.format("%s works for %s", 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 $$$"); + } +} diff --git a/src/main/java/lambdas/Person.java b/src/main/java/lambdas/Person.java index 3124833..7760f99 100644 --- a/src/main/java/lambdas/Person.java +++ b/src/main/java/lambdas/Person.java @@ -30,10 +30,7 @@ public void setName(String name) { @Override public boolean equals(Object o) { if (this == o) return true; - if (!(o instanceof Person)) return false; - - Person person = (Person) o; - + if (!(o instanceof Person person)) return false; return Objects.equals(name, person.name); } diff --git a/src/main/java/lambdas/UsePerson.java b/src/main/java/lambdas/UsePerson.java index a231dea..f438214 100644 --- a/src/main/java/lambdas/UsePerson.java +++ b/src/main/java/lambdas/UsePerson.java @@ -17,9 +17,9 @@ public static void main(String[] args) { } System.out.println(beatles); - List people = names.stream() // Stream + List people = names.stream() // Stream .map(name -> new Person(name)) // Stream - .collect(Collectors.toList()); // Converts Stream to List + .collect(Collectors.toList()); // Converts Stream to List System.out.println(people); people = names.stream() @@ -31,7 +31,7 @@ public static void main(String[] args) { Person[] peopleArray = names.stream() .map(Person::new) .toArray(Person[]::new); - //.toArray(value -> new Person[value]); + //.toArray(value -> new Person[value]); System.out.println(Arrays.toString(peopleArray)); List fullNames = Arrays.asList( @@ -49,9 +49,10 @@ public static void main(String[] args) { LinkedList linkedPersons = names.stream() .map(Person::new) .collect( - () -> new LinkedList(), // Supplier + () -> new LinkedList<>(), // Supplier (list, person) -> list.add(person), // BiConsumer (list1, list2) -> list1.addAll(list2)); // BiConsumer + //null); System.out.println(linkedPersons); linkedPersons = names.stream() diff --git a/src/main/java/lazy/LazyStreams.java b/src/main/java/lazy/LazyStreams.java index d7b8f28..4a9f3f4 100644 --- a/src/main/java/lazy/LazyStreams.java +++ b/src/main/java/lazy/LazyStreams.java @@ -1,11 +1,8 @@ package lazy; -import java.util.logging.Logger; import java.util.stream.IntStream; public class LazyStreams { - private static final Logger logger = Logger.getLogger(LazyStreams.class.getName()); - public static int multByTwo(int n) { System.out.printf("Inside multByTwo with arg %d on thread %s%n", n, Thread.currentThread().getName()); diff --git a/src/main/java/refactoring/after/LoopsSortsAndIfs.java b/src/main/java/refactoring/after/LoopsSortsAndIfs.java index 4b63ff1..bb334d1 100644 --- a/src/main/java/refactoring/after/LoopsSortsAndIfs.java +++ b/src/main/java/refactoring/after/LoopsSortsAndIfs.java @@ -1,30 +1,16 @@ package refactoring.after; -import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.Comparator; -import java.util.List; public class LoopsSortsAndIfs { public static void main(String[] args) { - String[] strings = "this is an array of strings".split(" "); - List evenLengths = new ArrayList<>(); - for (String s : strings) { - if (s.length() % 2 == 0) { - evenLengths.add(s.toUpperCase()); - } - } - - Collections.sort(evenLengths, new Comparator() { - @Override - public int compare(String s1, String s2) { - return s1.length() - s2.length(); - } - }); - - for (String s : evenLengths) { - System.out.println(s); - } + Arrays.stream("this is an array of strings".split(" ")) + .filter(s -> s.length() % 2 == 0) + .map(String::toUpperCase) + .sorted(Comparator.comparingInt(String::length) + .thenComparing(Comparator.naturalOrder())) + .forEach(System.out::println); } } diff --git a/src/test/java/interfaces/CompanyEmployeeTest.java b/src/test/java/interfaces/CompanyEmployeeTest.java index add8777..35e7539 100644 --- a/src/test/java/interfaces/CompanyEmployeeTest.java +++ b/src/test/java/interfaces/CompanyEmployeeTest.java @@ -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 @@ -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()); } } \ No newline at end of file