Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Scanner;
- enum Color {
- RED, GREEN, BLUE
- }
- interface Scalable{
- public void scale(float scaleFactor);
- }
- interface Stackable{
- public float weight();
- }
- abstract class Shape implements Scalable,Stackable{
- String id;
- Color color;
- Shape(){}
- Shape(String id, Color color){
- this.id=id;
- this.color=color;
- }
- abstract public void scale(float scaleFactor);
- abstract public float weight();
- abstract public String type();
- }
- class Circle extends Shape implements Scalable,Stackable{
- float radius;
- Circle(float r){
- super();
- radius=r;
- }
- Circle(String id, Color color, float radius){
- super(id,color);
- this.radius=radius;
- }
- public void scale(float scaleFactor) {
- radius*=scaleFactor;
- }
- public float weight() {
- return (radius*radius*(float)Math.PI);
- }
- public String type() {
- return "Circle";
- }
- }
- class Rectangle extends Shape implements Scalable,Stackable{
- float width,height;
- Rectangle(int w,int h){
- super();
- width=w;
- height=h;
- }
- Rectangle(String id, Color color, float width, float height){
- super(id,color);
- this.width=width;
- this.height=height;
- }
- public void scale(float scaleFactor) {
- width*=scaleFactor;
- height*=scaleFactor;
- }
- public float weight() {
- return (height*width);
- }
- public String type() {
- return "Rectangle";
- }
- }
- class Canvas{
- List<Shape> list;
- Canvas(){
- list=new ArrayList<>();
- }
- void add(String id, Color color, float radius) {
- if(list.size()==0) {
- list.add(new Circle(id,color,radius));
- }
- Circle c=new Circle(id,color,radius);
- for(int i=0;i<list.size();i++) {
- if(c.weight()>list.get(i).weight()) {
- list.add(i, c);
- return;
- }
- }
- if(c.weight()==list.get(list.size()-1).weight()) return;
- list.add(c);
- }
- void add(String id, Color color, float width, float height) {
- if(list.size()==0) {
- list.add(new Rectangle(id,color,width,height));
- }
- Rectangle r=new Rectangle(id,color,width,height);
- for(int i=0;i<list.size();i++) {
- if(r.weight()>list.get(i).weight()) {
- list.add(i, r);
- return;
- }
- }
- if(r.weight()==list.get(list.size()-1).weight()) return;
- list.add(r);
- }
- void scale(String id,float scaleFactor) {
- int index=-1;
- for(int i=0;i<list.size();i++) {
- if(list.get(i).id.equals(id)) {
- list.get(i).scale(scaleFactor);
- index=i;
- break;
- }
- }
- if(index==-1) return;
- if(list.get(index).type().equals("Circle")) {
- Circle c=(Circle)list.get(index);
- list.remove(index);
- this.add(c.id, c.color, c.radius);
- }
- if(list.get(index).type().equals("Rectangle")) {
- Rectangle r=(Rectangle)list.get(index);
- list.remove(index);
- this.add(r.id, r.color, r.width, r.height);
- }
- }
- public String toString() {
- StringBuilder sb=new StringBuilder();
- //System.out.println(list.size()+" size of list");
- for(Shape c:list) {
- if(c.type().equals("Circle")) {
- sb.append(String.format("C: %-5s%-10s%10.2f\n", c.id,c.color, c.weight()));
- }
- if(c.type().equals("Rectangle")) {
- sb.append(String.format("R: %-5s%-10s%10.2f\n", c.id,c.color, c.weight()));
- }
- }
- return sb.toString();
- }
- }
- 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