diff --git a/src/main/java/core/basesyntax/AreaCalculator.java b/src/main/java/core/basesyntax/AreaCalculator.java new file mode 100644 index 0000000000..673262c690 --- /dev/null +++ b/src/main/java/core/basesyntax/AreaCalculator.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface AreaCalculator { + double getArea(); +} diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..2c8480948a --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public class Circle extends Figure { + private int radius; + + public Circle(Color color, int radius) { + super(color); + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } + + @Override + public void draw() { + System.out.println("Circle{" + "radius=" + radius + + ", color='" + getColor() + '\'' + ", area=" + getArea() + '}'); + } +} diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java new file mode 100644 index 0000000000..06c7e5dd48 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,8 @@ +package core.basesyntax; + +public enum Color { + WHITE, + BLACK, + BLUE, + 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..27a45422f4 --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + private final Random rand = new Random(); + + public Color getRandomColor() { + return Color.values()[rand.nextInt(Color.values().length)]; + } +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..c436975181 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +public abstract class Figure implements AreaCalculator, FigureDrawer { + private Color color; + + public Figure(Color color) { + this.color = color; + } + + public Color getColor() { + return color; + } +} diff --git a/src/main/java/core/basesyntax/FigureDrawer.java b/src/main/java/core/basesyntax/FigureDrawer.java new file mode 100644 index 0000000000..c2eed9e53f --- /dev/null +++ b/src/main/java/core/basesyntax/FigureDrawer.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface FigureDrawer { + 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..fbf47da332 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,41 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private static final int MAX_NUMBER = 100; + private static final int DEFAULT_RADIUS = 10; + private static final ColorSupplier colorSupplier = new ColorSupplier(); + private final Random random = new Random(); + + public Figure getRandomFigure() { + FigureType randomFigure = FigureType.values()[random.nextInt(FigureType.values().length)]; + switch (randomFigure) { + case CIRCLE: + return new Circle(colorSupplier.getRandomColor(), + random.nextInt(MAX_NUMBER)); + case SQUARE: + return new Square(colorSupplier.getRandomColor(), + random.nextInt(MAX_NUMBER)); + case RECTANGLE: + return new Rectangle(colorSupplier.getRandomColor(), + random.nextInt(MAX_NUMBER), + random.nextInt(MAX_NUMBER)); + case RIGHT_RECTANGLE: + return new RightTriangle(colorSupplier.getRandomColor(), + random.nextInt(MAX_NUMBER), + random.nextInt(MAX_NUMBER)); + case ISOSCELES_TRAPEZOID: + return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), + random.nextInt(MAX_NUMBER), + random.nextInt(MAX_NUMBER), + random.nextInt(MAX_NUMBER)); + default: + return getDefaultFigure(); + } + } + + public Figure getDefaultFigure() { + return new Circle(Color.WHITE, DEFAULT_RADIUS); + } +} diff --git a/src/main/java/core/basesyntax/FigureType.java b/src/main/java/core/basesyntax/FigureType.java new file mode 100644 index 0000000000..51706acc17 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureType.java @@ -0,0 +1,9 @@ +package core.basesyntax; + +public enum FigureType { + CIRCLE, + SQUARE, + RECTANGLE, + RIGHT_RECTANGLE, + ISOSCELES_TRAPEZOID +} 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..582c5f26fd --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,27 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private double upSide; + private double downSide; + private double height; + + public IsoscelesTrapezoid(Color color, double upSide, double downSide, double height) { + super(color); + this.upSide = upSide; + this.downSide = downSide; + this.height = height; + } + + @Override + public double getArea() { + return (upSide + downSide) * height / 2; + } + + @Override + public void draw() { + System.out.println("IsoscelesTrapezoid{" + "upSide=" + upSide + + ", downSide=" + downSide + + ", height=" + height + ", color='" + + getColor() + '\'' + ", area=" + getArea() + '}'); + } +} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..bee7b536e1 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,18 @@ +package core.basesyntax; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter the number of figures: "); + int number = scanner.nextInt(); + Figure[] figures = new Figure[number]; + FigureSupplier figureSupplier = new FigureSupplier(); + for (int i = 0;i < number;i++) { + figures[i] = (i < number / 2) + ? figureSupplier.getRandomFigure() : figureSupplier.getDefaultFigure(); + figures[i].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..70adec3e0c --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private double firstLeg; + private double secondLeg; + + public Rectangle(Color color, double firstLeg, double secondLeg) { + super(color); + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + } + + @Override + public double getArea() { + return firstLeg * secondLeg; + } + + @Override + public void draw() { + System.out.println("Rectangle{" + "firstLeg=" + firstLeg + + ", secondLeg=" + secondLeg + ", color='" + getColor() + '\'' + + ", area=" + getArea() + '}'); + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..9a350f0533 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +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; + } + + @Override + public double getArea() { + return firstLeg * secondLeg / 2; + } + + @Override + public void draw() { + System.out.println("RightTriangle{" + "firstLeg=" + firstLeg + + ", secondLeg=" + secondLeg + ", color='" + getColor() + '\'' + + ", area=" + getArea() + '}'); + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..1193e9901e --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,22 @@ +package core.basesyntax; + +public class Square extends Figure { + private double side; + + public Square(Color color, double side) { + super(color); + this.side = side; + } + + @Override + public double getArea() { + return side * side; + } + + @Override + public void draw() { + System.out.println("Square{" + "side=" + side + + ", color='" + getColor() + '\'' + + ", area=" + getArea() + '}'); + } +}