From 11656a26fd435847bd87ebe4ba83b230ff03aa8d Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Tue, 30 Nov 2021 23:26:53 -0500 Subject: [PATCH] Completed lab --- README.md | 24 +++--- .../com/zipcode/lamda/AnonymousClass.java | 28 +++++++ src/main/com/zipcode/lamda/CheckPerson.java | 5 ++ src/main/com/zipcode/lamda/LambdaExp.java | 12 +++ src/main/com/zipcode/lamda/Local.java | 24 ++++++ src/main/com/zipcode/lamda/Person.java | 79 +++++++++++++++++++ 6 files changed, 160 insertions(+), 12 deletions(-) create mode 100644 src/main/com/zipcode/lamda/AnonymousClass.java create mode 100644 src/main/com/zipcode/lamda/CheckPerson.java create mode 100644 src/main/com/zipcode/lamda/LambdaExp.java create mode 100644 src/main/com/zipcode/lamda/Local.java create mode 100644 src/main/com/zipcode/lamda/Person.java diff --git a/README.md b/README.md index aed6d4d..ed30661 100644 --- a/README.md +++ b/README.md @@ -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 com.zipcode.lamda.Person class: ``` -public class Person { +public class com.zipcode.lamda.Person { public enum Sex { MALE, FEMALE @@ -25,14 +25,14 @@ public class Person { } ``` -Suppose that the members of your social networking application are stored in a `List` instance. +Suppose that the members of your social networking application are stored in a `List` 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 roster, int age) { - for (Person p : roster) { +public static void printPersonsOlderThan(List roster, int age) { + for (com.zipcode.lamda.Person p : roster) { if (p.getAge() >= age) { p.printPerson(); } @@ -44,15 +44,15 @@ The following method is more generic than printPersonsOlderThan; it prints membe ``` public static void printPersonsWithinAgeRange( - List roster, int low, int high) { - for (Person p : roster) { + List roster, int low, int high) { + for (com.zipcode.lamda.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 com.zipcode.lamda.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 @@ -60,8 +60,8 @@ The following method prints members that match search criteria that you specify: ``` public static void printPersons( - List roster, CheckPerson tester) { - for (Person p : roster) { + List roster, CheckPerson tester) { + for (com.zipcode.lamda.Person p : roster) { if (tester.test(p)) { p.printPerson(); } @@ -69,13 +69,13 @@ public static void printPersons( } ``` -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 com.zipcode.lamda.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 com.zipcode.lamda.Person instance. To specify the search criteria, you implement the CheckPerson interface: ``` interface CheckPerson { - boolean test(Person p); + boolean test(com.zipcode.lamda.Person p); } ``` diff --git a/src/main/com/zipcode/lamda/AnonymousClass.java b/src/main/com/zipcode/lamda/AnonymousClass.java new file mode 100644 index 0000000..5f2410e --- /dev/null +++ b/src/main/com/zipcode/lamda/AnonymousClass.java @@ -0,0 +1,28 @@ +package com.zipcode.lamda; + +import java.util.ArrayList; +import java.util.List; + +public class AnonymousClass { + public ArrayList printPersons(List roster) { + ArrayList temp = new ArrayList<>(); + + CheckPerson checkP = new CheckPerson() { + @Override + public boolean test(Person p) { + if (p.getAge() > 10 && p.gender == Person.Sex.FEMALE) { + return true; + } + return false; + } + }; + + for (Person p : roster) { + if (checkP.test(p)) { + temp.add(p); + System.out.println(p.printPerson()); + } + } + return temp; + } +} diff --git a/src/main/com/zipcode/lamda/CheckPerson.java b/src/main/com/zipcode/lamda/CheckPerson.java new file mode 100644 index 0000000..c406203 --- /dev/null +++ b/src/main/com/zipcode/lamda/CheckPerson.java @@ -0,0 +1,5 @@ +package com.zipcode.lamda; + +public interface CheckPerson { + boolean test(Person p); +} diff --git a/src/main/com/zipcode/lamda/LambdaExp.java b/src/main/com/zipcode/lamda/LambdaExp.java new file mode 100644 index 0000000..8d02ae3 --- /dev/null +++ b/src/main/com/zipcode/lamda/LambdaExp.java @@ -0,0 +1,12 @@ +package com.zipcode.lamda; + +public class LambdaExp { + public static CheckPerson tester; + //Static block is used for initializing the static variables + static { tester = (p)->p.getAge() > 4 && p.getGender()== Person.Sex.FEMALE;} + + public CheckPerson getLambda(){ + return tester; + } + +} diff --git a/src/main/com/zipcode/lamda/Local.java b/src/main/com/zipcode/lamda/Local.java new file mode 100644 index 0000000..43da686 --- /dev/null +++ b/src/main/com/zipcode/lamda/Local.java @@ -0,0 +1,24 @@ +package com.zipcode.lamda; + +import java.util.List; + +public class Local implements CheckPerson{ + public static void printPersons( + List roster, CheckPerson tester) { + for (Person p : roster) { + if (tester.test(p)) { + p.printPerson(); + } + } + } + + @Override + public boolean test(Person p) { + if(p.gender == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 30) { + return true; + } else { + return false; + } + } + +} diff --git a/src/main/com/zipcode/lamda/Person.java b/src/main/com/zipcode/lamda/Person.java new file mode 100644 index 0000000..85ddddb --- /dev/null +++ b/src/main/com/zipcode/lamda/Person.java @@ -0,0 +1,79 @@ +package com.zipcode.lamda; + +import java.time.LocalDate; +import java.util.List; + +public class Person { + + 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 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 today = LocalDate.now(); + return today.compareTo(this.birthday); + } + + public void printPersonsOlderThan(List roster, int age){ + for(Person p: roster){ + if (p.getAge() >= age){ + p.printPerson(); + } + } + } + + public boolean printPerson(){ + if (getAge() > 10 && gender == Person.Sex.FEMALE) { + return true; + } + return false; + + + } + +}