diff --git a/src/main/java/core/basesyntax/AreaCalculatable.java b/src/main/java/core/basesyntax/AreaCalculatable.java new file mode 100644 index 0000000000..d5368c270c --- /dev/null +++ b/src/main/java/core/basesyntax/AreaCalculatable.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface AreaCalculatable { + double calculateArea(); +} diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..3fdb84faba --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,23 @@ +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 calculateArea() { + return Math.PI * radius * radius; + } + + @Override + public void draw() { + System.out.println("Figure: Circle " + + " area " + calculateArea() + " sq.units," + + " side " + 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..6f3e01b780 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +public enum Color { + RED, + BLUE, + GREEN, + YELLOW, + WHITE, + BLACK, + VELVET; +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..cf3dee3e73 --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + private static final Color[] COLORS = Color.values(); + private Random random = new Random(); + + public Color getRandomColor() { + int index = random.nextInt(COLORS.length); + return COLORS[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..907909a277 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +public abstract class Figure implements AreaCalculatable, Drawable { + private Color color; + + public Figure(Color color) { + this.color = color; + } + + public Color getColor() { + return color; + } +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..258a68ee01 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,44 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private static final int FIGURE_AMOUNT = 5; + private static final int FIGURE_PARAMETERS_MAX = 20; + private static final Color DEFAULT_COLOR = Color.WHITE; + private static final int DEFAULT_RADIUS = 10; + private Random random = new Random(); + private ColorSupplier colorSupplier = new ColorSupplier(); + + public Figure getRandomFigure() { + Color randomColor = colorSupplier.getRandomColor(); + int randomFigureNumber = random.nextInt(FIGURE_AMOUNT + 1); + int randomFigureParameter = random.nextInt(FIGURE_PARAMETERS_MAX + 1); + int randomFigureHeight = random.nextInt(FIGURE_PARAMETERS_MAX + 1); + int randomTrapezoidBase = random.nextInt(FIGURE_PARAMETERS_MAX + 1); + switch (randomFigureNumber) { + case 0: { + return new Circle(randomColor, randomFigureParameter); + } + case 1: { + return new Square(randomColor, randomFigureParameter); + } + case 2: { + return new Rectangle(randomColor,randomFigureHeight, + randomFigureParameter); + } + case 3: { + return new RightTriangle(randomColor, + randomFigureParameter, randomFigureHeight); + } + default: { + return new IsoscelesTrapezoid(randomColor, + randomTrapezoidBase, randomFigureParameter, randomFigureHeight); + } + } + } + + public Figure getDefaultFigure() { + return new Circle(DEFAULT_COLOR, DEFAULT_RADIUS); + } +} 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..83801c8a53 --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,29 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private int topBase; + private int bottomBase; + private int height; + + public IsoscelesTrapezoid(Color color, int topBase, int bottomBase, int height) { + super(color); + this.topBase = topBase; + this.bottomBase = bottomBase; + this.height = height; + } + + @Override + public double calculateArea() { + return ((topBase + bottomBase) * 0.5) * height; + } + + @Override + public void draw() { + System.out.println("Figure: IsoscelesTrapezoid " + + " area " + calculateArea() + " sq.units," + + " topBase " + topBase + " units," + + "bottomBase" + bottomBase + " 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..a2c6d0c728 --- /dev/null +++ b/src/main/java/core/basesyntax/MainApp.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public class MainApp { + public static final int FIGURES_AMOUNT = 6; + + public static void main(String[] args) { + FigureSupplier figureSupplier = new FigureSupplier(); + Figure[] figures = new Figure[FIGURES_AMOUNT]; + + for (int i = 0; i < figures.length; i++) { + if (i < FIGURES_AMOUNT / 2) { + figures[i] = figureSupplier.getRandomFigure(); + } else { + figures[i] = figureSupplier.getDefaultFigure(); + } + } + for (Figure figure : figures) { + figure.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..5044f544c8 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private int height; + private int width; + + public Rectangle(Color color, int height, int width) { + super(color); + this.height = height; + this.width = width; + } + + @Override + public double calculateArea() { + return height * width; + } + + @Override + public void draw() { + System.out.println("Figure: Rectangle " + + " area " + calculateArea() + " sq.units," + + " heigth " + 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..89158a0031 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private int side; + private int height; + + public RightTriangle(Color color, int side, int height) { + super(color); + this.side = side; + this.height = height; + } + + @Override + public double calculateArea() { + return (side * height) * 0.5; + } + + @Override + public void draw() { + System.out.println("Figure: RightTriangle " + + " area " + calculateArea() + " sq.units," + + " side " + side + " 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..f32f69dc06 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class Square extends Figure { + private int side; + + public Square(Color color, int side) { + super(color); + this.side = side; + } + + @Override + public double calculateArea() { + return side * side; + } + + @Override + public void draw() { + System.out.println("Figure: Square " + + " area " + calculateArea() + " sq.units," + + " side " + side + " units" + + " color " + getColor()); + } +}