Advertisement
peterbodlev

ShibanaSnowwhite1

Nov 2nd, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class test {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         List<Dwofr> dwofrList = new ArrayList<>();
  12.  
  13.         while (true) {
  14.             String inputLine = reader.readLine();
  15.  
  16.             if ("Once upon a time".equals(inputLine)) {
  17.                 break;
  18.             }
  19.  
  20.             String[] tokens = inputLine.split("[\\s<:>]+");
  21.  
  22.             String name = tokens[0];
  23.             String color = tokens[1];
  24.             int phisics = Integer.parseInt(tokens[2]);
  25.  
  26.             boolean noneMatch = dwofrList.stream().noneMatch(d -> d.getName().equals(name) && d.getColor().equals(color));
  27.  
  28.             if (noneMatch) {
  29.                 Dwofr dwofr = new Dwofr(name, color, phisics);
  30.                 dwofrList.add(dwofr);
  31.             } else {
  32.                 dwofrList.stream().filter(d -> d.getName().equals(name) && d.getColor().equals(color)).findAny().get().setPhisics(phisics);
  33.             }
  34.         }
  35.  
  36.         dwofrList.stream().sorted((d1, d2) -> {
  37.             int phisicsSort = Integer.compare(d2.getPhisics(), d1.getPhisics());
  38.  
  39.             if (phisicsSort == 0) {
  40.  
  41.                 if (!d1.getColor().equals(d2.getColor())) {
  42.                     int color1 = 0;
  43.                     int color2 = 0;
  44.  
  45.                     for (Dwofr dwofr : dwofrList) {
  46.                         if (dwofr.getColor().equals(d1.getColor())) {
  47.                             color1++;
  48.                         } else {
  49.                             color2++;
  50.                         }
  51.                     }
  52.  
  53.                     return Integer.compare(color2, color1);
  54.                 } else {
  55.                     return 0;
  56.                 }
  57.             }
  58.             return phisicsSort;
  59.         }).forEach(System.out::println);
  60.     }
  61. }
  62.  
  63. class Dwofr {
  64.     private String name;
  65.     private String color;
  66.     private int phisics;
  67.  
  68.     public Dwofr(String name, String color, Integer phisics) {
  69.         this.name = name;
  70.         this.color = color;
  71.         this.phisics = phisics;
  72.     }
  73.  
  74.     public String getName() {
  75.         return this.name;
  76.     }
  77.  
  78.     public String getColor() {
  79.         return this.color;
  80.     }
  81.  
  82.     public Integer getPhisics() {
  83.         return this.phisics;
  84.     }
  85.  
  86.     public void setPhisics(int phisics) {
  87.         this.phisics = phisics;
  88.     }
  89.  
  90.     @Override
  91.     public String toString() {
  92.         return String.format("(%s) %s <-> %d", this.color, this.name, this.phisics);
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement