diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..cab1e50b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea +*.iml +.classpath +.project +.settings +/target/ +/bin/ \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 65514ae9..00000000 --- a/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Workshop Java 8 - -Thèmes abordées : - -* Méthode par défaut (01-default-method) \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..b20359bb --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + devinstitut + java-java17 + 1.0-SNAPSHOT + + + utf-8 + + + + + + + maven-compiler-plugin + + 1.17 + 1.17 + + + + + + + + + junit + junit + 4.12 + + + org.hamcrest + hamcrest-library + 1.3 + + + + \ No newline at end of file diff --git a/src/test/java/java17/data/Account.java b/src/test/java/java17/data/Account.java new file mode 100644 index 00000000..6b770795 --- /dev/null +++ b/src/test/java/java17/data/Account.java @@ -0,0 +1,23 @@ +package java17.data; + +public class Account { + + private Person owner; + private Integer balance; + + public Person getOwner() { + return owner; + } + + public void setOwner(Person owner) { + this.owner = owner; + } + + public Integer getBalance() { + return balance; + } + + public void setBalance(Integer balance) { + this.balance = balance; + } +} diff --git a/src/test/java/java17/data/Data.java b/src/test/java/java17/data/Data.java new file mode 100644 index 00000000..64c25a2c --- /dev/null +++ b/src/test/java/java17/data/Data.java @@ -0,0 +1,15 @@ +package java17.data; + + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Data { + + public static List buildPersonList(int nb) { + return IntStream.rangeClosed(1,nb) + .mapToObj(i -> new Person("first_" + i, "last_" + i, i, i % 9 == 0 ? "test": "password"+i)) + .collect(Collectors.toList()); + } +} diff --git a/src/test/java/java17/data/Person.java b/src/test/java/java17/data/Person.java new file mode 100644 index 00000000..2ddc7d8b --- /dev/null +++ b/src/test/java/java17/data/Person.java @@ -0,0 +1,51 @@ +package java17.data; + +public class Person { + + private String firstname; + private String lastname; + private Integer age; + private String password; + + public Person() { + } + + public Person(String firstname, String lastname, Integer age, String password) { + this.firstname = firstname; + this.lastname = lastname; + this.age = age; + this.password = password; + } + + public String getFirstname() { + return firstname; + } + + public void setFirstname(String firstname) { + this.firstname = firstname; + } + + public String getLastname() { + return lastname; + } + + public void setLastname(String lastname) { + this.lastname = lastname; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/test/java/java17/ex01/Optional_01_Test.java b/src/test/java/java17/ex01/Optional_01_Test.java new file mode 100644 index 00000000..43d73bcf --- /dev/null +++ b/src/test/java/java17/ex01/Optional_01_Test.java @@ -0,0 +1,53 @@ +package java17.ex01; + + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import org.junit.Test; + +import java17.data.Data; +import java17.data.Person; + +/** + * Exercice 02 - Filter, Map + */ +public class Optional_01_Test { + + class NotPresentException extends RuntimeException { + + } + + @Test + public void test_optional_ifPresent() throws Exception { + + List persons = Data.buildPersonList(10); + + // TODO rechercher dans la liste ci-dessus la 1ère personne ayant 18 ans + // TODO utiliser la méthode "findFirst" + Optional optPerson = persons.stream().findFirst(); + assertThat(optPerson.isPresent(), is(true)); + + // TODO afficher la personne en question si l'optional contient une personne + System.out.println(optPerson.get().getFirstname()); + } + + @Test(expected=NotPresentException.class) + public void test_optional_notPresent() throws Exception { + List persons = Data.buildPersonList(50); + + // TODO rechercher dans la liste ci-dessus la 1ère personne ayant 75 ans + // TODO utiliser la méthode "findFirst" + Optional optPerson = persons.stream().filter(person->person.getAge()==75).findFirst(); + assertThat(optPerson.isPresent(), is(false)); + + Person person = optPerson.orElseThrow(NotPresentException::new); + // TODO si la personne n'existe pas, jeter une exception NotPresentException + // TODO utiliser la méthode "orElseThrow" + + } +}