Advertisement
dzocesrce

[NP] Uncle Canvas

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