Advertisement
Guest User

Untitled

a guest
Jun 14th, 2020
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.*;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(
  12.                 new InputStreamReader(System.in, StandardCharsets.UTF_8));
  13.  
  14.         String personId = reader.readLine();
  15.  
  16.         List<Person> people = new ArrayList<>();
  17.         Map<String, List<String>> parentChildren = new LinkedHashMap<>();
  18.  
  19.         reader.lines()
  20.                 .takeWhile(line -> !"End".equals(line))
  21.                 .forEach(line -> {
  22.                     if (line.contains(" - ")) {
  23.                         String[] tokens = line.split(" - ");
  24.                         String parentId = tokens[0];
  25.                         String childId = tokens[1];
  26.                         parentChildren.putIfAbsent(parentId, new ArrayList<>());
  27.                         parentChildren.get(parentId).add(childId);
  28.                     } else {
  29.                         String[] tokens = line.split("\\s+");
  30.                         String name = tokens[0] + " " + tokens[1];
  31.                         String birthDate = tokens[2];
  32.                         people.add(new Person(name, birthDate));
  33.                     }
  34.                 });
  35.  
  36.         parentChildren.forEach((parentId, children) -> {
  37.             Person parent = findPerson(people, parentId);
  38.  
  39.             children.stream()
  40.                     .map(childId -> findPerson(people, childId))
  41.                     .forEach(parent::addChild);
  42.         });
  43.  
  44.         Person forPerson = findPerson(people, personId);
  45.  
  46.         System.out.println(getFamilyTreeFor(forPerson));
  47.     }
  48.  
  49.     private static Person findPerson(List<Person> people, String personId) {
  50.         return people.stream()
  51.                 .filter(person -> person.getBirthDate().equals(personId) || person.getName().equals(personId))
  52.                 .findFirst()
  53.                 .orElseThrow();
  54.     }
  55.  
  56.     public static String getFamilyTreeFor(Person person) {
  57.         return new StringBuilder()
  58.                 .append(person.toString()).append(System.lineSeparator())
  59.                 .append("Parents:").append(System.lineSeparator())
  60.                 .append(person.getParents().stream().map(Person::toString)
  61.                         .collect(Collectors.joining(System.lineSeparator())))
  62.                 .append(person.getParents().isEmpty() ? "" : System.lineSeparator())
  63.                 .append("Children:").append(System.lineSeparator())
  64.                 .append(person.getChildren().stream().map(Person::toString)
  65.                         .collect(Collectors.joining(System.lineSeparator())))
  66.                 .toString();
  67.     }
  68.  
  69.     private static final class Person {
  70.         private final String name;
  71.         private final String birthDate;
  72.         private final List<Person> parents = new ArrayList<>();
  73.         private final List<Person> children = new ArrayList<>();
  74.  
  75.         public Person(String name, String birthDate) {
  76.             this.name = name;
  77.             this.birthDate = birthDate;
  78.         }
  79.  
  80.         @Override
  81.         public String toString() {
  82.             return name + " " + birthDate;
  83.         }
  84.  
  85.         public String getName() {
  86.             return name;
  87.         }
  88.  
  89.         public void addChild(Person child) {
  90.             children.add(child);
  91.             child.parents.add(this);
  92.         }
  93.  
  94.         public String getBirthDate() {
  95.             return birthDate;
  96.         }
  97.  
  98.         public List<Person> getParents() {
  99.             return Collections.unmodifiableList(parents);
  100.         }
  101.  
  102.         public List<Person> getChildren() {
  103.             return Collections.unmodifiableList(children);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement