Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.Scanner;
- interface Stackable {
- float weight();
- }
- interface Scalable {
- void scale(float scaleFactor);
- }
- abstract class Shape implements Scalable, Stackable {
- protected String id;
- protected Color color;
- public Shape(String id, Color color) {
- this.id = id;
- this.color = color;
- }
- public String getId() {
- return id;
- }
- public Color getColor() {
- return color;
- }
- @Override
- public String toString() {
- return String.format("%-5s%-5s %10.2f", id, color.toString(), weight());
- }
- }
- class Circle extends Shape {
- private float radius;
- public Circle(String id, Color color, float radius) {
- super(id, color);
- this.radius = radius;
- }
- @Override
- public void scale(float scaleFactor) {
- radius *= scaleFactor;
- }
- @Override
- public float weight() {
- return (float) ((float)Math.pow(radius, 2)*Math.PI);
- }
- @Override
- public String toString() {
- return String.format("C: %s\n",super.toString() );
- }
- }
- class Rectangle extends Shape{
- private float width;
- private float height;
- public Rectangle(String id, Color color, float width, float height) {
- super(id, color);
- this.width = width;
- this.height = height;
- }
- @Override
- public void scale(float scaleFactor) {
- width*=scaleFactor;
- height*=scaleFactor;
- }
- @Override
- public float weight() {
- return width*height;
- }
- @Override
- public String toString() {
- return String.format("R: %s\n", super.toString());
- }
- }
- class Canvas {
- private List<Shape> shapes;
- public Canvas() {
- shapes = new ArrayList<>();
- }
- public Canvas(List<Shape> shapes) {
- shapes = new ArrayList<>();
- }
- private void addShape(Shape shape) {
- if(shapes.size()==0){
- shapes.add(shape);
- return;
- }
- for (int i = 0; i < shapes.size(); i++) {
- if (shape.weight() > shapes.get(i).weight()) {
- shapes.add(i, shape);
- return;
- }
- }
- shapes.add(shape);
- }
- void add(String id, Color color, float radius) {
- Circle circleToAdd = new Circle(id, color, radius);
- addShape(circleToAdd);
- }
- void add(String id, Color color, float width, float height) {
- Rectangle rectangleToAdd = new Rectangle(id, color, width, height);
- addShape(rectangleToAdd);
- }
- void scale(String id, float scaleFactor) {
- for (int i = 0; i < shapes.size(); i++) {
- if (shapes.get(i).id.equals(id)) {
- Shape toRemove = shapes.get(i);
- shapes.remove(toRemove);
- toRemove.scale(scaleFactor);
- addShape(toRemove);
- }
- }
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- for(int i = 0; i<shapes.size(); i++){
- sb.append(shapes.get(i).toString());
- }
- return sb.toString();
- // return shapes.stream().map(Shape::toString).collect(Collectors.joining());
- }
- }
- enum Color {
- RED, GREEN, BLUE
- }
- public class ShapesTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Canvas canvas = new Canvas();
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- String[] parts = line.split(" ");
- int type = Integer.parseInt(parts[0]);
- String id = parts[1];
- if (type == 1) {
- Color color = Color.valueOf(parts[2]);
- float radius = Float.parseFloat(parts[3]);
- canvas.add(id, color, radius);
- } else if (type == 2) {
- Color color = Color.valueOf(parts[2]);
- float width = Float.parseFloat(parts[3]);
- float height = Float.parseFloat(parts[4]);
- canvas.add(id, color, width, height);
- } else if (type == 3) {
- float scaleFactor = Float.parseFloat(parts[2]);
- System.out.println("ORIGNAL:");
- System.out.print(canvas);
- canvas.scale(id, scaleFactor);
- System.out.printf("AFTER SCALING: %s %.2f\n", id, scaleFactor);
- System.out.print(canvas);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement