diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..5adee88c1d --- /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(String 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() + + " radius: " + radius + " color " + color); + + } +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..c60ad518d7 --- /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(); + private static final String[] Colors = {"red", "blue", "yellow", "green", "purple", "black"}; + + public String getColorSupplier() { + return Colors[random.nextInt(Colors.length)]; + } +} diff --git a/src/main/java/core/basesyntax/Drawable.java b/src/main/java/core/basesyntax/Drawable.java new file mode 100644 index 0000000000..2a41dde80c --- /dev/null +++ b/src/main/java/core/basesyntax/Drawable.java @@ -0,0 +1,6 @@ +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..e8b204acbe --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,19 @@ +package core.basesyntax; + +public abstract class Figure implements Drawable, Measurable { + protected String color; + + public Figure(String color) { + this.color = color; + } + + @Override + public void draw() { + + } + + @Override + public double getArea() { + return 0; + } +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..0620e68813 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,30 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private static final Random random2 = new Random(); + private static final ColorSupplier COLOR_SUPPLIER = new ColorSupplier(); + + public Figure getRandomFigure() { + switch (random2.nextInt(5)) { + case 0: + return new Square(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() * 10 + 1); + case 1: + return new Rectangle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() + * 10 + 1, random2.nextDouble() * 10 + 1); + case 2: + return new RightTriangle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() + * 10 + 1, random2.nextDouble() * 10 * 1); + case 3: + return new Circle(COLOR_SUPPLIER.getColorSupplier(), random2.nextDouble() * 10 + 1); + default: + return new IsoscelesTrapezoid(COLOR_SUPPLIER.getColorSupplier(),random2.nextDouble() + * 10 + 1, random2.nextDouble() * 10 + 1, random2.nextDouble() * 10 + 1); + } + } + + public Figure getDefaultFigure() { + return new Circle("white", 10); + } +} 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..1bb2de15ac --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,25 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private double base1; + private double base2; + private double height; + + public IsoscelesTrapezoid(String color, double base1, double base2, double height) { + super(color); + this.base1 = base1; + this.base2 = base2; + this.height = height; + } + + @Override + public double getArea() { + return 0.5 * (base1 + base2) * height; + } + + @Override + public void draw() { + System.out.println(" Figure: IsoscelesTrapezoid, area: " + getArea() + " base1: " + + base1 + " base2: " + base2 + " height: " + height + " color: " + color); + } +} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..d26cd04677 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,20 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + + FigureSupplier figureSupplier = new FigureSupplier(); + Figure[] figures = new Figure[6]; + + for (int i = 0; i < figures.length / 2; i++) { + figures[i] = figureSupplier.getRandomFigure(); + } + for (int i = figures.length / 2; i < figures.length; i++) { + figures[i] = figureSupplier.getDefaultFigure(); + } + for (Figure figure : figures) { + figure.draw(); + } + } +} + diff --git a/src/main/java/core/basesyntax/Measurable.java b/src/main/java/core/basesyntax/Measurable.java new file mode 100644 index 0000000000..84be1716fa --- /dev/null +++ b/src/main/java/core/basesyntax/Measurable.java @@ -0,0 +1,7 @@ +package core.basesyntax; + +public interface Measurable { + + double getArea(); + +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..bb1f9873ab --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private double width; + private double height; + + public Rectangle(String color, double width, double height) { + super(color); + this.width = width; + this.width = height; + } + + @Override + public double getArea() { + return width * height; + } + + @Override + public void draw() { + System.out.println("Figure rectangle, area: " + getArea() + " width" + width + + " ,height" + height + " color " + color); + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..3af872303b --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private double fistLeg; + private double secondLeg; + + public RightTriangle(String color, double fistLeg, double secondLeg) { + super(color); + this.fistLeg = fistLeg; + this.secondLeg = secondLeg; + } + + @Override + public double getArea() { + return 0.5 * fistLeg * secondLeg; + } + + @Override + public void draw() { + System.out.println("Figure RightTriangle, area: " + getArea() + ", firstLeg " + + fistLeg + ", secondLeg " + secondLeg + " color " + color); + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..25fb2c2c6a --- /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(String 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() + " one side: " + side + " color:" + color); + } +}