Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- 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 Component{
- private String color;
- private int weight;
- private Set<Component> components;
- public Component(String color, int weght) {
- this.color = color;
- this.weight = weght;
- this.components = new TreeSet<>(Comparator.comparing(Component::getWeght).thenComparing(Component::getColor));
- }
- public void addComponent(Component component){
- components.add(component);
- }
- public String getColor() {
- return color;
- }
- public void changeColor(int weight, String color) {
- if(this.weight < weight)
- this.color = color;
- components.forEach(c -> c.changeColor(weight, color));
- }
- public int getWeght() {
- return weight;
- }
- public void setWeght(int weght) {
- this.weight = weght;
- }
- public String toString(String s){
- String s1 = String.format("%d:%s\n", weight, color);
- return components.stream()
- .collect(
- () -> new StringBuilder(s).append(s1),
- (sb, component) -> sb.append(component.toString(s + "---")),
- (s2, s3) -> s2.append(s3.toString())
- ).toString();
- }
- }
- class Window{
- private String name;
- private TreeMap<Integer, Component> components;
- Window(String name){
- this.name = name;
- components = new TreeMap<>();
- }
- public void addComponent(int position, Component component) throws InvalidPositionException{
- if(components.containsKey(position))
- throw new InvalidPositionException("Invalid position " + position + ", alredy taken!");
- components.put(position, component);
- }
- public void changeColor(int weight, String color){
- components.values().forEach(c -> c.changeColor(weight, color));
- }
- public void swichComponents(int pos1, int pos2){
- Component new2 = components.get(pos1);
- Component new1 = components.get(pos2);
- components.put(pos1, new1);
- components.put(pos2, new2);
- }
- @Override
- public String toString(){
- return components.entrySet().stream()
- .collect(
- () -> new StringBuilder("WINDOW ").append(name).append("\n"),
- (s, entry) -> s.append(entry.getKey()).append(":").append(entry.getValue().toString("")),
- (s1, s2) -> s1.append(s2.toString())
- ).toString();
- }
- }
- class InvalidPositionException extends Exception{
- InvalidPositionException(){
- super("InvalidPositionException");
- }
- InvalidPositionException(String s){
- super(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement