Advertisement
Guest User

Untitled

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