Advertisement
NenadKocev

[НП] Компоненти

Jan 20th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. class ComponentTest {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String name = scanner.nextLine();
  8.         Window window = new Window(name);
  9.         Component prev = null;
  10.         while (true) {
  11.             try {
  12.                 int what = scanner.nextInt();
  13.                 scanner.nextLine();
  14.                 if (what == 0) {
  15.                     int position = scanner.nextInt();
  16.                     window.addComponent(position, prev);
  17.                 } else if (what == 1) {
  18.                     String color = scanner.nextLine();
  19.                     int weight = scanner.nextInt();
  20.                     Component component = new Component(color, weight);
  21.                     prev = component;
  22.                 } else if (what == 2) {
  23.                     String color = scanner.nextLine();
  24.                     int weight = scanner.nextInt();
  25.                     Component component = new Component(color, weight);
  26.                     prev.addComponent(component);
  27.                     prev = component;
  28.                 } else if (what == 3) {
  29.                     String color = scanner.nextLine();
  30.                     int weight = scanner.nextInt();
  31.                     Component component = new Component(color, weight);
  32.                     prev.addComponent(component);
  33.                 } else if(what == 4) {
  34.                     break;
  35.                 }
  36.  
  37.             } catch (InvalidPositionException e) {
  38.                 System.out.println(e.getMessage());
  39.             }
  40.             scanner.nextLine();
  41.         }
  42.  
  43.         System.out.println("=== ORIGINAL WINDOW ===");
  44.         System.out.println(window);
  45.         int weight = scanner.nextInt();
  46.         scanner.nextLine();
  47.         String color = scanner.nextLine();
  48.         window.changeColor(weight, color);
  49.         System.out.println(String.format("=== CHANGED COLOR (%d, %s) ===", weight, color));
  50.         System.out.println(window);
  51.         int pos1 = scanner.nextInt();
  52.         int pos2 = scanner.nextInt();
  53.         System.out.println(String.format("=== SWITCHED COMPONENTS %d <-> %d ===", pos1, pos2));
  54.         window.swichComponents(pos1, pos2);
  55.         System.out.println(window);
  56.     }
  57. }
  58.  
  59. // вашиот код овде
  60.  
  61. class Component{
  62.     private String color;
  63.     private int weight;
  64.     private Set<Component> components;
  65.  
  66.     public Component(String color, int weght) {
  67.         this.color = color;
  68.         this.weight = weght;
  69.         this.components = new TreeSet<>(Comparator.comparing(Component::getWeght).thenComparing(Component::getColor));
  70.     }
  71.  
  72.     public void addComponent(Component component){
  73.         components.add(component);
  74.     }
  75.  
  76.     public String getColor() {
  77.         return color;
  78.     }
  79.  
  80.     public void changeColor(int weight, String color) {
  81.         if(this.weight < weight)
  82.         this.color = color;
  83.  
  84.         components.forEach(c -> c.changeColor(weight, color));
  85.     }
  86.  
  87.     public int getWeght() {
  88.         return weight;
  89.     }
  90.  
  91.     public void setWeght(int weght) {
  92.         this.weight = weght;
  93.     }
  94.  
  95.     public String toString(String s){
  96.         String s1 = String.format("%d:%s\n", weight, color);
  97.         return components.stream()
  98.                 .collect(
  99.                         () -> new StringBuilder(s).append(s1),
  100.                         (sb, component) -> sb.append(component.toString(s + "---")),
  101.                         (s2, s3) -> s2.append(s3.toString())
  102.                 ).toString();
  103.     }
  104. }
  105.  
  106. class Window{
  107.     private String name;
  108.     private TreeMap<Integer, Component> components;
  109.  
  110.     Window(String name){
  111.         this.name = name;
  112.         components = new TreeMap<>();
  113.     }
  114.  
  115.     public void addComponent(int position, Component component) throws InvalidPositionException{
  116.         if(components.containsKey(position))
  117.             throw new InvalidPositionException("Invalid position " + position + ", alredy taken!");
  118.  
  119.         components.put(position, component);
  120.     }
  121.  
  122.     public void changeColor(int weight, String color){
  123.         components.values().forEach(c -> c.changeColor(weight, color));
  124.     }
  125.  
  126.     public void swichComponents(int pos1, int pos2){
  127.         Component new2 = components.get(pos1);
  128.         Component new1 = components.get(pos2);
  129.         components.put(pos1, new1);
  130.         components.put(pos2, new2);
  131.     }
  132.  
  133.     @Override
  134.     public String toString(){
  135.         return components.entrySet().stream()
  136.                 .collect(
  137.                         () -> new StringBuilder("WINDOW ").append(name).append("\n"),
  138.                         (s, entry) -> s.append(entry.getKey()).append(":").append(entry.getValue().toString("")),
  139.                         (s1, s2) -> s1.append(s2.toString())
  140.                 ).toString();
  141.  
  142.     }
  143. }
  144.  
  145. class InvalidPositionException extends Exception{
  146.     InvalidPositionException(){
  147.         super("InvalidPositionException");
  148.     }
  149.     InvalidPositionException(String s){
  150.         super(s);
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement