diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..38d573b --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/lambdas-cmmsnow.iml b/.idea/lambdas-cmmsnow.iml new file mode 100644 index 0000000..f84d483 --- /dev/null +++ b/.idea/lambdas-cmmsnow.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..a3d1909 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..18e6728 --- /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/README.md b/README.md index aed6d4d..6c97c61 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 java.Person class: ``` -public class Person { +public class java.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 (java.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 (java.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 java.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, java.CheckPerson tester) { + for (java.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 java.Person instance contained in the List parameter roster whether it satisfies the search criteria specified in the java.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 java.Person instance. -To specify the search criteria, you implement the CheckPerson interface: +To specify the search criteria, you implement the java.CheckPerson interface: ``` -interface CheckPerson { - boolean test(Person p); +interface java.CheckPerson { + boolean test(java.Person p); } ``` diff --git a/out/production/lambdas-cmmsnow/CheckPerson.class b/out/production/lambdas-cmmsnow/CheckPerson.class new file mode 100644 index 0000000..faa13df Binary files /dev/null and b/out/production/lambdas-cmmsnow/CheckPerson.class differ diff --git a/out/production/lambdas-cmmsnow/Person$Sex.class b/out/production/lambdas-cmmsnow/Person$Sex.class new file mode 100644 index 0000000..d7f44aa Binary files /dev/null and b/out/production/lambdas-cmmsnow/Person$Sex.class differ diff --git a/out/production/lambdas-cmmsnow/Person.class b/out/production/lambdas-cmmsnow/Person.class new file mode 100644 index 0000000..48bb7a9 Binary files /dev/null and b/out/production/lambdas-cmmsnow/Person.class differ diff --git a/out/production/lambdas-cmmsnow/Search.class b/out/production/lambdas-cmmsnow/Search.class new file mode 100644 index 0000000..04f10ae Binary files /dev/null and b/out/production/lambdas-cmmsnow/Search.class differ diff --git a/out/production/lambdas-cmmsnow/SocialNetworks.class b/out/production/lambdas-cmmsnow/SocialNetworks.class new file mode 100644 index 0000000..6b98f6c Binary files /dev/null and b/out/production/lambdas-cmmsnow/SocialNetworks.class differ diff --git a/out/test/lambdas-cmmsnow/PersonTest.class b/out/test/lambdas-cmmsnow/PersonTest.class new file mode 100644 index 0000000..4111f2f Binary files /dev/null and b/out/test/lambdas-cmmsnow/PersonTest.class differ diff --git a/out/test/lambdas-cmmsnow/SearchTest.class b/out/test/lambdas-cmmsnow/SearchTest.class new file mode 100644 index 0000000..7d92e85 Binary files /dev/null and b/out/test/lambdas-cmmsnow/SearchTest.class differ diff --git a/src/main/java/CheckPerson.java b/src/main/java/CheckPerson.java new file mode 100644 index 0000000..492d16f --- /dev/null +++ b/src/main/java/CheckPerson.java @@ -0,0 +1,5 @@ +public interface CheckPerson { + + boolean test(Person p); + +} diff --git a/src/main/java/Person.java b/src/main/java/Person.java new file mode 100644 index 0000000..a46f0fa --- /dev/null +++ b/src/main/java/Person.java @@ -0,0 +1,93 @@ +import java.time.LocalDate; +import java.time.Period; +import java.util.List; + +public class Person /*implements CheckPerson*/ { + + 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 Person() { + this.name = ""; + this.birthday = LocalDate.now(); + this.gender = null; + this.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() { + return Period.between(birthday, LocalDate.now()).getYears(); + } + + @Override + public String toString() { + return "java.Person{" + + "name='" + name + '\'' + + ", birthday=" + birthday + + ", gender=" + gender + + ", emailAddress='" + emailAddress + '\'' + + '}'; + } + + public void printPerson() { + System.out.println(toString()); + } + +// @Override +// public boolean test(Person p) { +// return p.gender == Person.Sex.FEMALE && p.getAge() >= 0 && p.getAge() <= 80; +// } +// +// public static String printPersons(List roster, CheckPerson tester) { +// for (Person p : roster) { +// if (tester.test(p)) { +// p.printPerson(); +// } +// } +// } + +} diff --git a/src/main/java/Search.java b/src/main/java/Search.java new file mode 100644 index 0000000..220e6e1 --- /dev/null +++ b/src/main/java/Search.java @@ -0,0 +1,19 @@ +import java.util.List; + +public class Search 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) { + return p.gender == Person.Sex.FEMALE && p.getAge() >= 0 && p.getAge() <= 80; + } + + +} diff --git a/src/main/java/SocialNetworks.java b/src/main/java/SocialNetworks.java new file mode 100644 index 0000000..8a299b0 --- /dev/null +++ b/src/main/java/SocialNetworks.java @@ -0,0 +1,18 @@ +import java.util.ArrayList; +import java.util.List; + +public class SocialNetworks { + List network; + + public SocialNetworks(){ + this.network = new ArrayList<>(); + } + + public List getNetwork(){ + return network; + } + + public void add(Person p){ + network.add(p); + } +} diff --git a/src/test/java/PersonTest.java b/src/test/java/PersonTest.java new file mode 100644 index 0000000..485a4bb --- /dev/null +++ b/src/test/java/PersonTest.java @@ -0,0 +1,150 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +public class PersonTest { + Person person1; + Person person2; + Person person3; + List people; + + @Before + public void setup(){ + people = new ArrayList<>(); + person1 = new Person("Bob", LocalDate.of(1980, 1, 25), Person.Sex.MALE, "bob@bob.com"); + person2 = new Person("Sally", LocalDate.of(2000, 10, 5), Person.Sex.FEMALE, "sally@sally.com"); + person3 = new Person("Guy", LocalDate.of(1995, 6, 13), Person.Sex.MALE, "guy@guy.com"); + people.add(person1); + people.add(person2); + people.add(person3); + } + + @Test + public void testConstructor(){ + Assert.assertNotNull(person1); + } + + @Test + public void testNullaryConstructor(){ + Person person = new Person(); + Assert.assertNotNull(person); + } + + @Test + public void testGetName(){ + String expected = "Bob"; + String actual = person1.getName(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetName(){ + String expected = "Todd"; + person1.setName(expected); + String actual = person1.getName(); + Assert.assertEquals(expected, actual); + person1.setName("Bob"); + } + + @Test + public void testGetBirthday(){ + LocalDate expected = LocalDate.of(1980, 1, 25); + LocalDate actual = person1.getBirthday(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetBirthday(){ + LocalDate expected = LocalDate.of(1990, 2, 15); + person1.setBirthday(expected); + LocalDate actual = person1.getBirthday(); + Assert.assertEquals(expected, actual); + + person1.setBirthday(LocalDate.of(1980, 1, 25)); + } + + @Test + public void testGetGender(){ + Person.Sex expected = Person.Sex.MALE; + Person.Sex actual = person1.getGender(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetGender(){ + Person.Sex expected = Person.Sex.FEMALE; + person1.setGender(expected); + Person.Sex actual = person1.getGender(); + Assert.assertEquals(expected, actual); + + person1.setGender(Person.Sex.MALE); + } + + @Test + public void testGetEmail(){ + String expected = "bob@bob.com"; + String actual = person1.getEmailAddress(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetEmail(){ + String expected = "johnny@bob.com"; + person1.setEmailAddress(expected); + String actual = person1.getEmailAddress(); + Assert.assertEquals(expected, actual); + + person1.setEmailAddress("bob@bob.com"); + } + + @Test + public void testGetAge(){ + Person person1 = new Person("Bob", LocalDate.of(1980, 1, 25), Person.Sex.MALE, "bob@bob.com"); + int fakeAge = 10; + int age = person1.getAge(); + Assert.assertNotEquals(fakeAge, age); + } + + @Test + public void testToString(){ + String expected = "java.Person{" + + "name='" + person1.getName() + '\'' + + ", birthday=" + person1.getBirthday() + + ", gender=" + person1.getGender() + + ", emailAddress='" + person1.getEmailAddress() + '\'' + + '}'; + String actual = person1.toString(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testPrintPerson(){ +// Person kyle = new Person(); +// kyle.printPerson(); +// Assert.assertNotNull(kyle); + + String expected = "java.Person{" + + "name='" + person1.getName() + '\'' + + ", birthday=" + person1.getBirthday() + + ", gender=" + person1.getGender() + + ", emailAddress='" + person1.getEmailAddress() + '\'' + + '}'; +// String actual = person1.printPerson(); +// Assert.assertEquals(expected, actual); + } + + @Test + public void testPrintPersons(){ + String expected = "java.Person{" + + "name='" + person2.getName() + '\'' + + ", birthday=" + person2.getBirthday() + + ", gender=" + person2.getGender() + + ", emailAddress='" + person2.getEmailAddress() + '\'' + + '}'; + //String actual = person1.printPersons(people, person1.test()); + } +} diff --git a/src/test/java/SearchTest.java b/src/test/java/SearchTest.java new file mode 100644 index 0000000..af77319 --- /dev/null +++ b/src/test/java/SearchTest.java @@ -0,0 +1,11 @@ +import org.junit.Test; + +public class SearchTest { + SocialNetworks network = new SocialNetworks(); + Search search = new Search(); + + @Test + public void searchTest(){ + Search.printPersons(network.getNetwork(), search); + } +}