Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/lambdas-cmmsnow.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Lambdas Exercise
Suppose that you are creating a social networking application. You want to create a feature that enables an administrator to perform any kind of action, such as sending a message, on members of the social networking application that satisfy certain criteria. The following table describes this use case in detail:

Suppose that members of this social networking application are represented by the following Person class:
Suppose that members of this social networking application are represented by the following java.Person class:

```
public class Person {
public class java.Person {

public enum Sex {
MALE, FEMALE
Expand All @@ -25,14 +25,14 @@ public class Person {
}
```

Suppose that the members of your social networking application are stored in a `List<Person>` instance.
Suppose that the members of your social networking application are stored in a `List<java.Person>` instance.

### Approach 1: Create Methods That Search for Members That Match One Characteristic
One simplistic approach is to create several methods; each method searches for members that match one characteristic, such as gender or age. The following method prints members that are older than a specified age:

```
public static void printPersonsOlderThan(List<Person> roster, int age) {
for (Person p : roster) {
public static void printPersonsOlderThan(List<java.Person> roster, int age) {
for (java.Person p : roster) {
if (p.getAge() >= age) {
p.printPerson();
}
Expand All @@ -44,38 +44,38 @@ The following method is more generic than printPersonsOlderThan; it prints membe

```
public static void printPersonsWithinAgeRange(
List<Person> roster, int low, int high) {
for (Person p : roster) {
List<java.Person> roster, int low, int high) {
for (java.Person p : roster) {
if (low <= p.getAge() && p.getAge() < high) {
p.printPerson();
}
}
}
```
What if you want to print members of a specified sex, or a combination of a specified gender and age range? What if you decide to change the Person class and add other attributes such as relationship status or geographical location? Although this method is more generic than printPersonsOlderThan, trying to create a separate method for each possible search query can still lead to brittle code. You can instead separate the code that specifies the criteria for which you want to search in a different class.
What if you want to print members of a specified sex, or a combination of a specified gender and age range? What if you decide to change the java.Person class and add other attributes such as relationship status or geographical location? Although this method is more generic than printPersonsOlderThan, trying to create a separate method for each possible search query can still lead to brittle code. You can instead separate the code that specifies the criteria for which you want to search in a different class.

### Approach 3: Specify Search Criteria Code in a Local Class

The following method prints members that match search criteria that you specify:

```
public static void printPersons(
List<Person> roster, CheckPerson tester) {
for (Person p : roster) {
List<java.Person> roster, java.CheckPerson tester) {
for (java.Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}
```

This method checks each Person instance contained in the List parameter roster whether it satisfies the search criteria specified in the CheckPerson parameter tester by invoking the method tester.test. If the method tester.test returns a true value, then the method printPersons is invoked on the Person instance.
This method checks each java.Person instance contained in the List parameter roster whether it satisfies the search criteria specified in the java.CheckPerson parameter tester by invoking the method tester.test. If the method tester.test returns a true value, then the method printPersons is invoked on the java.Person instance.

To specify the search criteria, you implement the CheckPerson interface:
To specify the search criteria, you implement the java.CheckPerson interface:

```
interface CheckPerson {
boolean test(Person p);
interface java.CheckPerson {
boolean test(java.Person p);
}
```

Expand Down
Binary file added out/production/lambdas-cmmsnow/CheckPerson.class
Binary file not shown.
Binary file added out/production/lambdas-cmmsnow/Person$Sex.class
Binary file not shown.
Binary file added out/production/lambdas-cmmsnow/Person.class
Binary file not shown.
Binary file added out/production/lambdas-cmmsnow/Search.class
Binary file not shown.
Binary file added out/production/lambdas-cmmsnow/SocialNetworks.class
Binary file not shown.
Binary file added out/test/lambdas-cmmsnow/PersonTest.class
Binary file not shown.
Binary file added out/test/lambdas-cmmsnow/SearchTest.class
Binary file not shown.
5 changes: 5 additions & 0 deletions src/main/java/CheckPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public interface CheckPerson {

boolean test(Person p);

}
93 changes: 93 additions & 0 deletions src/main/java/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.time.LocalDate;
import java.time.Period;
import java.util.List;

public class Person /*implements CheckPerson*/ {

public enum Sex {
MALE, FEMALE
}

String name;
LocalDate birthday;
Sex gender;
String emailAddress;

public Person(String name, LocalDate birthday, Sex gender, String emailAddress) {
this.name = name;
this.birthday = birthday;
this.gender = gender;
this.emailAddress = emailAddress;
}

public Person() {
this.name = "";
this.birthday = LocalDate.now();
this.gender = null;
this.emailAddress = "";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public LocalDate getBirthday() {
return birthday;
}

public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}

public Sex getGender() {
return gender;
}

public void setGender(Sex gender) {
this.gender = gender;
}

public String getEmailAddress() {
return emailAddress;
}

public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

public int getAge() {
return Period.between(birthday, LocalDate.now()).getYears();
}

@Override
public String toString() {
return "java.Person{" +
"name='" + name + '\'' +
", birthday=" + birthday +
", gender=" + gender +
", emailAddress='" + emailAddress + '\'' +
'}';
}

public void printPerson() {
System.out.println(toString());
}

// @Override
// public boolean test(Person p) {
// return p.gender == Person.Sex.FEMALE && p.getAge() >= 0 && p.getAge() <= 80;
// }
//
// public static String printPersons(List<Person> roster, CheckPerson tester) {
// for (Person p : roster) {
// if (tester.test(p)) {
// p.printPerson();
// }
// }
// }

}
19 changes: 19 additions & 0 deletions src/main/java/Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.List;

public class Search implements CheckPerson{

public static void printPersons(List<Person> roster, CheckPerson tester) {
for (Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}

@Override
public boolean test(Person p) {
return p.gender == Person.Sex.FEMALE && p.getAge() >= 0 && p.getAge() <= 80;
}


}
Loading