Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.stream.Collectors;
- interface Scalable {
- public void scale(float scaleFactor);
- }
- interface Stackable {
- public float weight();
- }
- abstract class Shape implements Stackable,Scalable,Comparable<Shape> {
- private String id;
- private Color color;
- public String getId() {
- return id;
- }
- public Color getColor() {
- return color;
- }
- public Shape(String id, Color color) {
- this.id = id;
- this.color = color;
- }
- }
- class Rectangle extends Shape{
- float width;
- float heigth;
- public Rectangle(String id, Color color,float width, float height) {
- super(id, color);
- this.width=width;
- this.heigth=height;
- }
- @Override
- public float weight() {
- return width*heigth;
- }
- @Override
- public void scale(float scaleFactor) {
- width*=scaleFactor;
- heigth*=scaleFactor;
- }
- @Override
- public int compareTo(Shape o) {
- return Float.compare(this.weight(),o.weight());
- }
- @Override
- public String toString() {
- return String.format("R: %-5s%-10s%10.2f",super.getId(),super.getColor().name(),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)(radius*radius*Math.PI);
- }
- @Override
- public int compareTo(Shape o) {
- return Float.compare(this.weight(),o.weight());
- }
- @Override
- public String toString() {
- return String.format("C: %-5s%-10s%10.2f",super.getId(),super.getColor().name(),weight());
- }
- }
- 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);
- }
- }
- }
- }
- class Canvas {
- List<Shape> shapes;
- public Canvas() {
- this.shapes= new ArrayList<>();
- }
- public void add(String id, Color color, float radius) {
- shapes.add(new Circle(id,color,radius));
- }
- public void add(String id, Color color, float width, float height) {
- shapes.add(new Rectangle(id,color,width,height));
- }
- public void scale(String id, float scaleFactor) {
- shapes.stream().filter(i->i.getId().equals(id)).findAny().get().scale(scaleFactor);
- }
- @Override
- public String toString() {
- StringBuilder sb= new StringBuilder();
- shapes= shapes.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
- for(int i=0;i<shapes.size();i++){
- sb.append(shapes.get(i).toString());
- sb.append("\n");
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement