Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ComponentTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- Window window = new Window(name);
- Component prev = null;
- while (true) {
- try {
- int what = scanner.nextInt();
- scanner.nextLine();
- if (what == 0) {
- int position = scanner.nextInt();
- window.addComponent(position, prev);
- } else if (what == 1) {
- String color = scanner.nextLine();
- int weight = scanner.nextInt();
- Component component = new Component(color, weight);
- prev = component;
- } else if (what == 2) {
- String color = scanner.nextLine();
- int weight = scanner.nextInt();
- Component component = new Component(color, weight);
- prev.addComponent(component);
- prev = component;
- } else if (what == 3) {
- String color = scanner.nextLine();
- int weight = scanner.nextInt();
- Component component = new Component(color, weight);
- prev.addComponent(component);
- } else if (what == 4) {
- break;
- }
- } catch (InvalidPositionException e) {
- System.out.println(e.getMessage());
- }
- scanner.nextLine();
- }
- System.out.println("=== ORIGINAL WINDOW ===");
- System.out.println(window);
- int weight = scanner.nextInt();
- scanner.nextLine();
- String color = scanner.nextLine();
- window.changeColor(weight, color);
- System.out.println(String.format("=== CHANGED COLOR (%d, %s) ===", weight, color));
- System.out.println(window);
- int pos1 = scanner.nextInt();
- int pos2 = scanner.nextInt();
- System.out.println(String.format("=== SWITCHED COMPONENTS %d <-> %d ===", pos1, pos2));
- window.swichComponents(pos1, pos2);
- System.out.println(window);
- }
- }
- class InvalidPositionException extends Exception {
- public InvalidPositionException(int position) {
- super(String.format("Invalid position %d, alredy taken!", position));
- }
- }
- class Component implements Comparable<Component> {
- String color;
- int weight;
- TreeSet<Component> components;
- public String getColor() {
- return color;
- }
- public int getWeight() {
- return weight;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public void setWeight(int weight) {
- this.weight = weight;
- }
- public final static Comparator<Component> COMPONENT_COMPARATOR =
- Comparator.comparing(Component::getWeight)
- .thenComparing(Component::getColor);
- Component(String color, int weight) {
- this.color = color;
- this.weight = weight;
- components = new TreeSet<>(COMPONENT_COMPARATOR);
- }
- void addComponent(Component component) {
- components.add(component);
- }
- @Override
- public int compareTo(Component that) {
- return COMPONENT_COMPARATOR.compare(this, that);
- }
- /*
- === ORIGINAL WINDOW ===
- WINDOW FIREFOX
- 1:30:RED
- ---40:GREEN
- ------50:BLUE
- ---------60:CYAN
- ------50:RED
- ---90:MAGENTA
- 2:80:YELLOW
- ---35:WHITE
- */
- @Override
- public String toString() {
- return String.format("%d:%s", weight, color);
- }
- public String toString(String padding) {
- StringBuffer sb = new StringBuffer();
- sb.append(padding);
- sb.append(this.toString() + "\n");
- for (Component component : components) {
- sb.append(component.toString(padding + "---"));
- }
- return sb.toString();
- }
- void changeColors(int weight, String color) {
- if (this.weight < weight)
- this.color = color;
- for (Component component : components) {
- component.changeColors(weight, color);
- }
- }
- }
- class Window {
- String name;
- Map<Integer, Component> window;
- public Window(String name) {
- this.name = name;
- window = new TreeMap<Integer, Component>();
- }
- void addComponent(int position, Component component) throws InvalidPositionException {
- if (window.containsKey(position)) throw new InvalidPositionException(position);
- window.put(position, component);
- }
- /*
- === ORIGINAL WINDOW ===
- WINDOW FIREFOX
- 1:30:RED
- ---40:GREEN
- ------50:BLUE
- ---------60:CYAN
- ------50:RED
- ---90:MAGENTA
- 2:80:YELLOW
- ---35:WHITE
- */
- @Override
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append("WINDOW " + name + "\n");
- for (Integer position : window.keySet()) {
- sb.append(position + ":");
- sb.append(window.get(position).toString(""));
- }
- return sb.toString();
- }
- void changeColor(int weight, String color) {
- window.values().stream()
- .forEach(component -> component.changeColors(weight, color));
- }
- void swichComponents(int pos1, int pos2) {
- Component one = window.get(pos1);
- Component two = window.get(pos2);
- window.put(pos1, two);
- window.put(pos2, one);
- }
- }
- /*
- Да се дефинира класа Component во која се чуваат:
- бојата
- тежината
- колекција од внатрешни компоненти (референци од класата Component).
- Во оваа класа да се дефинираат методите:
- Component(String color, int weight) - конструктор со аргументи боја и тежина
- void addComponent(Component component) - за додавање нова компонента во внатрешната колекција (во оваа колекција компонентите секогаш се подредени според тежината во растечки редослед, ако имаат иста тежина подредени се алфабетски според бојата).
- Да се дефинира класа Window во која се чуваат:
- име
- компоненти.
- Во оваа класа да се дефинираат следните методи:
- Window(String) - конструктор
- void addComponent(int position, Component component) - додава нова компонента на дадена позиција (цел број).
- На секоја позиција може да има само една компонента,
- ако се обидеме да додадеме компонента на зафатена позиција треба да се фрли исклучок од класата InvalidPositionException со порака Invalid position [pos], alredy taken!.
- Компонентите се подредени во растечки редослед според позицијата.
- String toString() - враќа стринг репрезентација на објектот (дадена во пример излезот)
- void changeColor(int weight, String color) - ја менува бојата на сите компоненти со тежина помала од проследената
- void swichComponents(int pos1, int pos2) - ги заменува компонените од проследените позиции.
- */
Advertisement
Add Comment
Please, Sign In to add comment