diff --git a/src/main/java/core/basesyntax/AreaCalculable.java b/src/main/java/core/basesyntax/AreaCalculable.java new file mode 100644 index 0000000000..885f8651fc --- /dev/null +++ b/src/main/java/core/basesyntax/AreaCalculable.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface AreaCalculable { + double getArea(); +} diff --git a/src/main/java/core/basesyntax/Calculator.java b/src/main/java/core/basesyntax/Calculator.java new file mode 100644 index 0000000000..2b08f42202 --- /dev/null +++ b/src/main/java/core/basesyntax/Calculator.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Calculator { + + private static final int FIGURE_ARRAY_SIZE = 6; + + public static void main(String[] args) { + + FigureSupplier figureSupplier = new FigureSupplier(); + Figure[] figures = new Figure[FIGURE_ARRAY_SIZE]; + + 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/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..87b4048f73 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class Circle extends Figure { + + private final double radius; + + public Circle(String randomColor, double randomRadius) { + super(randomColor); + this.radius = randomRadius; + } + + @Override + public double getArea() { + return Math.PI * Math.pow(radius, 2); + } + + @Override + public void draw() { + System.out.println("Figure: circle, area: " + getArea() + + " sq. units, radius: " + radius + + " units, 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..ff1d8418b0 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +public enum Color { + YELLOW, + BLUE, + WHITE, + GREEN, + PURPLE, + BLACK, + RED +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..67882131c7 --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + + private final Random random = new Random(); + + public String getRandomColor() { + return Color.values()[random.nextInt(Color.values().length)].name(); + } + +} diff --git a/src/main/java/core/basesyntax/Drawable.java b/src/main/java/core/basesyntax/Drawable.java new file mode 100644 index 0000000000..d045270178 --- /dev/null +++ b/src/main/java/core/basesyntax/Drawable.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Drawable { + void draw(); +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..30a842df03 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,17 @@ +package core.basesyntax; + +public abstract class Figure implements Drawable, AreaCalculable { + private final String color; + + public Figure(String color) { + this.color = color; + } + + public String getColor() { + return color; + } + + public abstract double getArea(); + + public abstract 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..75e90e5f0d --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,60 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private static final int FIGURE_COUNT = 5; + private static final double MIN_DIMENSION = 1; + private static final double MAX_DIMENSION = 10; + private static final double DEFAULT_RADIUS = 10; + private static final String DEFAULT_COLOR = Color.WHITE.name(); + private final Random random = new Random(); + private final ColorSupplier colorSupplier = new ColorSupplier(); + //Circle + private final double radius = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + //Trapezoid + private final double firstLegTrapezoid = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + private final double secondLegTrapezoid = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + private final double heightTrapezoid = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + //Square + private final double side = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + //Triangle + private final double firstLegTriangle = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + private final double secondLegTriangle = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + //Rectangle + private final double widthRectangle = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + private final double heightRectangle = random.nextDouble(MIN_DIMENSION, MAX_DIMENSION); + + public Figure getRandomFigure() { + switch (random.nextInt(FIGURE_COUNT)) { + case 0 -> { + return new Circle(colorSupplier.getRandomColor(), radius); + } + case 1 -> { + return new Rectangle(colorSupplier.getRandomColor(), + widthRectangle, heightRectangle); + } + case 2 -> { + return new RightTriangle(colorSupplier.getRandomColor(), + firstLegTriangle, secondLegTriangle); + } + case 3 -> { + return new Square(colorSupplier.getRandomColor(), side); + } + case 4 -> { + return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), + firstLegTrapezoid, + secondLegTrapezoid, + heightTrapezoid); + } + default -> { + return getDefaultFigure(); + } + } + } + + public Figure getDefaultFigure() { + return new Circle(DEFAULT_COLOR, DEFAULT_RADIUS); + } + +} 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..6e309909ca --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,30 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + + private final double firstLeg; + private final double secondLeg; + private final double height; + + public IsoscelesTrapezoid(String randomColor, double randomBaseA, + double randomBaseB, double randomHeight) { + super(randomColor); + this.firstLeg = randomBaseA; + this.secondLeg = randomBaseB; + this.height = randomHeight; + } + + @Override + public double getArea() { + return (firstLeg + secondLeg) / 2 * height; + } + + @Override + public void draw() { + System.out.println("Figure: isosceles trapezoid, area: " + getArea() + + " sq. units, firstLeg: " + firstLeg + + " units, secondLeg: " + secondLeg + + " units, height: " + height + + " units, color: " + getColor()); + } +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..1156777689 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + + private final double width; + private final double height; + + public Rectangle(String randomColor, double randomWidth, double randomHeight) { + super(randomColor); + this.width = randomWidth; + this.height = randomHeight; + } + + @Override + public double getArea() { + return width * height; + } + + @Override + public void draw() { + System.out.println("Figure: rectangle, area: " + getArea() + + " sq. units, width: " + width + + " units, height: " + height + + " units, color: " + getColor()); + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..be8abc2205 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + + private final double firstLeg; + private final double secondLeg; + + public RightTriangle(String randomColor, double randomLegA, double randomLegB) { + super(randomColor); + this.firstLeg = randomLegA; + this.secondLeg = randomLegB; + } + + @Override + public double getArea() { + return (firstLeg * secondLeg) / 2; + } + + @Override + public void draw() { + System.out.println("Figure: right triangle, area: " + getArea() + + " sq. units, firstLeg: " + firstLeg + + " units, secondLeg: " + secondLeg + + " units, color: " + getColor()); + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..b96626df8c --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class Square extends Figure { + + private final double side; + + public Square(String randomColor, double randomSide) { + super(randomColor); + this.side = randomSide; + } + + @Override + public double getArea() { + return Math.pow(side, 2); + } + + @Override + public void draw() { + System.out.println("Figure: square, area: " + getArea() + + " sq. units, side: " + side + + " units, color: " + getColor()); + } +}