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..3c541f812c --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,22 @@ +package core.basesyntax; + +public class Circle extends Figure { + private double radius; + + public Circle(Color color, double radius) { + super(color); + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * (radius * radius); + } + + @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..4fbf0d5da8 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,12 @@ +package core.basesyntax; + +public enum Color { + WHITE, + BLACK, + PURPLE, + ORANGE, + GREEN, + BROWN, + BLUE, + YELLOW +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..a85911c25b --- /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 Color getRandomColor() { + int index = random.nextInt(Color.values().length); + return Color.values()[index]; + } +} 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..4caa3ab82c --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +public abstract class Figure implements Drawable, AreaCalculator { + private Color color; + + public Figure(Color color) { + this.color = color; + } + + public String getColor() { + return color.name().toLowerCase(); + } +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..be131733d2 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,46 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private static final int MAX_FIGURE_NUMBER = 5; + private static final int MAX_VALUE = 100; + private static final int MIN_VALUE = 1; + private static final double DEFAULT_RADIUS = 10; + + private Random random = new Random(); + private ColorSupplier supplier = new ColorSupplier(); + + public Figure getRandomFigure() { + int randomFigureNumber = random.nextInt(MAX_FIGURE_NUMBER); + Color randomColor = supplier.getRandomColor(); + + switch (randomFigureNumber) { + case 0: + return new Circle(randomColor, + getRandomNumber()); + case 1: + return new IsoscelesTrapezoid(randomColor, + getRandomNumber(), getRandomNumber(), getRandomNumber()); + case 2: + return new Rectangle(randomColor, + getRandomNumber(), getRandomNumber()); + case 3: + return new RightTriangle(randomColor, + getRandomNumber(), getRandomNumber()); + case 4: + return new Square(randomColor, + getRandomNumber()); + default: + return getDefaultFigure(); + } + } + + public Figure getDefaultFigure() { + return new Circle(Color.WHITE, DEFAULT_RADIUS); + } + + private int getRandomNumber() { + return random.nextInt(MAX_VALUE) + MIN_VALUE; + } +} 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..5c39c1194a --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,37 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private double topParallelSide; + private double bottomParallelSide; + private double side; + + public IsoscelesTrapezoid( + Color color, double topParallelSide, + double bottomParallelSide, double side) { + super(color); + if (topParallelSide < bottomParallelSide) { + this.topParallelSide = topParallelSide; + this.bottomParallelSide = bottomParallelSide; + } else { + this.topParallelSide = bottomParallelSide; + this.bottomParallelSide = topParallelSide; + } + this.side = side; + } + + @Override + public double getArea() { + return ((topParallelSide + bottomParallelSide) / 2) + * (Math.sqrt(side * side + - Math.pow((bottomParallelSide - topParallelSide) / 2, 2))); + } + + @Override + public void draw() { + System.out.println("Figure: isosceles trapezoid, area: " + getArea() + " sq.units, " + + "topParallelSide: " + topParallelSide + " units, " + + "bottomParallelSide: " + bottomParallelSide + " units, " + + "side: " + side + " units, " + + "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..58499a7494 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,19 @@ +package core.basesyntax; + +public class Main { + private static final int SIZE = 6; + + public static void main(String[] args) { + Figure[] figures = new Figure[SIZE]; + FigureSupplier supplier = new FigureSupplier(); + + for (int i = 0; i < SIZE; i++) { + if (SIZE / 2 > i) { + figures[i] = supplier.getRandomFigure(); + } else { + figures[i] = supplier.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..30a7928c77 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private double height; + private double width; + + public Rectangle(Color color, double width, double height) { + super(color); + this.width = width; + this.height = height; + } + + @Override + public double getArea() { + return height * width; + } + + @Override + public void draw() { + System.out.println("Figure: rectangle, area: " + getArea() + " sq.units, " + + "height: " + height + " units, width: " + width + " 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..daebe84a27 --- /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("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..aca799fe45 --- /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("Figure: square, area: " + getArea() + " sq.units, " + + "side: " + side + " units, " + + "color: " + getColor()); + } +}