diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java index 97db782bf7..1617d8175f 100644 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ b/src/main/java/core/basesyntax/HelloWorld.java @@ -1,8 +1,25 @@ package core.basesyntax; -/** - * Feel free to remove this class and create your own. - */ +import core.basesyntax.figures.Figure; +import core.basesyntax.service.FigureSupplier; +import core.basesyntax.variables.Constants; + public class HelloWorld { + public static void main(String[] args) { + FigureSupplier figureSupplier = new FigureSupplier(); + + final Figure[] figures = new Figure[Constants.NUMBER_OF_FIGURES]; + + for (int i = 0; i < Constants.HALF; i++) { + figures[i] = figureSupplier.getRandomFigure(); + } + + for (int i = Constants.HALF; i < Constants.NUMBER_OF_FIGURES; i++) { + figures[i] = figureSupplier.getDefaultFigure(); + } + for (Figure figure : figures) { + System.out.println(figure); + } + } } diff --git a/src/main/java/core/basesyntax/figures/Circle.java b/src/main/java/core/basesyntax/figures/Circle.java new file mode 100644 index 0000000000..6e086e6c50 --- /dev/null +++ b/src/main/java/core/basesyntax/figures/Circle.java @@ -0,0 +1,34 @@ +package core.basesyntax.figures; + +import core.basesyntax.variables.Color; + +public class Circle extends Figure { + private double radius; + + public Circle(Color color, double radius) { + super(color); + this.radius = radius; + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + this.radius = radius; + } + + @Override + public double calculateArea() { + return 2 * Math.pow(radius, 2) * Math.PI; + } + + @Override + public String toString() { + return "Figure: circle" + + ", area: " + calculateArea() + + " sq. units" + + ", radius: " + getRadius() + + ", color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/figures/Figure.java b/src/main/java/core/basesyntax/figures/Figure.java new file mode 100644 index 0000000000..0ffc009df8 --- /dev/null +++ b/src/main/java/core/basesyntax/figures/Figure.java @@ -0,0 +1,19 @@ +package core.basesyntax.figures; + +import core.basesyntax.variables.Color; + +public abstract class Figure implements FigureBehavior { + private Color color; + + public Figure(Color color) { + this.color = color; + } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } +} diff --git a/src/main/java/core/basesyntax/figures/FigureBehavior.java b/src/main/java/core/basesyntax/figures/FigureBehavior.java new file mode 100644 index 0000000000..dd5e77245b --- /dev/null +++ b/src/main/java/core/basesyntax/figures/FigureBehavior.java @@ -0,0 +1,5 @@ +package core.basesyntax.figures; + +public interface FigureBehavior { + double calculateArea(); +} diff --git a/src/main/java/core/basesyntax/figures/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/figures/IsoscelesTrapezoid.java new file mode 100644 index 0000000000..1ccc49be5a --- /dev/null +++ b/src/main/java/core/basesyntax/figures/IsoscelesTrapezoid.java @@ -0,0 +1,65 @@ +package core.basesyntax.figures; + +import core.basesyntax.variables.Color; + +public class IsoscelesTrapezoid extends Figure { + private double top; + private double bottom; + private double height; + + public IsoscelesTrapezoid(Color color, double top, double bottom, double side) { + super(color); + this.top = top; + this.bottom = bottom; + this.height = side; + } + + public double getTop() { + return top; + } + + public void setTop(double top) { + this.top = top; + } + + public double getBottom() { + return bottom; + } + + public void setBottom(double bottom) { + this.bottom = bottom; + } + + public double getSide() { + return height; + } + + public void setSide(double side) { + this.height = side; + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } + + @Override + public double calculateArea() { + return (top + bottom) * height / 2; + } + + @Override + public String toString() { + return "Figure: right triangle" + + ", area: " + calculateArea() + + " sq. units" + + ", top: " + getTop() + + ", bottom: " + getBottom() + + ", side: " + getSide() + + ", height: " + getHeight() + + ", color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/figures/Rectangle.java b/src/main/java/core/basesyntax/figures/Rectangle.java new file mode 100644 index 0000000000..d16aede3f1 --- /dev/null +++ b/src/main/java/core/basesyntax/figures/Rectangle.java @@ -0,0 +1,45 @@ +package core.basesyntax.figures; + +import core.basesyntax.variables.Color; + +public class Rectangle extends Figure { + private double firstSide; + private double secondSide; + + public Rectangle(Color color, double firstSide, double secondSide) { + super(color); + this.firstSide = firstSide; + this.secondSide = secondSide; + } + + public double getFirstSide() { + return firstSide; + } + + public void setFirstSide(double firstSide) { + this.firstSide = firstSide; + } + + public double getSecondSide() { + return secondSide; + } + + public void setSecondSide(double secondSide) { + this.secondSide = secondSide; + } + + @Override + public double calculateArea() { + return firstSide * secondSide; + } + + @Override + public String toString() { + return "Figure: rectangle" + + ", area: " + calculateArea() + + " sq. units" + + ", first side: " + getFirstSide() + + ", second side: " + getSecondSide() + + ", color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/figures/RightTriangle.java b/src/main/java/core/basesyntax/figures/RightTriangle.java new file mode 100644 index 0000000000..11e3225d63 --- /dev/null +++ b/src/main/java/core/basesyntax/figures/RightTriangle.java @@ -0,0 +1,45 @@ +package core.basesyntax.figures; + +import core.basesyntax.variables.Color; + +public class RightTriangle extends Figure { + private double firstLeg; + private double secondLeg; + + public RightTriangle(Color color, double firstLeg, double secondLeg) { + super(color); + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + } + + public double getFirstLeg() { + return firstLeg; + } + + public void setFirstLeg(double firstLeg) { + this.firstLeg = firstLeg; + } + + public double getSecondLeg() { + return secondLeg; + } + + public void setSecondLeg(double secondLeg) { + this.secondLeg = secondLeg; + } + + @Override + public double calculateArea() { + return firstLeg * secondLeg / 2; + } + + @Override + public String toString() { + return "Figure: right triangle" + + ", area: " + calculateArea() + + " sq. units" + + ", first leg: " + getFirstLeg() + + ", second leg: " + getSecondLeg() + + ", color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/figures/Square.java b/src/main/java/core/basesyntax/figures/Square.java new file mode 100644 index 0000000000..1a8e908c6e --- /dev/null +++ b/src/main/java/core/basesyntax/figures/Square.java @@ -0,0 +1,34 @@ +package core.basesyntax.figures; + +import core.basesyntax.variables.Color; + +public class Square extends Figure { + private double side; + + public Square(Color color, double side) { + super(color); + this.side = side; + } + + public double getSide() { + return side; + } + + public void setSide(double side) { + this.side = side; + } + + @Override + public double calculateArea() { + return Math.pow(side, 2); + } + + @Override + public String toString() { + return "Figure: square" + + ", area: " + calculateArea() + + " sq. units" + + ", side: " + getSide() + + ", color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/service/ColorSupplier.java b/src/main/java/core/basesyntax/service/ColorSupplier.java new file mode 100644 index 0000000000..f338dd8c4d --- /dev/null +++ b/src/main/java/core/basesyntax/service/ColorSupplier.java @@ -0,0 +1,14 @@ +package core.basesyntax.service; + +import core.basesyntax.variables.Color; +import java.util.Random; + +public class ColorSupplier { + private static final Random RANDOM = new Random(); + + public Color getRandomColor() { + int index = RANDOM.nextInt(Color.values().length); + + return Color.values()[index]; + } +} diff --git a/src/main/java/core/basesyntax/service/FigureSupplier.java b/src/main/java/core/basesyntax/service/FigureSupplier.java new file mode 100644 index 0000000000..9407bf13e3 --- /dev/null +++ b/src/main/java/core/basesyntax/service/FigureSupplier.java @@ -0,0 +1,51 @@ +package core.basesyntax.service; + +import core.basesyntax.figures.Circle; +import core.basesyntax.figures.Figure; +import core.basesyntax.figures.IsoscelesTrapezoid; +import core.basesyntax.figures.Rectangle; +import core.basesyntax.figures.RightTriangle; +import core.basesyntax.figures.Square; +import core.basesyntax.variables.Color; +import core.basesyntax.variables.FigureType; +import java.util.Random; + +public class FigureSupplier { + private static final Random RANDOM = new Random(); + private static final ColorSupplier colorSupplier = new ColorSupplier(); + private static final double DEFAULT_RADIUS = 10; + + public Figure getRandomFigure() { + int index = RANDOM.nextInt(FigureType.values().length); + FigureType figureType = FigureType.values()[index]; + Color color = colorSupplier.getRandomColor(); + + return switch (figureType) { + case SQUARE -> new Square(color, RANDOM.nextDouble()); + case RECTANGLE -> new Rectangle( + color, + RANDOM.nextDouble(), + RANDOM.nextDouble() + ); + case RIGHT_TRIANGLE -> new RightTriangle( + color, + RANDOM.nextDouble(), + RANDOM.nextDouble() + ); + case CIRCLE -> new Circle( + color, + RANDOM.nextDouble() + ); + default -> new IsoscelesTrapezoid( + color, + RANDOM.nextDouble(), + RANDOM.nextDouble(), + RANDOM.nextDouble() + ); + }; + } + + public Figure getDefaultFigure() { + return new Circle(Color.WHITE, DEFAULT_RADIUS); + } +} diff --git a/src/main/java/core/basesyntax/variables/Color.java b/src/main/java/core/basesyntax/variables/Color.java new file mode 100644 index 0000000000..c7ce86d2f3 --- /dev/null +++ b/src/main/java/core/basesyntax/variables/Color.java @@ -0,0 +1,11 @@ +package core.basesyntax.variables; + +public enum Color { + RED, + YELLOW, + BLUE, + GREEN, + GRAY, + BLACK, + WHITE +} diff --git a/src/main/java/core/basesyntax/variables/Constants.java b/src/main/java/core/basesyntax/variables/Constants.java new file mode 100644 index 0000000000..b3877b27eb --- /dev/null +++ b/src/main/java/core/basesyntax/variables/Constants.java @@ -0,0 +1,6 @@ +package core.basesyntax.variables; + +public class Constants { + public static final int NUMBER_OF_FIGURES = 10; + public static final int HALF = NUMBER_OF_FIGURES / 2; +} diff --git a/src/main/java/core/basesyntax/variables/FigureType.java b/src/main/java/core/basesyntax/variables/FigureType.java new file mode 100644 index 0000000000..c452bc99dd --- /dev/null +++ b/src/main/java/core/basesyntax/variables/FigureType.java @@ -0,0 +1,9 @@ +package core.basesyntax.variables; + +public enum FigureType { + SQUARE, + RECTANGLE, + RIGHT_TRIANGLE, + CIRCLE, + ISOSCELES_TRAPEZOID +}