Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1. import java.util.Comparator;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.Set;
  5. import java.util.TreeMap;
  6. import java.util.TreeSet;
  7. public class ComponentTest {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         String name = scanner.nextLine();
  11.         Window window = new Window(name);
  12.         Component prev = null;
  13.         while (true) {
  14.             try {
  15.                 int what = scanner.nextInt();
  16.                 scanner.nextLine();
  17.                 if (what == 0) {
  18.                     int position = scanner.nextInt();
  19.                     window.addComponent(position, prev);
  20.                 } else if (what == 1) {
  21.                     String color = scanner.nextLine();
  22.                     int weight = scanner.nextInt();
  23.                     Component component = new Component(color, weight);
  24.                     prev = component;
  25.                 } else if (what == 2) {
  26.                     String color = scanner.nextLine();
  27.                     int weight = scanner.nextInt();
  28.                     Component component = new Component(color, weight);
  29.                     prev.addComponent(component);
  30.                     prev = component;
  31.                 } else if (what == 3) {
  32.                     String color = scanner.nextLine();
  33.                     int weight = scanner.nextInt();
  34.                     Component component = new Component(color, weight);
  35.                     prev.addComponent(component);
  36.                 } else if(what == 4) {
  37.                     break;
  38.                 }
  39.                
  40.             } catch (InvalidPositionException e) {
  41.                 System.out.println(e.getMessage());
  42.             }
  43.             scanner.nextLine();        
  44.         }
  45.        
  46.         System.out.println("=== ORIGINAL WINDOW ===");
  47.         System.out.println(window);
  48.         int weight = scanner.nextInt();
  49.         scanner.nextLine();
  50.         String color = scanner.nextLine();
  51.         window.changeColor(weight, color);
  52.         System.out.println(String.format("=== CHANGED COLOR (%d, %s) ===", weight, color));
  53.         System.out.println(window);
  54.         int pos1 = scanner.nextInt();
  55.         int pos2 = scanner.nextInt();
  56.         System.out.println(String.format("=== SWITCHED COMPONENTS %d <-> %d ===", pos1, pos2));
  57.         window.swichComponents(pos1, pos2);
  58.         System.out.println(window);
  59.         scanner.close();
  60.     }
  61. }
  62.  
  63. class ComponentComparator implements Comparator<Component>{
  64.  
  65.     @Override
  66.     public int compare(Component o1, Component o2) {
  67.         int cmp = Integer.compare(o1.getWeight(), o2.getWeight());
  68.         if(cmp==0){
  69.             return o1.getColor().compareTo(o2.getColor());
  70.         }
  71.         return cmp;
  72.     }
  73.    
  74.    
  75. }
  76.  
  77.  
  78. class Component{
  79.     private  String color;
  80.     private int weight;
  81.     private Set<Component> components;
  82.  
  83.     public Component(String color, int weight) {
  84.         this.color = color;
  85.         this.weight = weight;
  86.         components = new TreeSet<Component>( new ComponentComparator());
  87.     }
  88.  
  89.     public String getColor() {
  90.         return color;
  91.     }
  92.  
  93.     public int getWeight() {
  94.         return weight;
  95.     }
  96.  
  97.     public Set<Component> getComponents() {
  98.         return components;
  99.     }
  100.    
  101.     void addComponent(Component component){
  102.         components.add(component);
  103.     }
  104.     @Override
  105.     public String toString() {
  106.         return weight+":"+color;
  107.     }
  108.    
  109.     public String printMain(int pos){
  110.         return pos+":"+print(0);
  111.     }
  112.    
  113.    
  114.     public String print(int depth){
  115.         String str = "";
  116.         for(int i=0;i<depth;i++){
  117.             str+="---";
  118.         }
  119.         str+=this.toString()+"\n";
  120.         for(Component c : components){
  121.             str+=c.print(depth+1);
  122.         }
  123.         return str;
  124.     }
  125.    
  126.    
  127.  
  128.     public void changeColor(int weight, String color) {
  129.         if(this.weight<weight){
  130.             this.color = color;
  131.         }
  132.        
  133.         for(Component c : components){
  134.             if(c.getWeight()<weight){
  135.                 c.setColor(color);
  136.             }
  137.             c.changeColor(weight, color);
  138.         }
  139.     }
  140.  
  141.     private void setColor(String color) {
  142.         this.color = color;
  143.     }
  144.    
  145.    
  146.    
  147. }  
  148.  
  149. class Window{
  150.     private String name;
  151.     private Map<Integer,Component> components;
  152.     public Window(String name) {
  153.         this.name = name;
  154.         components = new TreeMap<Integer,Component>();
  155.     }
  156.    
  157.     void addComponent(int position, Component component) throws InvalidPositionException{
  158.         if(components.containsKey(position)){
  159.             throw new InvalidPositionException(position);
  160.         }
  161.         components.put(position, component);
  162.     }
  163.    
  164.     void changeColor(int weight, String color){
  165.         for(Component c : components.values()){
  166.             c.changeColor(weight,color);
  167.         }
  168.        
  169.        
  170.     }
  171.     void swichComponents(int pos1, int pos2){
  172.         Component comp1 = components.get(pos1);
  173.         Component comp2 = components.get(pos2);
  174.         components.put(pos2,comp1);
  175.         components.put(pos1,comp2);
  176.     }
  177.    
  178.     @Override
  179.     public String toString() {
  180.         StringBuilder sb = new StringBuilder();
  181.         sb.append("WINDOW " + this.name+"\n");
  182.         for(Map.Entry<Integer, Component> c : components.entrySet()){
  183.             sb.append(c.getValue().printMain(c.getKey()));
  184.         }
  185.         return sb.toString();
  186.     }
  187. }
  188.  
  189. @SuppressWarnings("serial")
  190. class InvalidPositionException extends Exception{
  191.     public InvalidPositionException(int pos) {
  192.         super("Invalid position "+pos+", alredy taken!");
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement