Advertisement
ilevishinov

Stacked canvas

Nov 17th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. enum Color {
  7.     RED, GREEN, BLUE
  8. }
  9.  
  10. interface Scalable{
  11.     public void scale(float scaleFactor);
  12. }
  13.  
  14. interface Stackable{
  15.     public float weight();
  16. }
  17.  
  18. abstract class Shape implements Scalable,Stackable{
  19.     String id;
  20.     Color color;
  21.     Shape(){}
  22.     Shape(String id, Color color){
  23.         this.id=id;
  24.         this.color=color;
  25.     }
  26.     abstract public void scale(float scaleFactor);
  27.     abstract public float weight();
  28.     abstract public String type();
  29. }
  30.  
  31. class Circle extends Shape implements Scalable,Stackable{
  32.     float radius;
  33.     Circle(float r){
  34.         super();
  35.         radius=r;
  36.     }
  37.    
  38.     Circle(String id, Color color, float radius){
  39.         super(id,color);
  40.         this.radius=radius;
  41.     }
  42.    
  43.     public void scale(float scaleFactor) {
  44.         radius*=scaleFactor;
  45.     }
  46.     public float weight() {
  47.         return (radius*radius*(float)Math.PI);
  48.     }
  49.    
  50.     public String type() {
  51.         return "Circle";
  52.     }
  53.    
  54. }
  55.  
  56. class Rectangle extends Shape implements Scalable,Stackable{
  57.     float width,height;
  58.     Rectangle(int w,int h){
  59.         super();
  60.         width=w;
  61.         height=h;
  62.     }
  63.    
  64.     Rectangle(String id, Color color, float width, float height){
  65.         super(id,color);
  66.         this.width=width;
  67.         this.height=height;
  68.     }
  69.     public void scale(float scaleFactor) {
  70.         width*=scaleFactor;
  71.         height*=scaleFactor;
  72.     }
  73.     public float weight() {
  74.         return (height*width);
  75.     }
  76.    
  77.     public String type() {
  78.         return "Rectangle";
  79.     }
  80.    
  81. }
  82.  
  83. class Canvas{
  84.     List<Shape> list;
  85.    
  86.     Canvas(){
  87.         list=new ArrayList<>();
  88.     }
  89.    
  90.     void add(String id, Color color, float radius) {
  91.         if(list.size()==0) {
  92.             list.add(new Circle(id,color,radius));
  93.         }
  94.         Circle c=new Circle(id,color,radius);
  95.         for(int i=0;i<list.size();i++) {
  96.             if(c.weight()>list.get(i).weight()) {
  97.                 list.add(i, c);
  98.                 return;
  99.             }
  100.         }
  101.         if(c.weight()==list.get(list.size()-1).weight()) return;
  102.         list.add(c);
  103.     }
  104.    
  105.     void add(String id, Color color, float width, float height) {
  106.         if(list.size()==0) {
  107.             list.add(new Rectangle(id,color,width,height));
  108.         }
  109.         Rectangle r=new Rectangle(id,color,width,height);
  110.         for(int i=0;i<list.size();i++) {
  111.             if(r.weight()>list.get(i).weight()) {
  112.                 list.add(i, r);
  113.                 return;
  114.             }
  115.         }
  116.        
  117.         if(r.weight()==list.get(list.size()-1).weight()) return;
  118.         list.add(r);
  119.     }
  120.    
  121.     void scale(String id,float scaleFactor) {
  122.         int index=-1;
  123.        
  124.         for(int i=0;i<list.size();i++) {
  125.             if(list.get(i).id.equals(id)) {
  126.                 list.get(i).scale(scaleFactor);
  127.                 index=i;
  128.                 break;
  129.             }
  130.         }
  131.        
  132.         if(index==-1) return;
  133.        
  134.         if(list.get(index).type().equals("Circle")) {
  135.             Circle c=(Circle)list.get(index);
  136.             list.remove(index);
  137.             this.add(c.id, c.color, c.radius);
  138.         }
  139.         if(list.get(index).type().equals("Rectangle")) {
  140.             Rectangle r=(Rectangle)list.get(index);
  141.             list.remove(index);
  142.             this.add(r.id, r.color, r.width, r.height);
  143.         }
  144.     }
  145.    
  146.     public String toString() {
  147.         StringBuilder sb=new StringBuilder();
  148.         //System.out.println(list.size()+" size of list");
  149.         for(Shape c:list) {
  150.            
  151.                 if(c.type().equals("Circle")) {
  152.                     sb.append(String.format("C: %-5s%-10s%10.2f\n", c.id,c.color, c.weight()));
  153.                 }
  154.                 if(c.type().equals("Rectangle")) {
  155.                     sb.append(String.format("R: %-5s%-10s%10.2f\n", c.id,c.color, c.weight()));
  156.                 }
  157.         }
  158.         return sb.toString();
  159.     }
  160.    
  161. }
  162.  
  163. public class ShapesTest {
  164.     public static void main(String[] args) {
  165.         Scanner scanner = new Scanner(System.in);
  166.         Canvas canvas = new Canvas();
  167.         while (scanner.hasNextLine()) {
  168.             String line = scanner.nextLine();
  169.             String[] parts = line.split(" ");
  170.             int type = Integer.parseInt(parts[0]);
  171.             String id = parts[1];
  172.             if (type == 1) {
  173.                 Color color = Color.valueOf(parts[2]);
  174.                 float radius = Float.parseFloat(parts[3]);
  175.                 canvas.add(id, color, radius);
  176.             } else if (type == 2) {
  177.                 Color color = Color.valueOf(parts[2]);
  178.                 float width = Float.parseFloat(parts[3]);
  179.                 float height = Float.parseFloat(parts[4]);
  180.                 canvas.add(id, color, width, height);
  181.             } else if (type == 3) {
  182.                 float scaleFactor = Float.parseFloat(parts[2]);
  183.                 System.out.println("ORIGNAL:");
  184.                 System.out.print(canvas);
  185.                 canvas.scale(id, scaleFactor);
  186.                 System.out.printf("AFTER SCALING: %s %.2f\n", id, scaleFactor);
  187.                 System.out.print(canvas);
  188.             }
  189.  
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement