Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tried all 3 approaches #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions .idea/Lambdas2-ZCW.iml

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

4 changes: 4 additions & 0 deletions .idea/encodings.xml

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

9 changes: 9 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.

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.

650 changes: 650 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/main/java/lambdaslab/Check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lambdaslab;

public class Check implements CheckPerson {
Integer method;

public Check(Integer method) {
this.method = method;
}

public Boolean test(Person person) {
switch (method) {
case 0:
return checkIfMale(person);
case 1:
return checkIfOlderThan(person, 42);
default:
return false;
}
}

public Boolean checkIfMale(Person p) {
return (p.getGender().equals(Person.Sex.MALE));
}

public Boolean checkIfOlderThan(Person p, Integer age) {
return (p.getAge() >= age);
}

}
5 changes: 5 additions & 0 deletions src/main/java/lambdaslab/CheckPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lambdaslab;

public interface CheckPerson {
Boolean test(Person p);
}
109 changes: 109 additions & 0 deletions src/main/java/lambdaslab/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package lambdaslab;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Person {
public enum Sex {
MALE, FEMALE
}

String name;
LocalDate birthday;
Sex gender;
String emailAddress;
Boolean test = false;
public Person () {}

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

public Person (String name, Sex gender, LocalDate birthday) {
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.emailAddress = name.split(" ")[0] + "@email.com";
}

public Person(String name, Sex gender, Integer yyyy, Integer mm, Integer dd) {
this(name, gender, LocalDate.of(yyyy,mm,dd));
}

public Person(String name, Sex gender, Integer yyyy, Integer mm, Integer dd, Boolean test) {
this(name, gender, LocalDate.of(yyyy,mm,dd));
this.test = test;
}

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

public LocalDate getNow() {
return test ? LocalDate.of(2019, 12, 1) : LocalDate.now();
}

public Integer getAge() {
return getNow().compareTo(birthday);
}



public String toString() {
return (String.format("\nPerson:\n" +
"\tName:\t\t%s\n" +
"\tSex:\t\t%s\n" +
"\tBirthday:\t%s\n" +
"\tEmail:\t\t%s\n",
this.getName(), this.getGender().name(),
this.getBirthday().format(DateTimeFormatter.ofPattern("MM-dd-yyyy")),
this.getEmailAddress()));
}

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

public String getName() {
return this.name;
}

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

public Sex getGender() {
return this.gender;
}

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

public LocalDate getBirthday() {
return this.birthday;
}

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

public void setBirthday(Integer year, Integer month, Integer day) {
this.birthday = LocalDate.of(year, month, day);
}

public String getEmailAddress() {
return this.emailAddress;
}

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



}
24 changes: 24 additions & 0 deletions src/main/java/lambdaslab/PersonWareHouse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package lambdaslab;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static lambdaslab.Person.Sex.*;

public class PersonWareHouse {
List<Person> roster;

public PersonWareHouse () {
roster = new ArrayList<>();
roster.add(new Person("Wes", MALE,1980,5,22, true));
roster.add(new Person("Kai", FEMALE, 1970, 4, 2, true));
roster.add(new Person("Ryan", MALE, 1960, 2,29, true));
roster.add(new Person("Val", FEMALE, 1990, 6,10, true));
roster.add(new Person("Kendra", FEMALE, 2000, 3,30, true));
}

public List<Person> getRoster() {
return this.roster;
}
}
67 changes: 67 additions & 0 deletions src/main/java/lambdaslab/Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package lambdaslab;

import java.time.LocalDate;
import java.util.List;
import java.util.function.Predicate;

public class Search {
public static String printPersonsOlderThan(List<Person> roster, Integer age) {
StringBuilder out = new StringBuilder();
roster.stream().filter(p -> p.getAge() >= age).forEach(person -> {
out.append(person.toString()+"\n");
person.printPerson();
});
return out.toString();
}

public static String printPersonsWithinAgeRange(List<Person> roster, Integer low, Integer high) {
StringBuilder out = new StringBuilder();
roster.stream()
.filter(p -> p.getAge() >= low && p.getAge() <= high)
.forEach(person -> {
out.append(person.toString()+"\n");
person.printPerson();
});
return out.toString();
}

public static String printPersons(List<Person> roster, CheckPerson tester) {
StringBuilder out = new StringBuilder();
roster.stream()
.filter(p -> tester.test(p))
.forEach(p -> {
p.printPerson();
out.append(p.toString());
});
return out.toString();
}

// Example using Local Class
static class CheckAge implements CheckPerson {
@Override
public Boolean test(Person person) {
return (person.getAge() >= 42);
}
}

public static String exWithLocalClass(List<Person> roster) {
return printPersons(roster, new CheckAge());
}

public static String exWithLambdas(List<Person> roster) {
return printPersons(roster, person -> person.getAge() >= 42);
}

public static String exWithAnonClass(List<Person> roster) {
return printPersons(roster, new CheckPerson() {
@Override
public Boolean test(Person p) {
return p.getGender().equals(Person.Sex.MALE);
}
});
}




}
34 changes: 34 additions & 0 deletions src/test/java/lambdaslab/testPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lambdaslab;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDate;

public class testPerson {
Person person;

@Before
public void setUp() {
person = new Person("Wes", Person.Sex.MALE, LocalDate.of(1997, 8, 5), "[email protected]", true);
}

@Test
public void testGetAge() {
Integer actual = person.getAge();
Integer expected = 22;
Assert.assertEquals(expected, actual);
}

@Test
public void testPrintPerson() {
String actual = person.toString();
String expected = "\nPerson:\n" +
"\tName:\t\tWes\n" +
"\tSex:\t\tMALE\n" +
"\tBirthday:\t08-05-1997\n" +
"\tEmail:\t\[email protected]\n";
Assert.assertEquals(expected, actual);
}
}
Loading