Advertisement
Martina312

[НП] - Stacked canvas

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