Skip to content

Commit

Permalink
Refactor and streamline code, add CompanyEmployee feature
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kousen committed Dec 11, 2024
1 parent 6fb07f4 commit f0b0029
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
30 changes: 30 additions & 0 deletions src/main/java/interfaces/CompanyEmployee.java
Original file line number Diff line number Diff line change
@@ -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 $$$");
}
}
5 changes: 1 addition & 4 deletions src/main/java/lambdas/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/lambdas/UsePerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static void main(String[] args) {
}
System.out.println(beatles);

List<Person> people = names.stream() // Stream<String>
List<Person> people = names.stream() // Stream<String>
.map(name -> new Person(name)) // Stream<Person>
.collect(Collectors.toList()); // Converts Stream<Person> to List<Person>
.collect(Collectors.toList()); // Converts Stream<Person> to List<Person>
System.out.println(people);

people = names.stream()
Expand All @@ -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<String> fullNames = Arrays.asList(
Expand All @@ -49,9 +49,10 @@ public static void main(String[] args) {
LinkedList<Person> linkedPersons = names.stream()
.map(Person::new)
.collect(
() -> new LinkedList<Person>(), // Supplier<LinkedList>
() -> new LinkedList<>(), // Supplier<LinkedList>
(list, person) -> list.add(person), // BiConsumer<LinkedList, Person>
(list1, list2) -> list1.addAll(list2)); // BiConsumer<LinkedList, LinkedList>
//null);
System.out.println(linkedPersons);

linkedPersons = names.stream()
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/lazy/LazyStreams.java
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
28 changes: 7 additions & 21 deletions src/main/java/refactoring/after/LoopsSortsAndIfs.java
Original file line number Diff line number Diff line change
@@ -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<String> evenLengths = new ArrayList<>();
for (String s : strings) {
if (s.length() % 2 == 0) {
evenLengths.add(s.toUpperCase());
}
}

Collections.sort(evenLengths, new Comparator<String>() {
@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);
}
}
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 f0b0029

Please sign in to comment.