Last active
December 10, 2015 04:48
-
-
Save Fabszn/4383335 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javapuzzle; | |
import com.google.common.base.Joiner; | |
import com.google.common.base.Predicate; | |
import com.google.common.collect.Collections2; | |
import com.google.common.collect.Lists; | |
import java.util.Collection; | |
import java.util.List; | |
/** | |
* My first Java puzzle | |
*/ | |
public class JavaPuzzle { | |
public static void main(String[] args) { | |
Personne p1 = new Personne("Rue des marroniers", "Martin", "Louis"); | |
Personne p2 = new Personne("Rue des marins", "Jean", "Jacques", 24); | |
Personne p3 = new Personne("Rue des enfants", "Michel", "Martin", 16); | |
Personne p4 = new Personne("Rue des enfants", "Cover", "Harry", 19); | |
List<Personne> personnes = Lists.newArrayList(p1, p2, p3, p4); | |
Collection<Personne> filteredPersonne = Collections2.filter(personnes, new Predicate<Personne>() { | |
public boolean apply(Personne personne) { | |
return personne.isMajeur(); | |
} | |
}); | |
System.out.println(Joiner.on(",").join(filteredPersonne).concat(" sont majeurs")); | |
} | |
} | |
class Personne { | |
private static final Integer MAJORITE = 18; | |
private String prenom; | |
private String nom; | |
private String adresse; | |
private Integer age; | |
private Boolean majeur; | |
public Personne(String adresse, String nom, String prenom) { | |
this(adresse, nom, prenom, null); | |
} | |
public Personne(String adresse, String nom, String prenom, Integer age) { | |
this.adresse = adresse; | |
this.age = age; | |
this.nom = nom; | |
this.prenom = prenom; | |
if (age != null) { | |
majeur = MAJORITE.compareTo(age) < 0; | |
} | |
} | |
public String getAdresse() { | |
return adresse; | |
} | |
public Integer getAge() { | |
return age; | |
} | |
public Boolean isMajeur() { | |
return majeur; | |
} | |
public String getNom() { | |
return nom; | |
} | |
public String getPrenom() { | |
return prenom; | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder(); | |
sb.append(nom).append(' '); | |
sb.append(prenom).append(' '); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment