diff --git a/.idea/$PRODUCT_WORKSPACE_FILE$ b/.idea/$PRODUCT_WORKSPACE_FILE$ new file mode 100644 index 0000000..3733e0d --- /dev/null +++ b/.idea/$PRODUCT_WORKSPACE_FILE$ @@ -0,0 +1,19 @@ + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..0e40fe8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ + +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/Lambdas2-ZCW.iml b/.idea/Lambdas2-ZCW.iml new file mode 100644 index 0000000..f3800b6 --- /dev/null +++ b/.idea/Lambdas2-ZCW.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..67865bb --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_github_stefanbirkner_system_rules_1_19_0.xml b/.idea/libraries/Maven__com_github_stefanbirkner_system_rules_1_19_0.xml new file mode 100644 index 0000000..638bae2 --- /dev/null +++ b/.idea/libraries/Maven__com_github_stefanbirkner_system_rules_1_19_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_9.xml b/.idea/libraries/Maven__junit_junit_4_9.xml new file mode 100644 index 0000000..c42d5ab --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml new file mode 100644 index 0000000..acdf443 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5053471 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..15f1b5c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LambdasLab/java/CheckForSelectiveService.java b/LambdasLab/java/CheckForSelectiveService.java new file mode 100644 index 0000000..47176c0 --- /dev/null +++ b/LambdasLab/java/CheckForSelectiveService.java @@ -0,0 +1,11 @@ +import com.sun.tools.javac.comp.Check; + +public class CheckForSelectiveService implements CheckPerson { + + @Override + public Boolean test(Person person) { + return person.gender == Person.Sex.MALE && + person.getAge() >= 18 && + person.getAge() <= 25; + } +} diff --git a/LambdasLab/java/CheckPerson.java b/LambdasLab/java/CheckPerson.java new file mode 100644 index 0000000..f8004b1 --- /dev/null +++ b/LambdasLab/java/CheckPerson.java @@ -0,0 +1,7 @@ +public interface CheckPerson { +//This is the search criteria + + Boolean test(Person person); + //classes will implement the specific search criteria + +} diff --git a/LambdasLab/java/Main.java b/LambdasLab/java/Main.java new file mode 100644 index 0000000..970642e --- /dev/null +++ b/LambdasLab/java/Main.java @@ -0,0 +1,69 @@ +import java.time.LocalDate; +import java.time.Month; +import java.util.ArrayList; +import java.util.List; + +public class Main { + + + public static void printPersons( + List roster, CheckPerson tester) { + for (Person p : roster) { + if (tester.test(p)) { + p.printPerson(); + } + } + } + + + public static void main(String[] args) { + Person tp, tp1, tp2, tp3, tp4; + ArrayList myPeeps = new ArrayList<>(); + + tp = new Person("Ricardo Jiminez", LocalDate.of(1990, Month.APRIL, 22), Person.Sex.MALE, "RickyBaby@gmail.com"); + tp1 = new Person("Sarah Jiminez", LocalDate.of(1970, Month.AUGUST, 12), Person.Sex.FEMALE, "SarahBaby@gmail.com"); + tp2 = new Person("Martha Stewart", LocalDate.of(1980, Month.JULY, 18), Person.Sex.MALE, "CookBaby@gmail.com"); + tp3 = new Person("JimBob Johnson", LocalDate.of(2001, Month.MARCH, 7), Person.Sex.MALE, "JimBob@gmail.com"); + tp4 = new Person("Ol Man Tucker", LocalDate.of(2003, Month.MAY, 22), Person.Sex.MALE, "Tuck@gmail.com"); + myPeeps.add(tp1); + myPeeps.add(tp2); + myPeeps.add(tp3); + myPeeps.add(tp4); + myPeeps.add(tp); + + + + //Example of a local class + class checkForOver18 implements CheckPerson { + @Override //Function we're implementing from the Interface below + public Boolean test(Person person) { + return person.getAge() >= 18; + } + } + printPersons(myPeeps, new checkForOver18()); + + + //Example using an anonymous class + printPersons(myPeeps, new CheckPerson() { + @Override // Function we're implementing from the interface below + public Boolean test(Person person) { + return person.getAge() >= 18; + } + }); + + + //Example using an lambdas + printPersons(myPeeps, person -> person.getAge() >= 45); + + } + + + + + + + + + +} + diff --git a/LambdasLab/java/Person.java b/LambdasLab/java/Person.java new file mode 100644 index 0000000..a0a0f01 --- /dev/null +++ b/LambdasLab/java/Person.java @@ -0,0 +1,105 @@ +import java.time.LocalDate; +import java.time.Period; + +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; + +public class Person { + + String name; + LocalDate birthday; //format of + Sex gender; + String emailAddress; + ArrayList peopleList = new ArrayList<>(); + + + public enum Sex { + MALE, FEMALE + } + + + + public Person(String name, LocalDate birthday, Sex gender, String emailAddress) { + this.name = name; + this.birthday = birthday; + this.gender = gender; + this.emailAddress = emailAddress; + } + + + public Person create (String name, LocalDate birthday, Sex gender, String emailAddress){ + Person createdPerson = new Person(name, birthday, gender, emailAddress); + peopleList.add(createdPerson); + return createdPerson; + } + + public ArrayList getPeopleList () { + return peopleList; + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LocalDate getBirthday() { + return birthday; + } + + public void setBirthday(LocalDate birthday) { + //DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd-yyyy"); + //LocalDate tenthFeb2014 = LocalDate.of(2014, Month.FEBRUARY, 10); + 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 Integer getAge() { + return Period.between(birthday, LocalDate.now()).getYears(); + } + + + public void printPerson() { +// StringBuilder sb = new StringBuilder(); +// sb.append(String.format("Person Object{ Name: %s, Birthday: %s, Gender: %s, EmailAddress: %s}\n", name, birthday, gender, emailAddress)); + System.out.println(String.format("Person Object{ Name: %s, Birthday: %s, Gender: %s, EmailAddress: %s}\n", name, birthday, gender, emailAddress)); + } + + public static void printPersons( + List roster, CheckPerson tester) { + for (Person p : roster) { + if (tester.test(p)) { + p.printPerson(); + } + } + } + + + public int getAge(LocalDate birthday) { + LocalDate today = LocalDate.now(); + Integer p = Period.between(birthday, today).getYears(); + // OR + return p; + } + +} diff --git a/LambdasLab/java/PrintOlderThan.java b/LambdasLab/java/PrintOlderThan.java new file mode 100644 index 0000000..d3e61bd --- /dev/null +++ b/LambdasLab/java/PrintOlderThan.java @@ -0,0 +1,27 @@ +import java.util.ArrayList; +import java.util.List; + +public class PrintOlderThan implements CheckPerson{ + //ArrayList + +// public static void printPersonsOlderThan(List roster, int age) { +// for (Person person : roster) { +// if (person.getAge() >= age) { +// person.printPerson(); +// } +// } +// } +// + + @Override + public Boolean test(Person person) { + if (person.getAge() >= 18) { + person.printPerson(); + return true; + } + return false; + } + + + +} diff --git a/Test/java/MainTest.java b/Test/java/MainTest.java new file mode 100644 index 0000000..5d1b62c --- /dev/null +++ b/Test/java/MainTest.java @@ -0,0 +1,112 @@ + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.time.LocalDate; +import java.time.Month; +import java.util.ArrayList; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; + +public class MainTest { + + Person p, p1, p2, p3, p4, p5; + ArrayList myPeeps = new ArrayList<>(0); + + @Before + public void setUp() throws Exception { + p = new Person("Augustus", LocalDate.of(1990, Month.APRIL, 22), Person.Sex.MALE, "CaeAugus@gmail.com"); + p1 = new Person("Appolonia", LocalDate.of(1970, Month.AUGUST, 12), Person.Sex.FEMALE, "Appy@gmail.com"); + p2 = new Person("Hadrian", LocalDate.of(1980, Month.JULY, 18), Person.Sex.MALE, "Haddy@gmail.com"); + p3 = new Person("Caesar", LocalDate.of(2001, Month.MARCH, 7), Person.Sex.MALE, "Cdogg@gmail.com"); + p4 = new Person("Cleopatra", LocalDate.of(2003, Month.MAY, 22), Person.Sex.FEMALE, "Cleo@gmail.com"); + p5 = new Person("Mark Anthony", LocalDate.of(1993, Month.DECEMBER, 4), Person.Sex.MALE, "markant@yahoo.com"); + myPeeps.add(p); + myPeeps.add(p1); + myPeeps.add(p2); + myPeeps.add(p3); + myPeeps.add(p4); + myPeeps.add(p5); + } + + @Test + public void printPersonsLocalClass() { + String expected = "Person Object{ Name: Appolonia, Birthday: 1970-08-12, Gender: FEMALE, EmailAddress: Appy@gmail.com}"; + ByteArrayOutputStream sink = new ByteArrayOutputStream(); + System.setOut(new PrintStream(sink, true)); + class checkForOver18 implements CheckPerson { + @Override //Function we're implementing from the Interface below + public Boolean test(Person person) { + return person.getGender() == Person.Sex.FEMALE && + person.getAge() > 35; + } + } + Main.printPersons(myPeeps, new checkForOver18()); + assertThat(new String(sink.toByteArray()), containsString(expected)); + } //matches if the output contains this information. + //Works in this case, because it's just the one person, so it does contain this substring. + //Better way below. + + + @Test + public void printPersonsLocalClass2() { + String expected = "Person Object{ Name: Appolonia, Birthday: 1970-08-12, Gender: FEMALE, EmailAddress: Appy@gmail.com}"; + class checkForOver18 implements CheckPerson { + @Override //Function we're implementing from the Interface below + public Boolean test(Person person) { + return person.getGender() == Person.Sex.FEMALE && + person.getAge() > 35; + } + } + Main.printPersons(myPeeps, new checkForOver18()); + } + + + + @Test + public void printPersonsAnonymousClass() { + String expected = "Person Object{ Name: Caesar, Birthday: 2001-03-07, Gender: MALE, EmailAddress: Cdogg@gmail.com}"; + ByteArrayOutputStream sink = new ByteArrayOutputStream(); + System.setOut(new PrintStream(sink, true)); + Main.printPersons(myPeeps, new CheckPerson() { + @Override // Function we're implementing from the interface below + public Boolean test(Person person) { + return person.getAge() <= 18; + } + }); + assertThat(new String(sink.toByteArray()), containsString(expected)); + + } + @Rule + public final SystemOutRule systemOutRule = new SystemOutRule().enableLog(); + + @Test + public void printPersonsAnonymousClass2() { + String expected = "Person Object{ Name: Caesar, Birthday: 2001-03-07, Gender: MALE, EmailAddress: Cdogg@gmail.com}\n\nPerson Object{ Name: Cleopatra, Birthday: 2003-05-22, Gender: FEMALE, EmailAddress: Cleo@gmail.com}\n\n"; + Main.printPersons(myPeeps, new CheckPerson() { + @Override // Function we're implementing from the interface below + public Boolean test(Person person) { + return person.getAge() <= 18; + } + }); + Assert.assertEquals(expected, systemOutRule.getLog()); + } + + + @Test + public void printPersonsLambda () { + String expected = "Person Object{ Name: Appolonia, Birthday: 1970-08-12, Gender: FEMALE, EmailAddress: Appy@gmail.com}\n\n"; + Main.printPersons(myPeeps, person -> person.getAge() >= 45 && person.getGender() == Person.Sex.FEMALE); + Assert.assertEquals(expected, systemOutRule.getLog()); + } + + + } + diff --git a/Test/java/PersonTest.java b/Test/java/PersonTest.java new file mode 100644 index 0000000..8cc2ac1 --- /dev/null +++ b/Test/java/PersonTest.java @@ -0,0 +1,80 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.Month; + +public class PersonTest { + Person tp; + + + @Test + public void testConstructorWithParameters() { + // Given + Integer expectedAge = 37; + String expectedName = "Waldo"; + LocalDate birthdayExpected = LocalDate.parse("07-08-1982"); + Person.Sex expectedSex = Person.Sex.MALE; + String emailExpected = "wheresWaldo@gmail.com"; + + // When + Person person = new Person(expectedName, birthdayExpected, expectedSex, emailExpected); + + // Then + + String actualName = person.getName(); + Integer actualAge = person.getAge(); + Person.Sex actualSex = person.getGender(); + String actualEmail = person.getEmailAddress(); + LocalDate actualBirthday = person.getBirthday(); + + Assert.assertEquals(expectedAge, actualAge); + Assert.assertEquals(expectedName, actualName); + Assert.assertEquals(expectedSex, actualSex); + Assert.assertEquals(emailExpected, actualEmail); + Assert.assertEquals(birthdayExpected, actualBirthday); + } + + @Before + public void setUp() throws Exception { + tp = new Person("John Doe", LocalDate.of(1976, Month.JANUARY, 10), Person.Sex.MALE, "johndoe@gmail.com"); + } + + @Test + public void setName() { + String expectedName = "Ricardo Montana"; + tp.setName("Ricardo Montana"); + String actual = tp.getName(); + Assert.assertEquals(expectedName, actual); + } + + @Test + public void setBirthday() { + LocalDate expected = LocalDate.of(1993, Month.APRIL, 22); + tp.setBirthday(expected); + LocalDate actual = tp.getBirthday(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setGender() { + Person.Sex expected = Person.Sex.FEMALE; + tp.setGender(expected); + Person.Sex actual = tp.getGender(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setEmailAddress() { + String expectedEmail = "JD455@hotmail.com"; + tp.setEmailAddress(expectedEmail); + String actualEmail = tp.getEmailAddress(); + Assert.assertEquals(expectedEmail, actualEmail); + } + + @Test + public void print () { + tp.printPerson(); + } +} diff --git a/Test/java/PrintOlderThanTest.java b/Test/java/PrintOlderThanTest.java new file mode 100644 index 0000000..4181456 --- /dev/null +++ b/Test/java/PrintOlderThanTest.java @@ -0,0 +1,46 @@ +import org.junit.Before; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.Month; +import java.util.ArrayList; + +public class PrintOlderThanTest { + Person tp, tp1, tp2, tp3, tp4; + ArrayList myPeeps = new ArrayList<>(0); + PrintOlderThan myRoster; + + + @Before + public void setUp() throws Exception { + myRoster = new PrintOlderThan(); + tp = new Person("Ricardo Jiminez", LocalDate.of(1990, Month.APRIL, 22), Person.Sex.MALE, "RickyBaby@gmail.com"); + tp1 = new Person("Sarah Jiminez", LocalDate.of(1970, Month.AUGUST, 12), Person.Sex.FEMALE, "SarahBaby@gmail.com"); + tp2 = new Person("Martha Stewart", LocalDate.of(1980, Month.JULY, 18), Person.Sex.MALE, "CookBaby@gmail.com"); + tp3 = new Person("JimBob Johnson", LocalDate.of(2001, Month.MARCH, 7), Person.Sex.MALE, "JimBob@gmail.com"); + tp4 = new Person("Ol Man Tucker", LocalDate.of(2003, Month.MAY, 22), Person.Sex.MALE, "Tuck@gmail.com"); + myPeeps.add(tp1); + myPeeps.add(tp2); + myPeeps.add(tp3); + myPeeps.add(tp4); + myPeeps.add(tp); + } + + @Test + public void printPersonsOlderThan() { + PrintOlderThan testPrintOlderThan = new PrintOlderThan(); +// ArrayList expected = new ArrayList<>(0); +// expected.add(tp3); +// expected.add(tp4); + //myRoster.test(myPeeps, PrintOlderThan); + + + + } + + @Test + public void printPersons() { + } + + +} diff --git a/Test/java/SearchLocalTest.java b/Test/java/SearchLocalTest.java new file mode 100644 index 0000000..accb61f --- /dev/null +++ b/Test/java/SearchLocalTest.java @@ -0,0 +1,2 @@ +public class SearchLocalTest { +} diff --git a/maven/maven.iml b/maven/maven.iml new file mode 100644 index 0000000..ccdb0fd --- /dev/null +++ b/maven/maven.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/maven/pom.xml b/maven/pom.xml new file mode 100644 index 0000000..cc136d7 --- /dev/null +++ b/maven/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + com.zipcodewilmington + lambdas2zcw + 1.0-SNAPSHOT + + + junit + junit + 4.9 + test + + + + com.github.stefanbirkner + system-rules + 1.19.0 + test + + + + \ No newline at end of file