diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..751c68c76f --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,20 @@ +package core.basesyntax; + +public class Circle extends Figure { + private int radius; + + public Circle(int radius, String color) { + super(color); + this.radius = radius; + } + + public double getArea() { + return Math.PI * radius * radius; + } + + @Override + public String printInformation() { + return "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..ce55c1624b --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +public enum Color { + BLUE, + YELLOW, + BLACK, + BROWN, + PINK, + GREEN, + WHITE +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..b1cfbb335e --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,12 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + private final Random random = new Random(); + + public String getRandomColor() { + int index = random.nextInt(Color.values().length); + return Color.values()[index].name(); + } +} diff --git a/src/main/java/core/basesyntax/Ellipse.java b/src/main/java/core/basesyntax/Ellipse.java new file mode 100644 index 0000000000..255f5b8d96 --- /dev/null +++ b/src/main/java/core/basesyntax/Ellipse.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Ellipse extends Figure { + private double lengthA; + private double lengthB; + + public Ellipse(double sideA, double lengthB, String color) { + super(color); + this.lengthA = sideA; + this.lengthB = lengthB; + } + + @Override + public double getArea() { + return Math.PI * lengthA * lengthB; + } + + @Override + public String printInformation() { + return "Figure: Ellipse, area: " + getArea() + + "sq. units, lengthA: " + lengthA + " units, lengthB: " + + lengthB + " units, color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..d57c0f5c84 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public abstract class Figure implements FigureSquare, FigureInformation { + private String color; + + public Figure(String color) { + this.color = color; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public abstract double getArea(); + + public abstract String printInformation(); +} diff --git a/src/main/java/core/basesyntax/FigureInformation.java b/src/main/java/core/basesyntax/FigureInformation.java new file mode 100644 index 0000000000..5c321eee7d --- /dev/null +++ b/src/main/java/core/basesyntax/FigureInformation.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface FigureInformation { + String printInformation(); +} diff --git a/src/main/java/core/basesyntax/FigureSquare.java b/src/main/java/core/basesyntax/FigureSquare.java new file mode 100644 index 0000000000..fbdfa19d06 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSquare.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface FigureSquare { + double getArea(); +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..854450b08f --- /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 static final int FIRST_MAX_NUMBER = 5; + private static final int SECOND_MAX_NUMBER = 100; + private static final int RADIUS = 10; + private final Random random; + private final ColorSupplier colorSupplier; + + public FigureSupplier(Random random, ColorSupplier colorSupplier) { + this.random = random; + this.colorSupplier = colorSupplier; + } + + public Figure getRandomFigure() { + int max1 = random.nextInt(SECOND_MAX_NUMBER) + 1; + int max2 = random.nextInt(SECOND_MAX_NUMBER) + 1; + String randomColor = colorSupplier.getRandomColor(); + Figure figure = switch (random.nextInt(FIRST_MAX_NUMBER) + 1) { + case 1 -> new Rectangle(max1, max2, randomColor); + case 2 -> new RightTriangle(max1, max2, randomColor); + case 3 -> new Ellipse(max1, max2, randomColor); + case 4 -> new Square(max1, randomColor); + default -> new IsoscelesTrapezoid(max1, max2, max2, randomColor); + }; + return figure; + } + + public Figure getDefaultFigure() { + return new Circle(RADIUS, "WHITE"); + } +} 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..50dd8ca4df --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private double sideA; + private double sideB; + private double height; + + public IsoscelesTrapezoid(double sideA, double sideB, double height, String color) { + super(color); + this.sideA = sideA; + this.sideB = sideB; + this.height = height; + } + + @Override + public double getArea() { + return (sideA + sideB) / 2 * height; + } + + @Override + public String printInformation() { + return "Figure: IsoscelesTrapezoid, area: " + getArea() + + "sq. units, sideA: " + sideA + " units, sideB: " + sideB + " units, height: " + + height + " units, color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/MainApp.java b/src/main/java/core/basesyntax/MainApp.java new file mode 100644 index 0000000000..759d3bc13a --- /dev/null +++ b/src/main/java/core/basesyntax/MainApp.java @@ -0,0 +1,22 @@ +package core.basesyntax; + +import java.util.Random; + +public class MainApp { + public static void main(String[] args) { + ColorSupplier colorSupplier = new ColorSupplier(); + Random random = new Random(); + + FigureSupplier figureSupplier = new FigureSupplier(random, colorSupplier); + Figure[] figures = new Figure[6]; + + for (int i = 0; i < figures.length; i++) { + if (i < figures.length / 2) { + figures[i] = figureSupplier.getRandomFigure(); + } else { + figures[i] = figureSupplier.getDefaultFigure(); + } + System.out.println(figures[i].printInformation()); + } + } +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..a560c9d3f0 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private double sideA; + private double sideB; + + public Rectangle(double sideA, double sideB, String color) { + super(color); + this.sideA = sideA; + this.sideB = sideB; + } + + @Override + public double getArea() { + return sideA * sideB; + } + + @Override + public String printInformation() { + return "Figure: Rectangle, area: " + getArea() + + "sq. units, sideA: " + sideA + " units, sideB: " + + sideB + " 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..4b8e388d88 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private double base; + private double height; + + public RightTriangle(double base, double height, String color) { + super(color); + this.base = base; + this.height = height; + } + + @Override + public double getArea() { + return base * height / 2; + } + + @Override + public String printInformation() { + return "Figure: RightTriangle, area: " + getArea() + + "sq. units, base: " + base + " units, height: " + + height + " 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..919f6182e3 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public class Square extends Figure { + private int side; + + public Square(int side, String color) { + super(color); + this.side = side; + } + + @Override + public double getArea() { + return side * side; + } + + @Override + public String printInformation() { + return "Figure: Square, area: " + getArea() + + "sq. units, side: " + side + " units, color: " + getColor(); + } +}