diff --git a/src/main/java/core/basesyntax/AreaCalculator.java b/src/main/java/core/basesyntax/AreaCalculator.java new file mode 100644 index 0000000000..c0fe3457a8 --- /dev/null +++ b/src/main/java/core/basesyntax/AreaCalculator.java @@ -0,0 +1,7 @@ +package core.basesyntax; + +public interface AreaCalculator { + + int 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..45967892c5 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,32 @@ +package core.basesyntax; + +public class Circle extends Figure { + private int radius; + + public Circle(String color, String name, int radius) { + super(color, name); + this.radius = radius; + } + + @Override + public int calculateArea() { + return (int) (radius * radius * Math.PI); + + } + + @Override + public String toString() { + return "Figure:" + + "name: " + + super.getName() + + ", " + + "area: " + + calculateArea() + + " sq. units, " + + "radius: " + + radius + + ", " + + "color: " + + super.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..3a48422a35 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +public enum Color { + YELLOW, + BLUE, + BLACK, + WHITE, + GREEN, + 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..3a2201c09a --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + + public String getRandomColor() { + Color[] values = Color.values(); + int randomIndex = new Random().nextInt(values.length); + return values[randomIndex].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..75b13b68df --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,51 @@ +package core.basesyntax; + +public class Figure implements AreaCalculator { + private String color; + private String name; + + public Figure(String color, String name) { + this.color = color; + this.name = name; + } + + public Figure() { + } + + public void draw() { + System.out.println(this); + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Figure{" + + "color='" + + color + + ", " + + "name='" + + name + + '\'' + + '}'; + } + + @Override + public int calculateArea() { + return 0; + } +} diff --git a/src/main/java/core/basesyntax/FigureName.java b/src/main/java/core/basesyntax/FigureName.java new file mode 100644 index 0000000000..fea248abcf --- /dev/null +++ b/src/main/java/core/basesyntax/FigureName.java @@ -0,0 +1,10 @@ +package core.basesyntax; + +public enum FigureName { + CIRCLE, + ISOSCELES_TRAPEZOID, + RECTANGLE, + SQUARE, + RIGHT_TRIANGLE + +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..e2680c9c61 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,58 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + + public Figure getRandomFigure() { + Figure randomFigure; + int randomIndex = new Random().nextInt(5); + switch (randomIndex) { + case 0 -> randomFigure = getRandomCircle(); + case 1 -> randomFigure = getIsoscelesTrapezoid(); + case 2 -> randomFigure = getRandomRectangle(); + case 3 -> randomFigure = getRightTriangle(); + case 4 -> randomFigure = getRandomSquare(); + default -> randomFigure = new Figure(); + } + return randomFigure; + } + + public Figure getDefaultFigure() { + return new Circle(Color.WHITE.name(), FigureName.CIRCLE.name(), 10); + } + + private Circle getRandomCircle() { + return new Circle(new ColorSupplier().getRandomColor(), + FigureName.CIRCLE.name(), + new Random().nextInt(101)); + } + + private IsoscelesTrapezoid getIsoscelesTrapezoid() { + return new IsoscelesTrapezoid(new ColorSupplier().getRandomColor(), + FigureName.ISOSCELES_TRAPEZOID.name(), + new Random().nextInt(101), + new Random().nextInt(101), + new Random().nextInt(101)); + } + + private Rectangle getRandomRectangle() { + return new Rectangle(new ColorSupplier().getRandomColor(), + FigureName.RECTANGLE.name(), + new Random().nextInt(101), + new Random().nextInt(101)); + } + + private RightTriangle getRightTriangle() { + return new RightTriangle(new ColorSupplier().getRandomColor(), + FigureName.RIGHT_TRIANGLE.name(), + new Random().nextInt(101)); + } + + private Square getRandomSquare() { + return new Square(new ColorSupplier().getRandomColor(), + FigureName.SQUARE.name(), + new Random().nextInt(101)); + } + +} 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..fc44711535 --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,47 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private int bottomBase; + private int topBase; + private int side; + + public IsoscelesTrapezoid(String color, String name, int bottomBase, int topBase, int side) { + super(color, name); + this.bottomBase = bottomBase; + this.topBase = topBase; + this.side = side; + } + + @Override + public int calculateArea() { + return (int) (((bottomBase + topBase) / 2) + * (Math.sqrt((Math.pow(side, 2)) + - Math.pow((((Math.pow((bottomBase - topBase), 2)) + + (Math.pow(side, 2)) - (Math.pow(side, 2))) + / (2 * (bottomBase - topBase))), 2)))); + } + + @Override + public String toString() { + return "Figure:" + + "name: " + + super.getName() + + ", " + + "bottomBase: " + + bottomBase + + ", " + + "topBase: " + + topBase + + ", " + + "side: " + + side + + ", " + + "area: " + + calculateArea() + + " sq. units, " + + ", " + + "color: " + + super.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..2ed69ea69f --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,17 @@ +package core.basesyntax; + +public class Main { + + public static void main(String[] args) { + Figure[] figures = new Figure[6]; + for (int i = 0; i < 3; i++) { + figures[i] = new FigureSupplier().getRandomFigure(); + } + for (int i = 3; i < 6; i++) { + figures[i] = new 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..42d597fa6d --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,37 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private int firstLag; + private int secondLag; + + public Rectangle(String color, String name, int firstLag, int secondLag) { + super(color, name); + this.firstLag = firstLag; + this.secondLag = secondLag; + } + + @Override + public int calculateArea() { + return firstLag * secondLag; + } + + @Override + public String toString() { + return "Figure:" + + "name: " + + super.getName() + + ", " + + "area: " + + calculateArea() + + " sq. units, " + + "firstLag: " + + firstLag + + ", " + + "secondLag: " + + secondLag + + ", " + + "color: " + + super.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..c60cad9d91 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,32 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private int firstLag; + + public RightTriangle(String color, String name, int firstLag) { + super(color, name); + this.firstLag = firstLag; + } + + @Override + public int calculateArea() { + return (int) (Math.pow(firstLag, 2) * (Math.sqrt(3)) / 4); + } + + @Override + public String toString() { + return "Figure:" + + "name: " + + super.getName() + + ", " + + "area: " + + calculateArea() + + " sq. units, " + + "firstLag: " + + firstLag + + ", " + + "color: " + + super.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..7a31ded42c --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,32 @@ +package core.basesyntax; + +public class Square extends Figure { + private int firstLag; + + public Square(String color, String name, int firstLag) { + super(color, name); + this.firstLag = firstLag; + } + + @Override + public int calculateArea() { + return firstLag * firstLag; + } + + @Override + public String toString() { + return "Figure:" + + "name: " + + super.getName() + + ", " + + "area: " + + calculateArea() + + " sq. units, " + + "firstLag: " + + firstLag + + ", " + + "color: " + + super.getColor(); + } + +}