diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..582a0a7f98 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,30 @@ +package core.basesyntax; + +import java.util.Random; + +public class Circle extends Figure implements FindArea { + private int radius; + + public Circle() { + super(); + this.radius = new Random().nextInt(1, 10); + } + + public Circle(int radius, String color) { + super(color); + this.radius = radius; + } + + @Override + public double getArea() { + return 2 * Math.PI * Math.pow(radius, 2); + } + + @Override + public void draw() { + System.out.println("\nCircle " + + "color: " + this.getColor() + + " area: " + this.getArea() + + " radius: " + radius); + } +} diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java new file mode 100644 index 0000000000..1afd3aaed3 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +public enum Color { + RED, + YELLOW, + BLUE, + GREEN, + BROWN, + WHITE, + BLACK +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..a27995eca2 --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,10 @@ +package core.basesyntax; + +import java.util.Random; + +public class ColorSupplier { + public String getRandomColor() { + int index = new 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..5573183186 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,32 @@ +package core.basesyntax; + +public abstract class Figure { + private String color; + private int area; + + public Figure() { + color = new ColorSupplier().getRandomColor(); + } + + public Figure(String color) { + this.color = color; + } + + public double getArea() { + return area; + } + + public void setArea(int area) { + this.area = area; + } + + public abstract void draw(); + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } +} diff --git a/src/main/java/core/basesyntax/FigureSuplier.java b/src/main/java/core/basesyntax/FigureSuplier.java new file mode 100644 index 0000000000..c4246907c6 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSuplier.java @@ -0,0 +1,22 @@ +package core.basesyntax; + +import java.lang.invoke.SwitchPoint; +import java.util.Random; + +public class FigureSuplier { + public Figure getRandomFigure() { + int index = new Random().nextInt(5); + return switch (index) { + case 1 -> new Circle(); + case 2 -> new IsoscelesTrapezoid(); + case 3 -> new Rectangle(); + case 4 -> new RightTriangle(); + case 5 -> new Square(); + default -> null; + }; + } + + public Figure getDeafoltFigure() { + return new Circle(10, "WHITE"); + } +} diff --git a/src/main/java/core/basesyntax/FindArea.java b/src/main/java/core/basesyntax/FindArea.java new file mode 100644 index 0000000000..ee36293ffb --- /dev/null +++ b/src/main/java/core/basesyntax/FindArea.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface FindArea { + double getArea(); +} 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..074d58d329 --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,50 @@ +package core.basesyntax; + +import java.util.Random; + +public class IsoscelesTrapezoid extends Figure implements FindArea { + private int biggerBaseSide; + private int smallerBaseSide; + private double side; + private int height; + + public IsoscelesTrapezoid() { + super(); + int firstRandomInt = new Random().nextInt(1, 10); + int secondRandomInt = new Random().nextInt(1, 10); + if (firstRandomInt > secondRandomInt) { + biggerBaseSide = firstRandomInt; + smallerBaseSide = secondRandomInt; + } else { + smallerBaseSide = firstRandomInt; + biggerBaseSide = secondRandomInt; + } + this.height = 10; + this.side = getSide(); + } + + @Override + public double getArea() { + return (double) (biggerBaseSide + smallerBaseSide) * height / 2; + } + + @Override + public void draw() { + System.out.println("\nIsosceles trapezoid " + + "color: " + this.getColor() + + " area: " + this.getArea() + + " bigger base: " + biggerBaseSide + + " smaller base: " + smallerBaseSide + + " side: " + side + + " height: " + height); + } + + private double getSide() { + if (biggerBaseSide == smallerBaseSide) { + return height; + } + + return Math.sqrt(Math.pow(((double) (biggerBaseSide - smallerBaseSide) / 2), 2) + + Math.pow(height, 2)); + } +} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..78df7fd52a --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,19 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] arg) { + Figure[] figures = new Figure[6]; + + for (int i = 0; i < figures.length/2; i++) { + figures[i] = new FigureSuplier().getRandomFigure(); + } + + for (int i = 3; i < figures.length; i++) { + figures[i] = new FigureSuplier().getDeafoltFigure(); + } + + 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..8d3de16100 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,28 @@ +package core.basesyntax; + +import java.util.Random; + +public class Rectangle extends Figure implements FindArea { + private int firstSide; + private int secondSide; + + public Rectangle() { + super(); + this.firstSide = new Random().nextInt(1, 10); + this.secondSide = new Random().nextInt(1, 10); + } + + @Override + public double getArea() { + return firstSide * secondSide; + } + + @Override + public void draw() { + System.out.println("\nRectangle " + + "color: " + this.getColor() + + " area: " + this.getArea() + + " first side: " + firstSide + + " first side: " + secondSide); + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..ebb57c86b9 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,31 @@ +package core.basesyntax; + +import java.util.Random; + +public class RightTriangle extends Figure implements FindArea { + private int firsLeg; + private int secondLeg; + private double thirdLeg; + + public RightTriangle() { + super(); + this.firsLeg = new Random().nextInt(1, 10); + this.secondLeg = new Random().nextInt(1, 10); + this.thirdLeg = Math.sqrt(Math.pow(firsLeg, 2) + Math.pow(secondLeg, 2)); + } + + @Override + public double getArea() { + return (double) firsLeg * secondLeg / 2; + } + + @Override + public void draw() { + System.out.println("\nRight triangle " + + "color: " + this.getColor() + + " area: " + this.getArea() + + " first leg: " + firsLeg + + " second leg: " + secondLeg + + " third leg: " + thirdLeg); + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..a179b32131 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,25 @@ +package core.basesyntax; + +import java.util.Random; + +public class Square extends Figure implements FindArea { + private int side; + + public Square() { + super(); + this.side = new Random().nextInt(1, 10); + } + + @Override + public double getArea() { + return Math.pow(side, 2); + } + + @Override + public void draw() { + System.out.println("\nSquere " + + "color: " + this.getColor() + + " area: " + this.getArea() + + " side: " + side); + } +}