Filip_Markoski

[NP] Stacked canvas

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