Filip_Markoski

[NP] Components

Dec 24th, 2017
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.73 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ComponentTest {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String name = scanner.nextLine();
  7.         Window window = new Window(name);
  8.         Component prev = null;
  9.         while (true) {
  10.             try {
  11.                 int what = scanner.nextInt();
  12.                 scanner.nextLine();
  13.                 if (what == 0) {
  14.                     int position = scanner.nextInt();
  15.                     window.addComponent(position, prev);
  16.                 } else if (what == 1) {
  17.                     String color = scanner.nextLine();
  18.                     int weight = scanner.nextInt();
  19.                     Component component = new Component(color, weight);
  20.                     prev = component;
  21.                 } else if (what == 2) {
  22.                     String color = scanner.nextLine();
  23.                     int weight = scanner.nextInt();
  24.                     Component component = new Component(color, weight);
  25.                     prev.addComponent(component);
  26.                     prev = component;
  27.                 } else if (what == 3) {
  28.                     String color = scanner.nextLine();
  29.                     int weight = scanner.nextInt();
  30.                     Component component = new Component(color, weight);
  31.                     prev.addComponent(component);
  32.                 } else if (what == 4) {
  33.                     break;
  34.                 }
  35.  
  36.             } catch (InvalidPositionException e) {
  37.                 System.out.println(e.getMessage());
  38.             }
  39.             scanner.nextLine();
  40.         }
  41.  
  42.         System.out.println("=== ORIGINAL WINDOW ===");
  43.         System.out.println(window);
  44.         int weight = scanner.nextInt();
  45.         scanner.nextLine();
  46.         String color = scanner.nextLine();
  47.         window.changeColor(weight, color);
  48.         System.out.println(String.format("=== CHANGED COLOR (%d, %s) ===", weight, color));
  49.         System.out.println(window);
  50.         int pos1 = scanner.nextInt();
  51.         int pos2 = scanner.nextInt();
  52.         System.out.println(String.format("=== SWITCHED COMPONENTS %d <-> %d ===", pos1, pos2));
  53.         window.swichComponents(pos1, pos2);
  54.         System.out.println(window);
  55.     }
  56. }
  57.  
  58. class InvalidPositionException extends Exception {
  59.     public InvalidPositionException(int position) {
  60.         super(String.format("Invalid position %d, alredy taken!", position));
  61.     }
  62. }
  63.  
  64. class Component implements Comparable<Component> {
  65.     String color;
  66.     int weight;
  67.     TreeSet<Component> components;
  68.  
  69.     public String getColor() {
  70.         return color;
  71.     }
  72.  
  73.     public int getWeight() {
  74.         return weight;
  75.     }
  76.  
  77.     public void setColor(String color) {
  78.         this.color = color;
  79.     }
  80.  
  81.     public void setWeight(int weight) {
  82.         this.weight = weight;
  83.     }
  84.  
  85.     public final static Comparator<Component> COMPONENT_COMPARATOR =
  86.             Comparator.comparing(Component::getWeight)
  87.                     .thenComparing(Component::getColor);
  88.  
  89.     Component(String color, int weight) {
  90.         this.color = color;
  91.         this.weight = weight;
  92.         components = new TreeSet<>(COMPONENT_COMPARATOR);
  93.     }
  94.  
  95.     void addComponent(Component component) {
  96.         components.add(component);
  97.     }
  98.  
  99.     @Override
  100.     public int compareTo(Component that) {
  101.         return COMPONENT_COMPARATOR.compare(this, that);
  102.     }
  103.  
  104.     /*
  105.     === ORIGINAL WINDOW ===
  106.     WINDOW FIREFOX
  107.     1:30:RED
  108.     ---40:GREEN
  109.     ------50:BLUE
  110.     ---------60:CYAN
  111.     ------50:RED
  112.     ---90:MAGENTA
  113.     2:80:YELLOW
  114.     ---35:WHITE
  115.      */
  116.  
  117.     @Override
  118.     public String toString() {
  119.         return String.format("%d:%s", weight, color);
  120.     }
  121.  
  122.     public String toString(String padding) {
  123.         StringBuffer sb = new StringBuffer();
  124.  
  125.         sb.append(padding);
  126.         sb.append(this.toString() + "\n");
  127.  
  128.         for (Component component : components) {
  129.             sb.append(component.toString(padding + "---"));
  130.         }
  131.         return sb.toString();
  132.     }
  133.  
  134.     void changeColors(int weight, String color) {
  135.  
  136.         if (this.weight < weight)
  137.             this.color = color;
  138.  
  139.         for (Component component : components) {
  140.             component.changeColors(weight, color);
  141.         }
  142.     }
  143. }
  144.  
  145. class Window {
  146.     String name;
  147.     Map<Integer, Component> window;
  148.  
  149.     public Window(String name) {
  150.         this.name = name;
  151.         window = new TreeMap<Integer, Component>();
  152.     }
  153.  
  154.     void addComponent(int position, Component component) throws InvalidPositionException {
  155.         if (window.containsKey(position)) throw new InvalidPositionException(position);
  156.         window.put(position, component);
  157.     }
  158.  
  159.     /*
  160.     === ORIGINAL WINDOW ===
  161.     WINDOW FIREFOX
  162.     1:30:RED
  163.     ---40:GREEN
  164.     ------50:BLUE
  165.     ---------60:CYAN
  166.     ------50:RED
  167.     ---90:MAGENTA
  168.     2:80:YELLOW
  169.     ---35:WHITE
  170.      */
  171.  
  172.     @Override
  173.     public String toString() {
  174.         StringBuffer sb = new StringBuffer();
  175.         sb.append("WINDOW " + name + "\n");
  176.         for (Integer position : window.keySet()) {
  177.             sb.append(position + ":");
  178.             sb.append(window.get(position).toString(""));
  179.         }
  180.         return sb.toString();
  181.     }
  182.  
  183.     void changeColor(int weight, String color) {
  184.         window.values().stream()
  185.                 .forEach(component -> component.changeColors(weight, color));
  186.     }
  187.  
  188.     void swichComponents(int pos1, int pos2) {
  189.         Component one = window.get(pos1);
  190.         Component two = window.get(pos2);
  191.         window.put(pos1, two);
  192.         window.put(pos2, one);
  193.     }
  194. }
  195. /*
  196. Да се дефинира класа Component во која се чуваат:
  197.  
  198. бојата
  199. тежината
  200. колекција од внатрешни компоненти (референци од класата Component).
  201. Во оваа класа да се дефинираат методите:
  202.  
  203. Component(String color, int weight) - конструктор со аргументи боја и тежина
  204. void addComponent(Component component) - за додавање нова компонента во внатрешната колекција (во оваа колекција компонентите секогаш се подредени според тежината во растечки редослед, ако имаат иста тежина подредени се алфабетски според бојата).
  205. Да се дефинира класа Window во која се чуваат:
  206. име
  207. компоненти.
  208. Во оваа класа да се дефинираат следните методи:
  209.  
  210. Window(String) - конструктор
  211. void addComponent(int position, Component component) - додава нова компонента на дадена позиција (цел број).
  212. На секоја позиција може да има само една компонента,
  213. ако се обидеме да додадеме компонента на зафатена позиција треба да се фрли исклучок од класата InvalidPositionException со порака Invalid position [pos], alredy taken!.
  214. Компонентите се подредени во растечки редослед според позицијата.
  215. String toString() - враќа стринг репрезентација на објектот (дадена во пример излезот)
  216. void changeColor(int weight, String color) - ја менува бојата на сите компоненти со тежина помала од проследената
  217. void swichComponents(int pos1, int pos2) - ги заменува компонените од проследените позиции.
  218.  */
Advertisement
Add Comment
Please, Sign In to add comment