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..d6ebd48 --- /dev/null +++ b/.idea/Lambdas2-ZCW.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..bd60617 --- /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..6b5c00d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b8d5b83 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ 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/lambdas2zcw/.idea/.gitignore b/lambdas2zcw/.idea/.gitignore new file mode 100644 index 0000000..0e40fe8 --- /dev/null +++ b/lambdas2zcw/.idea/.gitignore @@ -0,0 +1,3 @@ + +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/lambdas2zcw/.idea/compiler.xml b/lambdas2zcw/.idea/compiler.xml new file mode 100644 index 0000000..bd60617 --- /dev/null +++ b/lambdas2zcw/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lambdas2zcw/.idea/libraries/Maven__com_github_stefanbirkner_system_rules_1_19_0.xml b/lambdas2zcw/.idea/libraries/Maven__com_github_stefanbirkner_system_rules_1_19_0.xml new file mode 100644 index 0000000..638bae2 --- /dev/null +++ b/lambdas2zcw/.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/lambdas2zcw/.idea/libraries/Maven__junit_junit_4_9.xml b/lambdas2zcw/.idea/libraries/Maven__junit_junit_4_9.xml new file mode 100644 index 0000000..c42d5ab --- /dev/null +++ b/lambdas2zcw/.idea/libraries/Maven__junit_junit_4_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lambdas2zcw/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml b/lambdas2zcw/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml new file mode 100644 index 0000000..acdf443 --- /dev/null +++ b/lambdas2zcw/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lambdas2zcw/.idea/misc.xml b/lambdas2zcw/.idea/misc.xml new file mode 100644 index 0000000..d30d09e --- /dev/null +++ b/lambdas2zcw/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/lambdas2zcw/.idea/modules.xml b/lambdas2zcw/.idea/modules.xml new file mode 100644 index 0000000..ff5030b --- /dev/null +++ b/lambdas2zcw/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/lambdas2zcw/.idea/vcs.xml b/lambdas2zcw/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/lambdas2zcw/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lambdas2zcw/lambdas2zcw.iml b/lambdas2zcw/lambdas2zcw.iml new file mode 100644 index 0000000..cebd3c6 --- /dev/null +++ b/lambdas2zcw/lambdas2zcw.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lambdas2zcw/pom.xml b/lambdas2zcw/pom.xml new file mode 100644 index 0000000..cb0ac42 --- /dev/null +++ b/lambdas2zcw/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + com.zipcodewilmington + lambdas2zcw + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + junit + junit + 4.9 + test + + + com.github.stefanbirkner + system-rules + 1.19.0 + test + + + \ No newline at end of file diff --git a/lambdas2zcw/src/main/java/CheckPerson.java b/lambdas2zcw/src/main/java/CheckPerson.java new file mode 100644 index 0000000..882fdb2 --- /dev/null +++ b/lambdas2zcw/src/main/java/CheckPerson.java @@ -0,0 +1,6 @@ +public interface CheckPerson { + //this is search criteria + Boolean test (Person person); + + +} diff --git a/lambdas2zcw/src/main/java/MainClass.java b/lambdas2zcw/src/main/java/MainClass.java new file mode 100644 index 0000000..ffa1823 --- /dev/null +++ b/lambdas2zcw/src/main/java/MainClass.java @@ -0,0 +1,61 @@ +import java.time.LocalDate; +import java.time.Month; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class MainClass { + public static void printPersons( + List roster, CheckPerson tester) { + for (Person p : roster) { + if (tester.test(p)) { + p.printPerson(); + } + } + } + //using lambdas +// public List getMyPeepsOver18(List myPeeps) { +// List myPeepsOver18 = myPeeps.stream().filter(p -> p.getAge() >= 18).collect(Collectors.toList()); +// return myPeepsOver18; +// } + + public static void main(String[] args) { + Person tp, tp1, tp2, tp3, tp4, tp5; + ArrayList myPeeps = new ArrayList<>(); + + tp = new Person("rashmi bapat", LocalDate.of(1994, Month.APRIL, 19), Person.Sex.FEMALE, "rbrashmi1@rediffmail.com"); + //TODO create tp1,tp2,tp3,tp4; + tp1 = new Person("manasi bhide", LocalDate.of(1985, Month.AUGUST, 12), Person.Sex.FEMALE, "manasib123@yahoo.com"); + tp2 = new Person("kedar limaye", LocalDate.of(1983, Month.DECEMBER, 2), Person.Sex.MALE, "klimaye@gmail.com"); + tp3 = new Person("keya param", LocalDate.of(2010, Month.JUNE, 23), Person.Sex.FEMALE, "k23parm@gmx.com"); + tp4 = new Person("raja kotak", LocalDate.of(2015, Month.FEBRUARY, 28), Person.Sex.MALE, "rajaKotak11@yahoo.com"); + tp5 = new Person("risha kapoor", LocalDate.of(2011, Month.JULY, 25), Person.Sex.FEMALE, "risha23@gmail.com"); + myPeeps.add(tp); + //add rest tp1,... + myPeeps.add(tp1); + myPeeps.add(tp2); + myPeeps.add(tp3); + myPeeps.add(tp4); + myPeeps.add(tp5); + + + class checkForOver18 implements CheckPerson { + @Override + public Boolean test(Person person) { + return person.getAge() >= 18; + } + } + printPersons(myPeeps, new checkForOver18()); + + //example using anonymous class + printPersons(myPeeps, new CheckPerson() { + @Override + public Boolean test(Person person) { + return person.getAge() >= 18; + } + }); + + //example using lambda + printPersons(myPeeps, person -> person.getAge() >= 18); + } +} diff --git a/lambdas2zcw/src/main/java/Person.java b/lambdas2zcw/src/main/java/Person.java new file mode 100644 index 0000000..112751f --- /dev/null +++ b/lambdas2zcw/src/main/java/Person.java @@ -0,0 +1,81 @@ +import java.time.LocalDate; +import java.time.Period; +import java.util.ArrayList; +import java.util.List; +import java.time.format.DateTimeFormatter; + +public class Person { + String name; + LocalDate birthday; //format of + Sex gender; + String emailAddress; + ArrayListpeopleList = 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(){ + + } + + public String getName() { + return name; + } + + public LocalDate getBirthday() { + return birthday; + } + + public Sex getGender() { + return gender; + } + + public String getEmailAddress() { + return emailAddress; + } + + public void setName(String name) { + this.name = name; + } + + public void setBirthday(LocalDate birthday) { + this.birthday = birthday; + } + + public void setGender(Sex gender) { + this.gender = gender; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + public void printPerson(){ + System.out.println(String.format("Person Object{Name: %s, Birthday: %s, Gender: %s, EmailAddress: %s}\n", + name, birthday, gender, emailAddress)); + } + public Integer getAge(){ + return Period.between(birthday, LocalDate.now()).getYears(); + } + + + public static void printPersonsOlderThan(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(); + return p; + +} +} diff --git a/lambdas2zcw/src/test/java/MainTest.java b/lambdas2zcw/src/test/java/MainTest.java new file mode 100644 index 0000000..bc266c4 --- /dev/null +++ b/lambdas2zcw/src/test/java/MainTest.java @@ -0,0 +1,100 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.hamcrest.CoreMatchers; +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 tp, tp1, tp2, tp3, tp4, tp5; + ArrayList myPeeps = new ArrayList<>(); + + @Before + public void setUp() throws Exception { + tp = new Person("rashmi bapat", LocalDate.of(1994, Month.APRIL, 19), Person.Sex.FEMALE, "rbrashmi1@rediffmail.com"); + //TODO create tp1,tp2,tp3,tp4; + tp1 = new Person("manasi bhide", LocalDate.of(1985, Month.AUGUST, 12), Person.Sex.FEMALE, "manasib123@yahoo.com"); + tp2 = new Person("kedar limaye", LocalDate.of(1983, Month.DECEMBER, 2), Person.Sex.MALE, "klimaye@gmail.com"); + tp3 = new Person("keya param", LocalDate.of(2010, Month.JUNE, 23), Person.Sex.FEMALE, "k23parm@gmx.com"); + tp4 = new Person("raja kotak", LocalDate.of(2015, Month.FEBRUARY, 28), Person.Sex.MALE, "rajaKotak11@yahoo.com"); + tp5 = new Person("risha kapoor", LocalDate.of(2011, Month.JULY, 25), Person.Sex.FEMALE, "risha23@gmail.com"); + myPeeps.add(tp); + //add rest tp1,... + myPeeps.add(tp1); + myPeeps.add(tp2); + myPeeps.add(tp3); + myPeeps.add(tp4); + myPeeps.add(tp5); + } + + @Test + public void printPersonsLocalClass() { + String expected = "Person Object{ Name: manasi bhide, Birthday: 1994, Month.APRIL, 19, Gender: FEMALE, EmailAddress: manasib123@yahoo.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; + } + } + MainClass.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: manasi bhide, Birthday: 1994, Month.APRIL, 19, Gender: FEMALE, EmailAddress: rbrashmi1@rediffmail.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; + } + } + MainClass.printPersons(myPeeps, new checkForOver18()); + } + + + @Test + public void printPersonsAnonymousClass() { + String expected = "Person Object{ Name: risha kapoor, Birthday: 2011, Month.JULY, 25, Gender: FEMALE, EmailAddress: risha23@gmail.com}"; + ByteArrayOutputStream sink = new ByteArrayOutputStream(); + System.setOut(new PrintStream(sink, true)); + MainClass.printPersons(myPeeps, new CheckPerson() { + @Override // Function we're implementing from the interface below + public Boolean test(Person person) { + return person.getAge() <= 18; + } + }); + //Assert.assertEquals(new String(sink.toByteArray()), containsString(expected)); + + } + + + @Rule + public final SystemOutRule systemOutRule = new SystemOutRule().enableLog(); + + + + @Test + public void printPersonsLambda() { + String expected = "Person Object{Name: rashmi bapat, Birthday: 1994-04-19, Gender: FEMALE, EmailAddress: rbrashmi1@rediffmail.com}" + "\n" + "\n" + "Person Object{Name: manasi bhide, Birthday: 1985-08-12, Gender: FEMALE, EmailAddress: manasib123@yahoo.com}\n\n"; + MainClass.printPersons(myPeeps, person -> person.getAge() >= 18 && person.getGender() == Person.Sex.FEMALE); + Assert.assertEquals(expected, systemOutRule.getLog()); + } + + +} diff --git a/lambdas2zcw/src/test/java/PersonTest.java b/lambdas2zcw/src/test/java/PersonTest.java new file mode 100644 index 0000000..dc010b3 --- /dev/null +++ b/lambdas2zcw/src/test/java/PersonTest.java @@ -0,0 +1,69 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.Month; + +public class PersonTest { + Person person = new Person(); + +@Test +public void personConstructor(){ + String expectedName = ""; + LocalDate expectedBirthday= LocalDate.of(1976, Month.JANUARY, 10); + Person.Sex expectedGender = Person.Sex.MALE; + String expectedEmailAddress =""; + Person person = new Person(expectedName,expectedBirthday,expectedGender,expectedEmailAddress); + person.setName(expectedName); + person.setBirthday(expectedBirthday); + person.setGender(expectedGender); + person.setEmailAddress(expectedEmailAddress); + String actualName = person.getName(); + LocalDate actualBirthday = person.getBirthday(); + Person.Sex actualGender = person.getGender(); + String actualEmailAddress = person.getEmailAddress(); + Assert.assertEquals(expectedName,actualName); + Assert.assertEquals(expectedBirthday,actualBirthday); + Assert.assertEquals(expectedGender,actualGender); + Assert.assertEquals(expectedEmailAddress,actualEmailAddress); +} + @Test + public void setName() { + String expectedName = "Ricardo Montana"; + person.setName("Ricardo Montana"); + String actual = person.getName(); + Assert.assertEquals(expectedName, actual); + } + + @Test + public void setBirthday() { + LocalDate expected = LocalDate.of(1993, Month.APRIL, 22); + person.setBirthday(expected); + LocalDate actual = person.getBirthday(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setGender() { + Person.Sex expected = Person.Sex.FEMALE; + person.setGender(expected); + Person.Sex actual = person.getGender(); + Assert.assertEquals(expected, actual); + } + + @Test + public void setEmailAddress() { + String expectedEmail = "JD455@hotmail.com"; + person.setEmailAddress(expectedEmail); + String actualEmail = person.getEmailAddress(); + Assert.assertEquals(expectedEmail, actualEmail); + } + + @Test + public void print () { + person.printPerson(); + } +} + + diff --git a/lambdas2zcw/target/classes/CheckPerson.class b/lambdas2zcw/target/classes/CheckPerson.class new file mode 100644 index 0000000..e7a0a49 Binary files /dev/null and b/lambdas2zcw/target/classes/CheckPerson.class differ diff --git a/lambdas2zcw/target/classes/MainClass$1.class b/lambdas2zcw/target/classes/MainClass$1.class new file mode 100644 index 0000000..5eceff4 Binary files /dev/null and b/lambdas2zcw/target/classes/MainClass$1.class differ diff --git a/lambdas2zcw/target/classes/MainClass$1checkForOver18.class b/lambdas2zcw/target/classes/MainClass$1checkForOver18.class new file mode 100644 index 0000000..86cc1b1 Binary files /dev/null and b/lambdas2zcw/target/classes/MainClass$1checkForOver18.class differ diff --git a/lambdas2zcw/target/classes/MainClass.class b/lambdas2zcw/target/classes/MainClass.class new file mode 100644 index 0000000..f8dabd8 Binary files /dev/null and b/lambdas2zcw/target/classes/MainClass.class differ diff --git a/lambdas2zcw/target/classes/Person$Sex.class b/lambdas2zcw/target/classes/Person$Sex.class new file mode 100644 index 0000000..d3dbccb Binary files /dev/null and b/lambdas2zcw/target/classes/Person$Sex.class differ diff --git a/lambdas2zcw/target/classes/Person.class b/lambdas2zcw/target/classes/Person.class new file mode 100644 index 0000000..2f68959 Binary files /dev/null and b/lambdas2zcw/target/classes/Person.class differ diff --git a/lambdas2zcw/target/test-classes/MainTest$1.class b/lambdas2zcw/target/test-classes/MainTest$1.class new file mode 100644 index 0000000..639490b Binary files /dev/null and b/lambdas2zcw/target/test-classes/MainTest$1.class differ diff --git a/lambdas2zcw/target/test-classes/MainTest$1checkForOver18.class b/lambdas2zcw/target/test-classes/MainTest$1checkForOver18.class new file mode 100644 index 0000000..e4100b4 Binary files /dev/null and b/lambdas2zcw/target/test-classes/MainTest$1checkForOver18.class differ diff --git a/lambdas2zcw/target/test-classes/MainTest$2checkForOver18.class b/lambdas2zcw/target/test-classes/MainTest$2checkForOver18.class new file mode 100644 index 0000000..b249834 Binary files /dev/null and b/lambdas2zcw/target/test-classes/MainTest$2checkForOver18.class differ diff --git a/lambdas2zcw/target/test-classes/MainTest.class b/lambdas2zcw/target/test-classes/MainTest.class new file mode 100644 index 0000000..9224ccf Binary files /dev/null and b/lambdas2zcw/target/test-classes/MainTest.class differ diff --git a/lambdas2zcw/target/test-classes/PersonTest.class b/lambdas2zcw/target/test-classes/PersonTest.class new file mode 100644 index 0000000..bccd21d Binary files /dev/null and b/lambdas2zcw/target/test-classes/PersonTest.class differ