From 2bca4a643095e4dce237bfcd728a1ea777547a65 Mon Sep 17 00:00:00 2001 From: David Trombello Date: Fri, 22 Nov 2019 21:27:30 -0500 Subject: [PATCH 1/4] all classes and test classes made --- LambdasLab/java/Person.java | 2 ++ LambdasLab/java/SearchAnonymous.java | 2 ++ LambdasLab/java/SearchLambdas.java | 2 ++ LambdasLab/java/SearchLocal.java | 2 ++ Test/java/PersonTest.java | 3 +++ Test/java/SearchAnonymousTest.java | 2 ++ Test/java/SearchLambdasTest.java | 2 ++ Test/java/SearchLocalTest.java | 2 ++ 8 files changed, 17 insertions(+) create mode 100644 LambdasLab/java/Person.java create mode 100644 LambdasLab/java/SearchAnonymous.java create mode 100644 LambdasLab/java/SearchLambdas.java create mode 100644 LambdasLab/java/SearchLocal.java create mode 100644 Test/java/PersonTest.java create mode 100644 Test/java/SearchAnonymousTest.java create mode 100644 Test/java/SearchLambdasTest.java create mode 100644 Test/java/SearchLocalTest.java diff --git a/LambdasLab/java/Person.java b/LambdasLab/java/Person.java new file mode 100644 index 0000000..425d40e --- /dev/null +++ b/LambdasLab/java/Person.java @@ -0,0 +1,2 @@ +public class Person { +} diff --git a/LambdasLab/java/SearchAnonymous.java b/LambdasLab/java/SearchAnonymous.java new file mode 100644 index 0000000..a8deae0 --- /dev/null +++ b/LambdasLab/java/SearchAnonymous.java @@ -0,0 +1,2 @@ +public class SearchAnonymous { +} diff --git a/LambdasLab/java/SearchLambdas.java b/LambdasLab/java/SearchLambdas.java new file mode 100644 index 0000000..ad9d2c8 --- /dev/null +++ b/LambdasLab/java/SearchLambdas.java @@ -0,0 +1,2 @@ +public class SearchLambdas { +} diff --git a/LambdasLab/java/SearchLocal.java b/LambdasLab/java/SearchLocal.java new file mode 100644 index 0000000..227f773 --- /dev/null +++ b/LambdasLab/java/SearchLocal.java @@ -0,0 +1,2 @@ +public class SearchLocal { +} diff --git a/Test/java/PersonTest.java b/Test/java/PersonTest.java new file mode 100644 index 0000000..7efb09d --- /dev/null +++ b/Test/java/PersonTest.java @@ -0,0 +1,3 @@ +public class PersonTest { + +} diff --git a/Test/java/SearchAnonymousTest.java b/Test/java/SearchAnonymousTest.java new file mode 100644 index 0000000..f8339c2 --- /dev/null +++ b/Test/java/SearchAnonymousTest.java @@ -0,0 +1,2 @@ +public class SearchAnonymousTest { +} diff --git a/Test/java/SearchLambdasTest.java b/Test/java/SearchLambdasTest.java new file mode 100644 index 0000000..6bf5eae --- /dev/null +++ b/Test/java/SearchLambdasTest.java @@ -0,0 +1,2 @@ +public class SearchLambdasTest { +} 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 { +} From 211fd2e985aba74f5828e7597bac9a7289ab0909 Mon Sep 17 00:00:00 2001 From: David Trombello Date: Tue, 26 Nov 2019 12:05:11 -0500 Subject: [PATCH 2/4] updating Person and test class --- LambdasLab/java/Person.java | 62 +++++++++++++++++++++++++++++++++++++ Test/java/PersonTest.java | 32 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/LambdasLab/java/Person.java b/LambdasLab/java/Person.java index 425d40e..ad2f883 100644 --- a/LambdasLab/java/Person.java +++ b/LambdasLab/java/Person.java @@ -1,2 +1,64 @@ +import java.time.LocalDate; +import java.time.Period; + public class Person { + + String name; + LocalDate birthday; + Sex gender; + String emailAddress; + + 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 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(LocalDate birthday) { + LocalDate today = LocalDate.now(); + Integer p = Period.between(birthday, today).getYears(); + // OR + return p; + } + + public void printPerson() { + // ... + } } diff --git a/Test/java/PersonTest.java b/Test/java/PersonTest.java index 7efb09d..7409078 100644 --- a/Test/java/PersonTest.java +++ b/Test/java/PersonTest.java @@ -1,3 +1,35 @@ +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; + + @Before + public void setUp() throws Exception { + tp = new Person ("John Doe", LocalDate.of(1976, Month.JANUARY, 10), Person.Sex.MALE, "johnDoe@hotmail.com"); + } + + @Test + void setName() { + String expectedName = "Pete Smith"; + tp.setName("Pete Smith"); + String actual = tp.getName(); + Assert.assertEquals(expectedName, actual); + } + + @Test + void setBirthday() { + } + + @Test + void setGender() { + } + @Test + void setEmailAddress() { + } } From d34f3b9039397a7891683580e7683fad6016acb2 Mon Sep 17 00:00:00 2001 From: David Trombello Date: Tue, 26 Nov 2019 18:28:50 -0500 Subject: [PATCH 3/4] local class created --- .idea/$PRODUCT_WORKSPACE_FILE$ | 19 +++ .idea/.gitignore | 3 + .idea/Lambdas2-ZCW.iml | 22 ++++ .idea/misc.xml | 9 ++ .idea/modules.xml | 8 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + LambdasLab/java/CheckForSelectiveService.java | 11 ++ LambdasLab/java/CheckPerson.java | 7 + LambdasLab/java/Main.java | 59 +++++++++ LambdasLab/java/Person.java | 87 ++++++++++++ LambdasLab/java/PrintOlderThan.java | 27 ++++ LambdasLab/java/SearchLocal.java | 13 +- Test/java/PersonTest.java | 77 +++++++++++ Test/java/PrintOlderThanTest.java | 46 +++++++ 15 files changed, 517 insertions(+), 1 deletion(-) create mode 100644 .idea/$PRODUCT_WORKSPACE_FILE$ create mode 100644 .idea/.gitignore create mode 100644 .idea/Lambdas2-ZCW.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 LambdasLab/java/CheckForSelectiveService.java create mode 100644 LambdasLab/java/CheckPerson.java create mode 100644 LambdasLab/java/Main.java create mode 100644 LambdasLab/java/PrintOlderThan.java create mode 100644 Test/java/PrintOlderThanTest.java 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..972471f --- /dev/null +++ b/.idea/Lambdas2-ZCW.iml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4fc6312 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e080e35 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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..b10c4ca --- /dev/null +++ b/LambdasLab/java/Main.java @@ -0,0 +1,59 @@ +import java.time.LocalDate; +import java.time.Month; +import java.util.ArrayList; +import java.util.List; + +public class Main { + + private ArrayList roster; + + 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 + public Boolean test(Person person) { + return person.getAge() >= 18; + } + } + printPersons(myPeeps, new checkForOver18()); + + + + } + + + + + + + + + +} + diff --git a/LambdasLab/java/Person.java b/LambdasLab/java/Person.java index 425d40e..724f264 100644 --- a/LambdasLab/java/Person.java +++ b/LambdasLab/java/Person.java @@ -1,2 +1,89 @@ +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<>(); + Person person; + + 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() { + 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(); + } + } + } + } 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/LambdasLab/java/SearchLocal.java b/LambdasLab/java/SearchLocal.java index 227f773..ad9df20 100644 --- a/LambdasLab/java/SearchLocal.java +++ b/LambdasLab/java/SearchLocal.java @@ -1,2 +1,13 @@ -public class SearchLocal { +import java.util.List; + +public class SearchLocal implements CheckPerson{ + + @Override + public Boolean test(Person person) { + + + return null; + } + + } diff --git a/Test/java/PersonTest.java b/Test/java/PersonTest.java index 7efb09d..330b9fa 100644 --- a/Test/java/PersonTest.java +++ b/Test/java/PersonTest.java @@ -1,3 +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() { + } + + +} From 38b40f22ee6f72a772556031a95d2b2a0f149c8b Mon Sep 17 00:00:00 2001 From: David Trombello Date: Wed, 27 Nov 2019 16:26:30 -0500 Subject: [PATCH 4/4] all classes created and tests passing --- .idea/Lambdas2-ZCW.iml | 1 + .idea/compiler.xml | 16 +++ ...thub_stefanbirkner_system_rules_1_19_0.xml | 13 ++ .idea/libraries/Maven__junit_junit_4_9.xml | 13 ++ .../Maven__org_hamcrest_hamcrest_core_1_1.xml | 13 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 1 + LambdasLab/java/Main.java | 16 ++- LambdasLab/java/Person.java | 2 + LambdasLab/java/SearchAnonymous.java | 2 - LambdasLab/java/SearchLambdas.java | 2 - LambdasLab/java/SearchLocal.java | 13 -- Test/java/MainTest.java | 112 ++++++++++++++++++ Test/java/SearchAnonymousTest.java | 2 - Test/java/SearchLambdasTest.java | 2 - maven/maven.iml | 18 +++ maven/pom.xml | 26 ++++ 17 files changed, 235 insertions(+), 24 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 .idea/libraries/Maven__com_github_stefanbirkner_system_rules_1_19_0.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_9.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml delete mode 100644 LambdasLab/java/SearchAnonymous.java delete mode 100644 LambdasLab/java/SearchLambdas.java delete mode 100644 LambdasLab/java/SearchLocal.java create mode 100644 Test/java/MainTest.java delete mode 100644 Test/java/SearchAnonymousTest.java delete mode 100644 Test/java/SearchLambdasTest.java create mode 100644 maven/maven.iml create mode 100644 maven/pom.xml diff --git a/.idea/Lambdas2-ZCW.iml b/.idea/Lambdas2-ZCW.iml index 972471f..f3800b6 100644 --- a/.idea/Lambdas2-ZCW.iml +++ b/.idea/Lambdas2-ZCW.iml @@ -18,5 +18,6 @@ + \ 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 index 4fc6312..5053471 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,6 +3,13 @@ + + + diff --git a/.idea/modules.xml b/.idea/modules.xml index e080e35..15f1b5c 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/LambdasLab/java/Main.java b/LambdasLab/java/Main.java index b10c4ca..970642e 100644 --- a/LambdasLab/java/Main.java +++ b/LambdasLab/java/Main.java @@ -5,7 +5,6 @@ public class Main { - private ArrayList roster; public static void printPersons( List roster, CheckPerson tester) { @@ -20,6 +19,7 @@ public static void printPersons( 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"); @@ -34,9 +34,8 @@ public static void main(String[] args) { //Example of a local class - class checkForOver18 implements CheckPerson { - @Override + @Override //Function we're implementing from the Interface below public Boolean test(Person person) { return person.getAge() >= 18; } @@ -44,6 +43,17 @@ public Boolean test(Person person) { 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 index 45e6e8a..a0a0f01 100644 --- a/LambdasLab/java/Person.java +++ b/LambdasLab/java/Person.java @@ -80,6 +80,8 @@ public Integer getAge() { 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)); } diff --git a/LambdasLab/java/SearchAnonymous.java b/LambdasLab/java/SearchAnonymous.java deleted file mode 100644 index a8deae0..0000000 --- a/LambdasLab/java/SearchAnonymous.java +++ /dev/null @@ -1,2 +0,0 @@ -public class SearchAnonymous { -} diff --git a/LambdasLab/java/SearchLambdas.java b/LambdasLab/java/SearchLambdas.java deleted file mode 100644 index ad9d2c8..0000000 --- a/LambdasLab/java/SearchLambdas.java +++ /dev/null @@ -1,2 +0,0 @@ -public class SearchLambdas { -} diff --git a/LambdasLab/java/SearchLocal.java b/LambdasLab/java/SearchLocal.java deleted file mode 100644 index ad9df20..0000000 --- a/LambdasLab/java/SearchLocal.java +++ /dev/null @@ -1,13 +0,0 @@ -import java.util.List; - -public class SearchLocal implements CheckPerson{ - - @Override - public Boolean test(Person person) { - - - return null; - } - - -} 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/SearchAnonymousTest.java b/Test/java/SearchAnonymousTest.java deleted file mode 100644 index f8339c2..0000000 --- a/Test/java/SearchAnonymousTest.java +++ /dev/null @@ -1,2 +0,0 @@ -public class SearchAnonymousTest { -} diff --git a/Test/java/SearchLambdasTest.java b/Test/java/SearchLambdasTest.java deleted file mode 100644 index 6bf5eae..0000000 --- a/Test/java/SearchLambdasTest.java +++ /dev/null @@ -1,2 +0,0 @@ -public class SearchLambdasTest { -} 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