Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorted Social App #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/Lambdas2-ZCW.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_10.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>Lambdas2-ZCW</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
6 changes: 6 additions & 0 deletions src/main/java/CheckFemale.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public interface CheckFemale extends CheckPerson{
@Override
default boolean test(Person p) {
return p.getGender()== Person.Sex.FEMALE;
}
}
6 changes: 6 additions & 0 deletions src/main/java/CheckMale.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class CheckMale implements CheckPerson{
@Override
public boolean test(Person p) {
return p.getGender() == Person.Sex.MALE ;
}
}
6 changes: 6 additions & 0 deletions src/main/java/CheckMaleOlderThan21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class CheckMaleOlderThan21 implements CheckPerson{
@Override
public boolean test(Person p) {
return p.getGender() == Person.Sex.MALE && p.getAge()>21;
}
}
8 changes: 8 additions & 0 deletions src/main/java/CheckPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public interface CheckPerson {

boolean test(Person p);

// boolean testFemale();


}
80 changes: 80 additions & 0 deletions src/main/java/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
import java.util.function.Predicate;

public class Person {

String name;
LocalDate birthday;
Sex gender;
String emailAddress;

public Person(String name, Sex sex, LocalDate birthday, String emailAddress) {
this.name = name;
this.gender= sex;
this.birthday = birthday;
this.emailAddress = emailAddress;
}

public enum Sex {
MALE, FEMALE
}



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();
Period diff = Period.between(birthday, today);
return diff.getYears();
}

public void printPerson() {
String person = this.toString();
System.out.println(person);
}



@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", birthday=" + birthday +
", gender=" + gender +
", emailAddress='" + emailAddress + '\'' +
'}';
}
}
32 changes: 32 additions & 0 deletions src/main/java/PersonBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.time.LocalDate;

public class PersonBuilder {
private String name;
private Person.Sex sex;
private LocalDate birthday = LocalDate.of(1990, 1, 1);
private String emailAddress;

public PersonBuilder setName(String name) {
this.name = name;
return this;
}

public PersonBuilder setSex(Person.Sex sex) {
this.sex = sex;
return this;
}

public PersonBuilder setBirthday(LocalDate birthday) {
this.birthday = birthday;
return this;
}

public PersonBuilder setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
return this;
}

public Person createPerson() {
return new Person(name, sex, birthday, emailAddress);
}
}
80 changes: 80 additions & 0 deletions src/main/java/SocialApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class SocialApp{

List<Person> roster = new ArrayList<>();


public List<Person> getList() {
return roster;
}


interface Checker extends CheckPerson {

boolean test(Person p);


static boolean testMale(Person p){
return p.getGender() == Person.Sex.MALE;
}

static boolean testFemale(Person p){
return p.getGender()== Person.Sex.FEMALE;
}
}



public void addUser(Person user){
roster.add(user);
}

public static void printPersonsOlderThan(List<Person> roster, int age) {
Predicate<Person> checkAge = p -> p.getAge() > age;
roster.stream().filter(checkAge).forEach(Person::printPerson);
}

public static void printPersonsWithinAgeRange(List<Person> roster, int low, int high) {
Predicate<Person> range = p -> low <= p.getAge() && p.getAge() < high;
roster.stream().filter(range).forEach(Person::printPerson);
}

public static void printPersonsFemale( List<Person> roster
// List<Person> roster, Checker tester
) {
Predicate<Person> checkFem = Checker::testFemale;
roster.stream().filter(checkFem).forEach(Person::printPerson);
}

public static void printMales21AndUp(List<Person> roster){
Predicate<Person> checkMale = Checker::testMale;
Predicate<Person> checkAge = p-> p.getAge() > 21;

roster.stream().filter(checkMale).filter(checkAge).forEach(Person::printPerson);
}

public static void printFemales21AndUp(List<Person> roster){
Predicate<Person> checkMale = Checker::testFemale;
Predicate<Person> checkAge = p-> p.getAge() > 21;

roster.stream().filter(checkMale).filter(checkAge).forEach(Person::printPerson);
}

public static void printFemalesBetweenAges(List<Person> roster, int low, int high){
Predicate<Person> checkFem = Checker::testFemale;
Predicate<Person> range = p -> low <= p.getAge() && p.getAge() < high;
roster.stream().filter(checkFem).filter(range).forEach(Person::printPerson);

}

public static void printMalesBetweenAges(List<Person> roster, int low, int high){
Predicate<Person> checkMale = Checker::testMale;
Predicate<Person> range = p -> low <= p.getAge() && p.getAge() < high;
roster.stream().filter(checkMale).filter(range).forEach(Person::printPerson);

}

}
Loading