Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Comparator;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Objects;
- import java.util.stream.Collectors;
- public class Shapes2Test {
- public static void main(String[] args) {
- ShapesApplication shapesApplication = new ShapesApplication(10000);
- System.out.println("===READING CANVASES AND SHAPES FROM INPUT STREAM===");
- shapesApplication.readCanvases(System.in);
- System.out.println("===PRINTING SORTED CANVASES TO OUTPUT STREAM===");
- shapesApplication.printCanvases(System.out);
- }
- }
- class ShapesApplication {
- private int maxArea;
- private List<Canvas> canvases;
- public ShapesApplication(int maxArea) {
- this.maxArea = maxArea;
- }
- public void readCanvases(InputStream in) {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
- canvases= bufferedReader.lines().map(i-> {
- try {
- return Canvas.create(i,maxArea);
- } catch (IrregularCanvasException e) {
- System.out.println(e.getMessage());
- return null;
- }
- })
- .filter(Objects::nonNull)
- .collect(Collectors.toList());
- }
- public void printCanvases(PrintStream out) {
- PrintWriter printWriter = new PrintWriter(out);
- canvases.stream().sorted(Comparator.reverseOrder()).forEach(i->printWriter.println(i));
- printWriter.flush();
- }
- }
- class Canvas implements Comparable<Canvas>{
- private String id;
- private List<Shape> shapes;
- public Canvas(String id, List<Shape> shapes) {
- this.id = id;
- this.shapes = shapes;
- }
- public double totalArea(){
- return shapes.stream().mapToDouble(i->i.getArea()).sum();
- }
- public static Canvas create(String s, int maxArea) throws IrregularCanvasException {
- String[] parts = s.split("\\s+");
- String id = parts[0];
- List<Shape> shapes = new ArrayList<>();
- for(int i=1;i< parts.length;i+=2){
- Shape shape;
- if(parts[i].charAt(0)=='C')
- shape = new Circle(Integer.parseInt(parts[i+1]));
- else
- shape = new Square(Integer.parseInt(parts[i+1]));
- if(shape.getArea()>maxArea)
- throw new IrregularCanvasException(id,maxArea);
- shapes.add(shape);
- }
- return new Canvas(id,shapes);
- }
- public double minArea(){
- return shapes.stream().mapToDouble(i->i.getArea()).min().getAsDouble();
- }
- public double maxArea(){
- return shapes.stream().mapToDouble(i->i.getArea()).max().getAsDouble();
- }
- public int numberOfCircles(){
- return (int)shapes.stream().filter(i->i.getType().equals(Type.CIRCLE)).count();
- }
- public int numberOfSquares(){
- return (int)shapes.stream().filter(i->i.getType().equals(Type.SQUARE)).count();
- }
- @Override
- public String toString() {
- return String.format("%s %d %d %d %.2f %.2f %.2f",id,shapes.size(),numberOfCircles(),numberOfSquares(), minArea(), maxArea(), totalArea()/shapes.size());
- }
- @Override
- public int compareTo(Canvas o) {
- return Double.compare(this.totalArea(),o.totalArea());
- }
- }
- class IrregularCanvasException extends Exception{
- public IrregularCanvasException(String id, int maxArea) {
- super(String.format("Canvas %s has a shape with area larger than %.2f",id,(double)maxArea));
- }
- }
- abstract class Shape implements Comparable<Shape>{
- private int line;
- public abstract Type getType();
- public Shape(int line) {
- this.line = line;
- }
- public int getLine(){
- return line;
- }
- public abstract double getArea();
- }
- enum Type {
- CIRCLE,
- SQUARE
- }
- class Square extends Shape{
- @Override
- public Type getType() {
- return Type.SQUARE;
- }
- public Square(int line) {
- super(line);
- }
- @Override
- public double getArea() {
- return super.getLine()*super.getLine();
- }
- @Override
- public int compareTo(Shape o) {
- return Double.compare(this.getArea(),o.getArea());
- }
- }
- class Circle extends Shape {
- public Circle(int line) {
- super(line);
- }
- @Override
- public Type getType() {
- return Type.CIRCLE;
- }
- @Override
- public double getArea() {
- return Math.pow(super.getLine(),2)*Math.PI;
- }
- @Override
- public int compareTo(Shape o) {
- return Double.compare(this.getArea(),o.getArea());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement