Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- public class test {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- List<Dwofr> dwofrList = new ArrayList<>();
- while (true) {
- String inputLine = reader.readLine();
- if ("Once upon a time".equals(inputLine)) {
- break;
- }
- String[] tokens = inputLine.split("[\\s<:>]+");
- String name = tokens[0];
- String color = tokens[1];
- int phisics = Integer.parseInt(tokens[2]);
- boolean noneMatch = dwofrList.stream().noneMatch(d -> d.getName().equals(name) && d.getColor().equals(color));
- if (noneMatch) {
- Dwofr dwofr = new Dwofr(name, color, phisics);
- dwofrList.add(dwofr);
- } else {
- dwofrList.stream().filter(d -> d.getName().equals(name) && d.getColor().equals(color)).findAny().get().setPhisics(phisics);
- }
- }
- dwofrList.stream().sorted((d1, d2) -> {
- int phisicsSort = Integer.compare(d2.getPhisics(), d1.getPhisics());
- if (phisicsSort == 0) {
- if (!d1.getColor().equals(d2.getColor())) {
- int color1 = 0;
- int color2 = 0;
- for (Dwofr dwofr : dwofrList) {
- if (dwofr.getColor().equals(d1.getColor())) {
- color1++;
- } else {
- color2++;
- }
- }
- return Integer.compare(color2, color1);
- } else {
- return 0;
- }
- }
- return phisicsSort;
- }).forEach(System.out::println);
- }
- }
- class Dwofr {
- private String name;
- private String color;
- private int phisics;
- public Dwofr(String name, String color, Integer phisics) {
- this.name = name;
- this.color = color;
- this.phisics = phisics;
- }
- public String getName() {
- return this.name;
- }
- public String getColor() {
- return this.color;
- }
- public Integer getPhisics() {
- return this.phisics;
- }
- public void setPhisics(int phisics) {
- this.phisics = phisics;
- }
- @Override
- public String toString() {
- return String.format("(%s) %s <-> %d", this.color, this.name, this.phisics);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement