From d65d1c72203e6ae91b5a68ffb48fe61c5dff94ca Mon Sep 17 00:00:00 2001 From: Yura9827 Date: Fri, 18 Oct 2024 02:03:58 +0300 Subject: [PATCH] jv-oop-advanced the task is completed --- src/main/java/core/basesyntax/Circle.java | 26 ++++++++++++++ src/main/java/core/basesyntax/Color.java | 9 +++++ .../java/core/basesyntax/ColorSupplier.java | 10 ++++++ src/main/java/core/basesyntax/Default.java | 29 +++++++++++++++ src/main/java/core/basesyntax/Figure.java | 9 +++++ .../java/core/basesyntax/FigureSupplier.java | 34 ++++++++++++++++++ src/main/java/core/basesyntax/HelloWorld.java | 8 ----- .../core/basesyntax/IsoscelesTrapezoid.java | 36 +++++++++++++++++++ src/main/java/core/basesyntax/Main.java | 20 +++++++++++ src/main/java/core/basesyntax/Rectangle.java | 33 +++++++++++++++++ .../java/core/basesyntax/RightTriangle.java | 34 ++++++++++++++++++ src/main/java/core/basesyntax/Square.java | 27 ++++++++++++++ 12 files changed, 267 insertions(+), 8 deletions(-) create mode 100644 src/main/java/core/basesyntax/Circle.java create mode 100644 src/main/java/core/basesyntax/Color.java create mode 100644 src/main/java/core/basesyntax/ColorSupplier.java create mode 100644 src/main/java/core/basesyntax/Default.java create mode 100644 src/main/java/core/basesyntax/Figure.java create mode 100644 src/main/java/core/basesyntax/FigureSupplier.java delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/main/java/core/basesyntax/IsoscelesTrapezoid.java create mode 100644 src/main/java/core/basesyntax/Main.java create mode 100644 src/main/java/core/basesyntax/Rectangle.java create mode 100644 src/main/java/core/basesyntax/RightTriangle.java create mode 100644 src/main/java/core/basesyntax/Square.java diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..d5f273c222 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class Circle extends Default { + private double size; + + public Circle(String color, double size) { + super(color); + this.size = size; + } + + @Override + public double getArea() { + return size * size; + } + + @Override + public double getProperty() { + return size; + } + + @Override + public void draw() { + System.out.println("Circle: area: " + Math.round(getArea()) + + ", Radius: " + Math.round(getProperty()) + ", Color: " + getColor()); + } +} diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java new file mode 100644 index 0000000000..77083f11c7 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,9 @@ +package core.basesyntax; + +public enum Color { + WHITE, + GREEN, + BLUE, + YELLOW, + ORANGE +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..a27995eca2 --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,10 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + public String getRandomColor() { + int index = new Random().nextInt(Color.values().length); + return Color.values()[index].name(); + } +} diff --git a/src/main/java/core/basesyntax/Default.java b/src/main/java/core/basesyntax/Default.java new file mode 100644 index 0000000000..518851aa0a --- /dev/null +++ b/src/main/java/core/basesyntax/Default.java @@ -0,0 +1,29 @@ +package core.basesyntax; + +public class Default implements Figure { + private String color; + + public Default(String color) { + this.color = color; + } + + @Override + public double getArea() { + return 0; + } + + @Override + public double getProperty() { + return 0; + } + + @Override + public void draw() { + + } + + public String getColor() { + return color; + } + +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..8e2c6a390e --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,9 @@ +package core.basesyntax; + +public interface Figure { + double getArea(); + + double getProperty(); + + void draw(); +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..b088f70bf9 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,34 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private ColorSupplier colorSupplier = new ColorSupplier(); + private Random random = new Random(); + private int randomNumber = random.nextInt(5); + + public Figure getRandomFigure() { + switch (randomNumber) { + case 0: + return new Square(colorSupplier.getRandomColor(), random.nextDouble() * 10); + case 1: + return new Circle(colorSupplier.getRandomColor(), random.nextDouble() * 10); + case 2: + return new Rectangle(colorSupplier.getRandomColor(), random.nextDouble() * 10, + random.nextDouble() * 10, random.nextDouble() * 10); + case 3: + return new RightTriangle(colorSupplier.getRandomColor(), random.nextDouble() * 10, + random.nextDouble() * 10, random.nextDouble() * 10); + default: + return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), + random.nextDouble() * 10, + random.nextDouble() * 10, + random.nextDouble() * 10, + random.nextDouble() * 10); + } + } + + public Figure getDefaultFigure() { + return new Circle("WHITE", 10); + } +} diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index 97db782bf7..0000000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,8 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { - -} diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java new file mode 100644 index 0000000000..c089fec37b --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,36 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Default { + private double size; + private double side1; + private double side2; + private double length; + + public IsoscelesTrapezoid(String color, double size, double side1, + double side2, double length) { + super(color); + this.size = size; + this.side1 = side1; + this.side2 = side2; + this.length = length; + } + + @Override + public double getArea() { + return size * size; + } + + @Override + public double getProperty() { + return 0.5 * (side1 * side2) * length; + } + + @Override + public void draw() { + System.out.println("IsoscelesTrapezoid: area: " + Math.round(getArea()) + + ", Property: " + Math.round(getProperty()) + + ", Color: " + getColor() + ", side1: " + Math.round(side1) + + ", side2: " + Math.round(side2) + + ", length: " + Math.round(length)); + } +} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..16a7087452 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,20 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + FigureSupplier figureSupplier = new FigureSupplier(); + Figure[] figures = new Figure[4]; + + for (int i = 0; i < figures.length / 2; i++) { + figures[i] = figureSupplier.getRandomFigure(); + } + + for (int i = figures.length / 2; i < figures.length; i++) { + figures[i] = figureSupplier.getDefaultFigure(); + } + + for (Figure figure : figures) { + figure.draw(); + } + } +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..245e507505 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,33 @@ +package core.basesyntax; + +public class Rectangle extends Default { + private double side; + private double firstLeg; + private double secondLeg; + + public Rectangle(String color, double side, double firstLeg, double secondLeg) { + super(color); + this.side = side; + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + } + + @Override + public double getProperty() { + return firstLeg * secondLeg; + } + + @Override + public double getArea() { + return side * side; + } + + @Override + public void draw() { + System.out.println("Rectangle: area: " + Math.round(getArea()) + + " Property: " + Math.round(getProperty()) + + " Color: " + getColor() + + " firstLeg: " + Math.round(firstLeg) + + " secondLeg: " + Math.round(secondLeg)); + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..c0827a94c9 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,34 @@ +package core.basesyntax; + +public class RightTriangle extends Default { + private double size; + private double firstLeg; + private double secondLeg; + + public RightTriangle(String color, double size, double firstLeg, double secondLeg) { + super(color); + this.size = size; + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + } + + @Override + public double getArea() { + + return size * size; + } + + @Override + public double getProperty() { + return 0.5 * firstLeg * secondLeg; + } + + @Override + public void draw() { + System.out.println("RightTriangle: area: " + Math.round(getArea()) + + ", Property: " + Math.round(getProperty()) + + ", Color: " + getColor() + + ", firstLeg: " + Math.round(firstLeg) + + ", secondLeg: " + Math.round(secondLeg)); + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..cd95a6451d --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,27 @@ +package core.basesyntax; + +public class Square extends Default { + private double size; + + public Square(String color,double size) { + super(color); + this.size = size; + } + + @Override + public double getProperty() { + return 4 * size; + } + + @Override + public double getArea() { + return size * size; + } + + @Override + public void draw() { + System.out.println("Square: color = " + getColor() + + ", size = " + Math.round(size) + + ", Perimeter = " + Math.round(getProperty())); + } +}