Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.io.*;
- import java.util.*;
- import java.util.stream.Collectors;
- class InvalidIDException extends Exception{
- public InvalidIDException(String id) {
- super(String.format("ID %s is not valid",id));
- }
- }
- enum ShapeType {
- CIRCLE,
- RECTANGLE,
- SQUARE
- }
- class Square extends Shape{
- public Square(String username, double x) {
- super(username, x);
- }
- @Override
- public double getPerimeter() {
- return 4*x;
- }
- @Override
- public double getArea() {
- return x*x;
- }
- @Override
- public ShapeType getShapeType() {
- return ShapeType.SQUARE;
- }
- @Override
- public String toString() {
- return String.format("Square: -> Side: %.2f Area: %.2f Perimeter: %.2f",super.getX(),getArea(),getPerimeter());
- }
- }
- class InvalidDimensionException extends Exception {
- public InvalidDimensionException(double x) {
- super(String.format("Dimension 0 is not allowed!"));
- }
- }
- class Rectange extends Shape{
- double y;
- public Rectange(String username, double x, double y) {
- super(username, x);
- this.y = y;
- }
- public double getY() {
- return y;
- }
- public void scaleY(double v) {
- this.y *=v;
- }
- @Override
- public ShapeType getShapeType() {
- return ShapeType.RECTANGLE;
- }
- @Override
- public double getPerimeter() {
- return 2*(x+y);
- }
- @Override
- public double getArea() {
- return x*y;
- }
- @Override
- public String toString() {
- return String.format("Rectangle: -> Sides: %.2f, %.2f Area: %.2f Perimeter: %.2f",super.getX(),getY(),getArea(),getPerimeter());
- }
- }
- class Circle extends Shape{
- public Circle(String username, double x) {
- super(username, x);
- }
- @Override
- public double getPerimeter() {
- return 2*x*Math.PI;
- }
- @Override
- public double getArea() {
- return x*x*Math.PI;
- }
- @Override
- public ShapeType getShapeType() {
- return ShapeType.CIRCLE;
- }
- @Override
- public String toString() {
- return String.format("Circle -> Radius: %.2f Area: %.2f Perimeter: %.2f",super.getX(),getArea(),getPerimeter());
- }
- }
- abstract class Shape {
- double x;
- String username;
- public abstract double getPerimeter();
- public abstract double getArea();
- public abstract ShapeType getShapeType();
- public Shape(String username, double x) {
- this.username= username;
- this.x = x;
- }
- public double getX() {
- return x;
- }
- public String getUsername() {
- return username;
- }
- public static Shape create(String line) throws InvalidIDException, InvalidDimensionException {
- String[] parts = line.split("\\s+");
- int shapeNumber = Integer.parseInt(parts[0]);
- String username = parts[1];
- double x = Double.parseDouble(parts[2]);
- if(x==0)
- throw new InvalidDimensionException(x);
- double y= 0.0;
- if(username.length()!=6)
- throw new InvalidIDException(username);
- for(int i=0;i<username.length();i++)
- if(!Character.isAlphabetic(username.charAt(i))&&!Character.isDigit(username.charAt(i))){
- throw new InvalidIDException(username);
- }
- if(parts.length==4){
- y = Double.parseDouble(parts[3]);
- if(y==0)
- throw new InvalidDimensionException(y);
- }
- if(shapeNumber==1)
- return new Circle(username,x);
- else if(shapeNumber==2)
- return new Square(username,x);
- else
- return new Rectange(username,x,y);
- }
- public void scaleX(double v) {
- this.x*=v;
- }
- }
- class Canvas {
- List<Shape> shapes;
- public void readShapes(InputStream in) {
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- shapes = new ArrayList<>();
- try {
- String line;
- while ((line = br.readLine()) != null) {
- try {
- Shape shape = Shape.create(line);
- if(shapes.isEmpty())
- shapes.add(shape);
- else{
- for(int i=0;i<shapes.size();i++){
- if(shapes.get(i).getArea()>shape.getArea()){
- shapes.add(i,shape);
- break;
- }
- }
- if(!shapes.contains(shape))
- shapes.add(shape);
- }
- } catch (InvalidIDException e) {
- System.out.println(e.getMessage());
- // Continue reading next line
- } catch (InvalidDimensionException e) {
- System.out.println(e.getMessage());
- // STOP reading shapes but do not crash the program
- break;
- }
- }
- } catch (IOException e) {
- e.printStackTrace(); // Handle IO error if needed
- }
- }
- public void printAllShapes(PrintStream out) {
- PrintWriter printWriter = new PrintWriter(out);
- shapes.stream().forEach(printWriter::println);
- printWriter.flush();
- }
- public void scaleShapes(String username, double v) {
- shapes.stream().filter(i->i.getUsername().equals(username)).forEach(i->{
- i.scaleX(v);
- });
- shapes.stream().filter(i->i.getUsername().equals(username) && i.getShapeType().equals(ShapeType.RECTANGLE))
- .map(i->(Rectange)i)
- .forEach(i->{
- i.scaleY(v);
- });
- }
- public void printByUserId(PrintStream out) {
- PrintWriter printWriter = new PrintWriter(out);
- Map<String,List<Shape>> shapesByUser = new HashMap<>();
- for(Shape shape: shapes) {
- shapesByUser.putIfAbsent(shape.getUsername(), new ArrayList<>());
- shapesByUser.get(shape.getUsername()).add(shape);
- }
- shapesByUser.entrySet().stream()
- .sorted(Comparator
- .comparing((Map.Entry<String, List<Shape>> entry) -> entry.getValue().size(), Comparator.reverseOrder())
- .thenComparing(entry -> entry.getValue().stream()
- .mapToDouble(Shape::getArea)
- .sum(), Comparator.naturalOrder())
- ).forEach(entry -> {
- printWriter.println(String.format("Shapes of user: %s", entry.getKey()));
- entry.getValue().stream().sorted(Comparator.comparing(Shape::getPerimeter))
- .forEach(shape -> printWriter.println(shape));
- });
- printWriter.flush();
- }
- public void statistics(PrintStream out) {
- PrintWriter printWriter = new PrintWriter(out);
- DoubleSummaryStatistics doubleSummaryStatistics = shapes.stream().mapToDouble(Shape::getArea).summaryStatistics();
- printWriter.println(String.format("count: %d\nsum: %.2f\nmin: %.2f\naverage: %.2f\nmax: %.2f\n",doubleSummaryStatistics.getCount()
- ,doubleSummaryStatistics.getSum()
- ,doubleSummaryStatistics.getMin()
- ,doubleSummaryStatistics.getAverage()
- ,doubleSummaryStatistics.getMax()));
- printWriter.flush();
- }
- }
- public class CanvasTest {
- public static void main(String[] args) {
- Canvas canvas = new Canvas();
- System.out.println("READ SHAPES AND EXCEPTIONS TESTING");
- canvas.readShapes(System.in);
- System.out.println("BEFORE SCALING");
- canvas.printAllShapes(System.out);
- canvas.scaleShapes("123456", 1.5);
- System.out.println("AFTER SCALING");
- canvas.printAllShapes(System.out);
- System.out.println("PRINT BY USER ID TESTING");
- canvas.printByUserId(System.out);
- System.out.println("PRINT STATISTICS");
- canvas.statistics(System.out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement