Advertisement
dzocesrce

[NP] Mapped Canvas

Apr 27th, 2025
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.27 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.util.stream.Collectors;
  7. class InvalidIDException extends Exception{
  8.     public InvalidIDException(String id) {
  9.         super(String.format("ID %s is not valid",id));
  10.     }
  11. }
  12. enum ShapeType {
  13.     CIRCLE,
  14.     RECTANGLE,
  15.     SQUARE
  16. }
  17.  
  18.  
  19. class Square extends Shape{
  20.  
  21.  
  22.     public Square(String username, double x) {
  23.         super(username, x);
  24.     }
  25.  
  26.     @Override
  27.     public double getPerimeter() {
  28.         return 4*x;
  29.     }
  30.  
  31.     @Override
  32.     public double getArea() {
  33.         return x*x;
  34.     }
  35.  
  36.     @Override
  37.     public ShapeType getShapeType() {
  38.         return ShapeType.SQUARE;
  39.     }
  40.  
  41.     @Override
  42.     public String toString() {
  43.         return String.format("Square: -> Side: %.2f Area: %.2f Perimeter: %.2f",super.getX(),getArea(),getPerimeter());
  44.     }
  45. }
  46.  
  47. class InvalidDimensionException extends Exception {
  48.     public InvalidDimensionException(double x) {
  49.         super(String.format("Dimension 0 is not allowed!"));
  50.     }
  51. }
  52.  
  53. class Rectange extends Shape{
  54.  
  55.     double y;
  56.  
  57.     public Rectange(String username, double x, double y) {
  58.         super(username, x);
  59.         this.y = y;
  60.     }
  61.  
  62.     public double getY() {
  63.         return y;
  64.     }
  65.  
  66.     public void scaleY(double v) {
  67.         this.y *=v;
  68.     }
  69.  
  70.     @Override
  71.     public ShapeType getShapeType() {
  72.         return ShapeType.RECTANGLE;
  73.     }
  74.  
  75.     @Override
  76.     public double getPerimeter() {
  77.         return 2*(x+y);
  78.     }
  79.  
  80.     @Override
  81.     public double getArea() {
  82.         return x*y;
  83.     }
  84.  
  85.     @Override
  86.     public String toString() {
  87.         return String.format("Rectangle: -> Sides: %.2f, %.2f Area: %.2f Perimeter: %.2f",super.getX(),getY(),getArea(),getPerimeter());
  88.     }
  89. }
  90. class Circle extends Shape{
  91.  
  92.  
  93.     public Circle(String username, double x) {
  94.         super(username, x);
  95.     }
  96.  
  97.     @Override
  98.     public double getPerimeter() {
  99.         return 2*x*Math.PI;
  100.     }
  101.  
  102.     @Override
  103.     public double getArea() {
  104.         return x*x*Math.PI;
  105.     }
  106.  
  107.     @Override
  108.     public ShapeType getShapeType() {
  109.         return ShapeType.CIRCLE;
  110.     }
  111.     @Override
  112.     public String toString() {
  113.         return String.format("Circle -> Radius: %.2f Area: %.2f Perimeter: %.2f",super.getX(),getArea(),getPerimeter());
  114.     }
  115. }
  116. abstract class Shape {
  117.     double x;
  118.     String username;
  119.     public abstract double getPerimeter();
  120.  
  121.     public abstract double getArea();
  122.  
  123.     public abstract ShapeType getShapeType();
  124.  
  125.     public Shape(String username, double x) {
  126.         this.username= username;
  127.         this.x = x;
  128.     }
  129.  
  130.     public double getX() {
  131.         return x;
  132.     }
  133.  
  134.     public String getUsername() {
  135.         return username;
  136.     }
  137.  
  138.     public static Shape create(String line) throws InvalidIDException, InvalidDimensionException {
  139.         String[] parts = line.split("\\s+");
  140.         int shapeNumber = Integer.parseInt(parts[0]);
  141.         String username = parts[1];
  142.         double x = Double.parseDouble(parts[2]);
  143.         if(x==0)
  144.             throw new InvalidDimensionException(x);
  145.         double y= 0.0;
  146.         if(username.length()!=6)
  147.             throw new InvalidIDException(username);
  148.         for(int i=0;i<username.length();i++)
  149.             if(!Character.isAlphabetic(username.charAt(i))&&!Character.isDigit(username.charAt(i))){
  150.                 throw new InvalidIDException(username);
  151.             }
  152.         if(parts.length==4){
  153.             y = Double.parseDouble(parts[3]);
  154.             if(y==0)
  155.                 throw new InvalidDimensionException(y);
  156.         }
  157.         if(shapeNumber==1)
  158.             return new Circle(username,x);
  159.         else if(shapeNumber==2)
  160.             return new Square(username,x);
  161.         else
  162.             return new Rectange(username,x,y);
  163.     }
  164.  
  165.     public void scaleX(double v) {
  166.         this.x*=v;
  167.     }
  168. }
  169. class Canvas {
  170.  
  171.     List<Shape> shapes;
  172.  
  173.  
  174.     public void readShapes(InputStream in) {
  175.         BufferedReader br = new BufferedReader(new InputStreamReader(in));
  176.         shapes = new ArrayList<>();
  177.  
  178.         try {
  179.             String line;
  180.             while ((line = br.readLine()) != null) {
  181.                 try {
  182.                     Shape shape = Shape.create(line);
  183.                     if(shapes.isEmpty())
  184.                         shapes.add(shape);
  185.                     else{
  186.                         for(int i=0;i<shapes.size();i++){
  187.                             if(shapes.get(i).getArea()>shape.getArea()){
  188.                                 shapes.add(i,shape);
  189.                                 break;
  190.                             }
  191.                         }
  192.                         if(!shapes.contains(shape))
  193.                             shapes.add(shape);
  194.                     }
  195.  
  196.                 } catch (InvalidIDException e) {
  197.                     System.out.println(e.getMessage());
  198.                     // Continue reading next line
  199.                 } catch (InvalidDimensionException e) {
  200.                     System.out.println(e.getMessage());
  201.                     // STOP reading shapes but do not crash the program
  202.                     break;
  203.                 }
  204.             }
  205.         } catch (IOException e) {
  206.             e.printStackTrace(); // Handle IO error if needed
  207.         }
  208.     }
  209.  
  210.     public void printAllShapes(PrintStream out) {
  211.  
  212.         PrintWriter printWriter = new PrintWriter(out);
  213.         shapes.stream().forEach(printWriter::println);
  214.         printWriter.flush();
  215.     }
  216.  
  217.     public void scaleShapes(String username, double v) {
  218.         shapes.stream().filter(i->i.getUsername().equals(username)).forEach(i->{
  219.             i.scaleX(v);
  220.         });
  221.         shapes.stream().filter(i->i.getUsername().equals(username) && i.getShapeType().equals(ShapeType.RECTANGLE))
  222.                 .map(i->(Rectange)i)
  223.                 .forEach(i->{
  224.             i.scaleY(v);
  225.         });
  226.  
  227.     }
  228.  
  229.         public void printByUserId(PrintStream out) {
  230.         PrintWriter printWriter = new PrintWriter(out);
  231.         Map<String,List<Shape>> shapesByUser = new HashMap<>();
  232.         for(Shape shape: shapes) {
  233.             shapesByUser.putIfAbsent(shape.getUsername(), new ArrayList<>());
  234.             shapesByUser.get(shape.getUsername()).add(shape);
  235.         }
  236.  
  237.         shapesByUser.entrySet().stream()
  238.                 .sorted(Comparator
  239.                         .comparing((Map.Entry<String, List<Shape>> entry) -> entry.getValue().size(), Comparator.reverseOrder())
  240.                         .thenComparing(entry -> entry.getValue().stream()
  241.                                 .mapToDouble(Shape::getArea)
  242.                                 .sum(), Comparator.naturalOrder())
  243.                 ).forEach(entry -> {
  244.                     printWriter.println(String.format("Shapes of user: %s", entry.getKey()));
  245.                     entry.getValue().stream().sorted(Comparator.comparing(Shape::getPerimeter))
  246.                             .forEach(shape -> printWriter.println(shape));
  247.                 });
  248.         printWriter.flush();
  249.     }
  250.  
  251.     public void statistics(PrintStream out) {
  252.         PrintWriter printWriter = new PrintWriter(out);
  253.         DoubleSummaryStatistics doubleSummaryStatistics = shapes.stream().mapToDouble(Shape::getArea).summaryStatistics();
  254.         printWriter.println(String.format("count: %d\nsum: %.2f\nmin: %.2f\naverage: %.2f\nmax: %.2f\n",doubleSummaryStatistics.getCount()
  255.                 ,doubleSummaryStatistics.getSum()
  256.                 ,doubleSummaryStatistics.getMin()
  257.                 ,doubleSummaryStatistics.getAverage()
  258.                 ,doubleSummaryStatistics.getMax()));
  259.         printWriter.flush();
  260.     }
  261. }
  262. public class CanvasTest {
  263.  
  264.     public static void main(String[] args) {
  265.         Canvas canvas = new Canvas();
  266.  
  267.         System.out.println("READ SHAPES AND EXCEPTIONS TESTING");
  268.         canvas.readShapes(System.in);
  269.      
  270.         System.out.println("BEFORE SCALING");
  271.         canvas.printAllShapes(System.out);
  272.         canvas.scaleShapes("123456", 1.5);
  273.         System.out.println("AFTER SCALING");
  274.         canvas.printAllShapes(System.out);
  275.  
  276.         System.out.println("PRINT BY USER ID TESTING");
  277.         canvas.printByUserId(System.out);
  278.  
  279.         System.out.println("PRINT STATISTICS");
  280.         canvas.statistics(System.out);
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement