Advertisement
dzocesrce

[NP] Canvas

Apr 13th, 2025
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.77 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Objects;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Shapes2Test {
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         ShapesApplication shapesApplication = new ShapesApplication(10000);
  14.  
  15.         System.out.println("===READING CANVASES AND SHAPES FROM INPUT STREAM===");
  16.         shapesApplication.readCanvases(System.in);
  17.  
  18.         System.out.println("===PRINTING SORTED CANVASES TO OUTPUT STREAM===");
  19.         shapesApplication.printCanvases(System.out);
  20.  
  21.  
  22.     }
  23. }
  24.  
  25. class ShapesApplication {
  26.     private int maxArea;
  27.     private List<Canvas> canvases;
  28.  
  29.     public ShapesApplication(int maxArea) {
  30.         this.maxArea = maxArea;
  31.  
  32.     }
  33.  
  34.     public void readCanvases(InputStream in) {
  35.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
  36.         canvases= bufferedReader.lines().map(i-> {
  37.                     try {
  38.                         return Canvas.create(i,maxArea);
  39.                     } catch (IrregularCanvasException e) {
  40.                         System.out.println(e.getMessage());
  41.                         return null;
  42.                     }
  43.                 })
  44.                 .filter(Objects::nonNull)
  45.                 .collect(Collectors.toList());
  46.     }
  47.  
  48.     public void printCanvases(PrintStream out) {
  49.         PrintWriter printWriter = new PrintWriter(out);
  50.  
  51.  
  52.         canvases.stream().sorted(Comparator.reverseOrder()).forEach(i->printWriter.println(i));
  53.  
  54.         printWriter.flush();
  55.     }
  56. }
  57.  
  58. class Canvas implements Comparable<Canvas>{
  59.  
  60.     private String id;
  61.     private List<Shape> shapes;
  62.  
  63.     public Canvas(String id, List<Shape> shapes) {
  64.         this.id = id;
  65.         this.shapes = shapes;
  66.     }
  67.  
  68.     public double totalArea(){
  69.         return shapes.stream().mapToDouble(i->i.getArea()).sum();
  70.     }
  71.  
  72.     public static Canvas create(String s, int maxArea) throws IrregularCanvasException {
  73.         String[] parts = s.split("\\s+");
  74.         String id = parts[0];
  75.         List<Shape> shapes = new ArrayList<>();
  76.         for(int i=1;i< parts.length;i+=2){
  77.             Shape shape;
  78.             if(parts[i].charAt(0)=='C')
  79.                 shape = new Circle(Integer.parseInt(parts[i+1]));
  80.             else
  81.                 shape = new Square(Integer.parseInt(parts[i+1]));
  82.             if(shape.getArea()>maxArea)
  83.                 throw new IrregularCanvasException(id,maxArea);
  84.  
  85.             shapes.add(shape);
  86.         }
  87.  
  88.         return new Canvas(id,shapes);
  89.  
  90.     }
  91.  
  92.     public double minArea(){
  93.         return shapes.stream().mapToDouble(i->i.getArea()).min().getAsDouble();
  94.     }
  95.  
  96.     public double maxArea(){
  97.         return shapes.stream().mapToDouble(i->i.getArea()).max().getAsDouble();
  98.  
  99.     }
  100.  
  101.     public int numberOfCircles(){
  102.         return (int)shapes.stream().filter(i->i.getType().equals(Type.CIRCLE)).count();
  103.  
  104.     }
  105.  
  106.     public int numberOfSquares(){
  107.         return (int)shapes.stream().filter(i->i.getType().equals(Type.SQUARE)).count();
  108.  
  109.     }
  110.  
  111.     @Override
  112.     public String toString() {
  113.         return String.format("%s %d %d %d %.2f %.2f %.2f",id,shapes.size(),numberOfCircles(),numberOfSquares(), minArea(), maxArea(), totalArea()/shapes.size());
  114.     }
  115.  
  116.  
  117.     @Override
  118.     public int compareTo(Canvas o) {
  119.         return Double.compare(this.totalArea(),o.totalArea());
  120.     }
  121. }
  122.  
  123. class IrregularCanvasException extends Exception{
  124.     public IrregularCanvasException(String id, int maxArea) {
  125.         super(String.format("Canvas %s has a shape with area larger than %.2f",id,(double)maxArea));
  126.     }
  127. }
  128.  
  129. abstract class Shape implements Comparable<Shape>{
  130.     private int line;
  131.  
  132.     public abstract Type getType();
  133.  
  134.     public Shape(int line) {
  135.         this.line = line;
  136.     }
  137.  
  138.     public int getLine(){
  139.         return line;
  140.     }
  141.  
  142.     public abstract double getArea();
  143. }
  144.  
  145. enum Type {
  146.     CIRCLE,
  147.     SQUARE
  148. }
  149.  
  150. class Square extends Shape{
  151.  
  152.     @Override
  153.     public Type getType() {
  154.         return Type.SQUARE;
  155.     }
  156.  
  157.     public Square(int line) {
  158.         super(line);
  159.     }
  160.  
  161.     @Override
  162.     public double getArea() {
  163.         return super.getLine()*super.getLine();
  164.     }
  165.  
  166.     @Override
  167.     public int compareTo(Shape o) {
  168.         return Double.compare(this.getArea(),o.getArea());
  169.     }
  170. }
  171.  
  172. class Circle extends Shape {
  173.  
  174.  
  175.     public Circle(int line) {
  176.         super(line);
  177.     }
  178.  
  179.  
  180.     @Override
  181.     public Type getType() {
  182.         return Type.CIRCLE;
  183.     }
  184.  
  185.     @Override
  186.     public double getArea() {
  187.         return Math.pow(super.getLine(),2)*Math.PI;
  188.     }
  189.  
  190.     @Override
  191.     public int compareTo(Shape o) {
  192.         return Double.compare(this.getArea(),o.getArea());
  193.     }
  194.  
  195. }
  196.  
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement