Advertisement
Martina312

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

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