diff --git a/src/main/java/core/basesyntax/AreaCalcualtor.java b/src/main/java/core/basesyntax/AreaCalcualtor.java new file mode 100644 index 0000000000..5a7872c17b --- /dev/null +++ b/src/main/java/core/basesyntax/AreaCalcualtor.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface AreaCalcualtor { + 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..8f7dc1bd88 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,20 @@ +package core.basesyntax; + +public class Circle extends Figure { + private double radius; + + public Circle(Color color, double radius) { + super(color.name()); + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } + + @Override + public void draw() { + System.out.println("radius: " + radius + " units"); + } +} diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java new file mode 100644 index 0000000000..98555e0497 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public enum Color { + RED, GREEN, BLUE, YELLOW, BLACK, WHITE; +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..46eb2c7769 --- /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 static final Random RANDOM = new Random(); + + public String getRandomColor() { + Color[] colors = Color.values(); + return colors[RANDOM.nextInt(colors.length)].toString(); + } +} 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..203fd37634 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,15 @@ +package core.basesyntax; + +public abstract class Figure implements Drawable, AreaCalcualtor { + protected String color; + + public Figure(String color) { + this.color = color; + } + + @Override + public void draw() { + System.out.println("Figure: " + this.getClass().getSimpleName() + + ", area: " + getArea() + " sq. units, color: " + 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..263c6bd8b4 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,47 @@ +package core.basesyntax; + +import java.util.Random; + +class FigureSupplier { + private static final int FIGURE_TYPES_COUNT = 5; + private static final int MAX_DIMENSION = 10; + private static final int MIN_DIMENSION = 1; + private static final int DEFAULT_RADIUS = 10; + + private Random random = new Random(); + private ColorSupplier colorSupplier = new ColorSupplier(); + + public Figure getRandomFigure() { + String color = colorSupplier.getRandomColor(); + switch (random.nextInt(FIGURE_TYPES_COUNT)) { + case 0: { + int side = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + return new Square(Color.valueOf(color), side); + } + case 1: { + int firstLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + return new Rectangle(Color.valueOf(color), firstLeg, secondLeg); + } + case 2: { + int firstLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + return new RightTriangle(Color.valueOf(color), firstLeg, secondLeg); + } + case 3: { + int radius = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + return new Circle(Color.valueOf(color), radius); + } + default: { + int topBase = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int bottomBase = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION; + return new IsoscelesTrapezoid(Color.valueOf(color), topBase, bottomBase, secondLeg); + } + } + } + + public Figure getDefaultFigure() { + return new Circle(Color.WHITE, 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..db32f87b1c --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private double topBase; + private double bottomBase; + private double height; + + public IsoscelesTrapezoid(Color color, double topBase, double bottomBase, double height) { + super(color.name()); + this.topBase = topBase; + this.bottomBase = bottomBase; + this.height = height; + } + + @Override + public void draw() { + super.draw(); + System.out.println("topBase: " + topBase + " units, bottomBase: " + + bottomBase + " units, height: " + height + " units"); + } + + @Override + public double getArea() { + return 0.5 * (topBase + bottomBase) * height; + } +} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..f49d665c00 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,22 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + FigureSupplier figureSupplier = new FigureSupplier(); + final int arraySize = (int) (Math.random() * 10) + 1; + Figure[] figures = new Figure[arraySize]; + + for (int i = 0; i < figures.length; i++) { + if (i < figures.length / 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..4c5e6eec58 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,22 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private double width; + private double height; + + public Rectangle(Color color, double width, double height) { + super(color.name()); + this.width = width; + this.height = height; + } + + @Override + public void draw() { + System.out.println("width: " + width + " units, height: " + height + " units"); + } + + @Override + public double getArea() { + return width * height; + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..951032c3ad --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,22 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private double firstLeg; + private double secondLeg; + + public RightTriangle(Color color, double firstLeg, double secondLeg) { + super(color.name()); + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + } + + @Override + public void draw() { + System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units"); + } + + @Override + public double getArea() { + return 0.5 * firstLeg * secondLeg; + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..53d981eaa7 --- /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(Color color, double side) { + super(color.name()); + this.side = side; + } + + @Override + public void draw() { + super.draw(); + System.out.println("side: " + side + " units"); + } + + @Override + public double getArea() { + return side * side; + } +}