diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..42dd3d9f6e --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public class Circle extends Figure { + private double radius; + + public Circle(String color, double radius) { + super(color); + this.radius = radius; + } + + @Override + public void calculateSquare() { + System.out.println("Square circle = " + Math.PI * radius * radius); + } + + @Override + String getInfo() { + return "Figure: Circle, area: " + Math.PI * radius * radius + + " 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..16d6f74208 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,10 @@ +package core.basesyntax; + +public enum Color { + WHITE, + BLUE, + BLACK, + RED, + GREEN, + PINK; +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..1566280b00 --- /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 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/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..5f60985111 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,15 @@ +package core.basesyntax; + +public abstract class Figure implements SquareOfFigures { + private String color; + + public Figure(String color) { + this.color = color; + } + + public String getColor() { + return color; + } + + abstract String getInfo(); +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..77196ac7dd --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,35 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private static final int MAX_FIGURE = 6; + private Random random = new Random(); + private ColorSupplier colorSupplier = new ColorSupplier(); + + public Figure getRandomFigure() { + int randomNumberOfFigure = random.nextInt(MAX_FIGURE); + switch (randomNumberOfFigure) { + case 1: + return new Circle(colorSupplier.getRandomColor(), random.nextInt(10)); + case 2: + return new IsoscelesTrapezoid(random.nextInt(10), + random.nextInt(10), random.nextInt(10), + colorSupplier.getRandomColor()); + case 3: + return new Rectangle(colorSupplier.getRandomColor(), + random.nextInt(10), random.nextInt(10)); + case 4: + return new RightTriangle(colorSupplier.getRandomColor(), + random.nextInt(10), random.nextInt(10)); + case 5: + return new Square(colorSupplier.getRandomColor(), random.nextInt(10)); + default: + return getDefaultFigure(); + } + } + + public Figure getDefaultFigure() { + return new Circle(Color.WHITE.name(), 10); + } +} diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java new file mode 100644 index 0000000000..c7f3668f36 --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,27 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private double sideOne; + private double sideTwo; + private double height; + + public IsoscelesTrapezoid(double sideOne, double sideTwo, double height, String color) { + super(color); + this.sideOne = sideOne; + this.sideTwo = sideTwo; + this.height = height; + } + + @Override + public void calculateSquare() { + System.out.println("Square Of IsoscelesTrapezoid = " + (sideOne + sideTwo) / 2 * height); + + } + + @Override + String getInfo() { + return "Figure: IsoscelesTrapezoid, area: " + (sideOne + sideTwo) / 2 * height + + " sq.units, " + "sideOne " + sideOne + " units, " + "sideTwo " + + sideTwo + " units, " + "height " + height + ", color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..b0ccbd439c --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,16 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + FigureSupplier figureSupplier = new FigureSupplier(); + Figure[] figures = new Figure[6]; + for (int i = 0; i < figures.length; i++) { + if (i < 3) { + figures[i] = figureSupplier.getRandomFigure(); + } else { + figures[i] = figureSupplier.getDefaultFigure(); + } + System.out.println(figures[i].getInfo()); + } + } +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..f93f7dc852 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private double sideOne; + private double sideTwo; + + public Rectangle(String color, double sideOne, double sideTwo) { + super(color); + this.sideOne = sideOne; + this.sideTwo = sideTwo; + } + + @Override + public void calculateSquare() { + System.out.println("Square of Rectangle = " + sideOne * sideTwo); + } + + @Override + String getInfo() { + return "Figure: Rectangle, area: " + sideOne * sideTwo + + " sq.units, " + "sideOne " + sideOne + " units, " + "sideTwo " + + sideTwo + " 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..e55fa644c4 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private double sideOne; + private double sideTwo; + + public RightTriangle(String color, double sideOne, double sideTwo) { + super(color); + this.sideOne = sideOne; + this.sideTwo = sideTwo; + } + + @Override + public void calculateSquare() { + System.out.println("Square of RightTriangle = " + (sideOne * sideTwo) / 2); + } + + @Override + String getInfo() { + return "Figure: RightTriangle, area: " + (sideOne * sideTwo) / 2 + + " sq.units, " + "sideOne " + sideOne + " units, " + "sideTwo " + + sideTwo + " 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..952f11e83e --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public class Square extends Figure { + private double side; + + public Square(String color, double side) { + super(color); + this.side = side; + } + + @Override + public void calculateSquare() { + System.out.println("Square of square = " + side * side); + } + + @Override + String getInfo() { + return "Figure: Square, area: " + side + + " sq.units, " + "side " + side + " units, " + "color: " + getColor(); + } +} diff --git a/src/main/java/core/basesyntax/SquareOfFigures.java b/src/main/java/core/basesyntax/SquareOfFigures.java new file mode 100644 index 0000000000..444deaf655 --- /dev/null +++ b/src/main/java/core/basesyntax/SquareOfFigures.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface SquareOfFigures { + void calculateSquare(); +}