Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Comparator;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.TreeMap;
- import java.util.TreeSet;
- 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);
- scanner.close();
- }
- }
- class ComponentComparator implements Comparator<Component>{
- @Override
- public int compare(Component o1, Component o2) {
- int cmp = Integer.compare(o1.getWeight(), o2.getWeight());
- if(cmp==0){
- return o1.getColor().compareTo(o2.getColor());
- }
- return cmp;
- }
- }
- class Component{
- private String color;
- private int weight;
- private Set<Component> components;
- public Component(String color, int weight) {
- this.color = color;
- this.weight = weight;
- components = new TreeSet<Component>( new ComponentComparator());
- }
- public String getColor() {
- return color;
- }
- public int getWeight() {
- return weight;
- }
- public Set<Component> getComponents() {
- return components;
- }
- void addComponent(Component component){
- components.add(component);
- }
- @Override
- public String toString() {
- return weight+":"+color;
- }
- public String printMain(int pos){
- return pos+":"+print(0);
- }
- public String print(int depth){
- String str = "";
- for(int i=0;i<depth;i++){
- str+="---";
- }
- str+=this.toString()+"\n";
- for(Component c : components){
- str+=c.print(depth+1);
- }
- return str;
- }
- public void changeColor(int weight, String color) {
- if(this.weight<weight){
- this.color = color;
- }
- for(Component c : components){
- if(c.getWeight()<weight){
- c.setColor(color);
- }
- c.changeColor(weight, color);
- }
- }
- private void setColor(String color) {
- this.color = color;
- }
- }
- class Window{
- private String name;
- private Map<Integer,Component> components;
- public Window(String name) {
- this.name = name;
- components = new TreeMap<Integer,Component>();
- }
- void addComponent(int position, Component component) throws InvalidPositionException{
- if(components.containsKey(position)){
- throw new InvalidPositionException(position);
- }
- components.put(position, component);
- }
- void changeColor(int weight, String color){
- for(Component c : components.values()){
- c.changeColor(weight,color);
- }
- }
- void swichComponents(int pos1, int pos2){
- Component comp1 = components.get(pos1);
- Component comp2 = components.get(pos2);
- components.put(pos2,comp1);
- components.put(pos1,comp2);
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("WINDOW " + this.name+"\n");
- for(Map.Entry<Integer, Component> c : components.entrySet()){
- sb.append(c.getValue().printMain(c.getKey()));
- }
- return sb.toString();
- }
- }
- @SuppressWarnings("serial")
- class InvalidPositionException extends Exception{
- public InvalidPositionException(int pos) {
- super("Invalid position "+pos+", alredy taken!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement