Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class InvalidPositionException extends Exception{
- public InvalidPositionException(String message) {
- super(message);
- }
- }
- class Component{
- private String color;
- private int weight;
- private Set<Component> components;
- public Component(String color, int weight) {
- this.color = color;
- this.weight = weight;
- this.components = new TreeSet<>(Comparator.comparing(Component::getWeight).thenComparing(Component::getColor));
- }
- public void addComponent(Component component){
- components.add(component);
- }
- public String getColor() {
- return color;
- }
- public int getWeight() {
- return weight;
- }
- public void changeColor(String color, int tezina){
- if (this.weight < tezina)
- this.color = color;
- for(Component c:components){
- c.changeColor(color,tezina);
- }
- }
- public String getRepresentation(String tab){
- StringBuilder sb = new StringBuilder();
- sb.append(tab+weight+":"+color+"\n");
- tab+="---";
- for (Component c:components){
- sb.append(c.getRepresentation(tab));
- }
- return sb.toString();
- }
- }
- class Window{
- private String name;
- Map<Integer, Component> map;
- public Window(String name) {
- this.name = name;
- this.map = new TreeMap<>(Integer::compareTo);
- }
- public void addComponent(int position, Component component) throws InvalidPositionException {
- if (map.containsKey(position))
- throw new InvalidPositionException("Invalid position "+position+", alredy taken!");
- map.put(position,component);
- }
- public void changeColor(int weight, String color){
- map.values().stream().forEach(component -> component.changeColor(color, weight));
- }
- public void swichComponents(int pos1, int pos2){
- Component tmp1 = map.get(pos1);
- Component tmp2 = map.get(pos2);
- map.put(pos1, tmp2);
- map.put(pos2, tmp1);
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- int k=1;
- sb.append("WINDOW ").append(name).append("\n");
- for (Component c:map.values()){
- sb.append(k+":"+c.getRepresentation(""));
- k++;
- }
- return sb.toString();
- }
- }
- 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);
- }
- }
- // вашиот код овде
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement