diff --git a/src/main/java/core/basesyntax/Application.java b/src/main/java/core/basesyntax/Application.java new file mode 100644 index 0000000000..1c9957c578 --- /dev/null +++ b/src/main/java/core/basesyntax/Application.java @@ -0,0 +1,18 @@ +package core.basesyntax; + +public class Application { + private static final int FIGURE_COUNT = 6; + + public static void main(String[] args) { + FigureSupplier figureSupplier = new FigureSupplier(); + Figure[] figures = new Figure[FIGURE_COUNT]; + for (int i = 0; i < FIGURE_COUNT; i++) { + if (i < FIGURE_COUNT / 2) { + figures[i] = figureSupplier.getRandomFigure(); + } else { + figures[i] = figureSupplier.getDefaultFigure(); + } + figures[i].draw(); + } + } +} diff --git a/src/main/java/core/basesyntax/AreaCalculator.java b/src/main/java/core/basesyntax/AreaCalculator.java new file mode 100644 index 0000000000..673262c690 --- /dev/null +++ b/src/main/java/core/basesyntax/AreaCalculator.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface AreaCalculator { + 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..2818c14e8c --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,23 @@ +package core.basesyntax; + +public class Circle extends Figure { + private final int radius; + + public Circle(int radius, String color) { + super(color); + this.radius = radius; + } + + @Override + public double getArea() { + return Math.PI * Math.pow(radius, 2); + } + + @Override + public void draw() { + System.out.println("Figure: circle, area: " + + getArea() + + ", radius: " + + radius + ", color: " + color); + } +} diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java new file mode 100644 index 0000000000..a2caace53f --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,12 @@ +package core.basesyntax; + +public enum Color { + BLACK, + WHITE, + YELLOW, + GREEN, + BLUE, + ORANGE, + PURPLE, + PINK +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..cb35171adc --- /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 final Random random = new Random(); + private final Color[] colorArray = Color.values(); + + public String getRandomColor() { + int index = random.nextInt(colorArray.length); + return colorArray[index].name(); + } +} 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..28c25f9f34 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +public abstract class Figure implements Drawable, AreaCalculator { + protected String color; + + public Figure(String color) { + this.color = color; + } + + public String 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..1c3b5ed579 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,40 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private static final int MAX_LENGTH = 100; + private static final int FIGURE_COUNT = 5; + private static final int DEFAULT_RADIUS = 10; + private static final String DEFAULT_COLOR = Color.WHITE.name(); + + private final Random random = new Random(); + private final ColorSupplier supplier = new ColorSupplier(); + + public Figure getRandomFigure() { + String color = supplier.getRandomColor(); + final int index = random.nextInt(FIGURE_COUNT); + + switch (index) { + case 0: + return new Circle(getRandomNumber(), color); + case 1: + return new IsoscelesTrapezoid(getRandomNumber(), getRandomNumber(), + getRandomNumber(), color); + case 2: + return new Rectangle(getRandomNumber(), getRandomNumber(), color); + case 3: + return new RightTriangle(getRandomNumber(), getRandomNumber(), color); + default: + return new Square(getRandomNumber(), color); + } + } + + public Figure getDefaultFigure() { + return new Circle(DEFAULT_RADIUS, DEFAULT_COLOR); + } + + private int getRandomNumber() { + return random.nextInt(MAX_LENGTH - 1) + 1; + } +} 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..f035a8e85f --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,30 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private final int height; + private final int firstBase; + private final int secondBase; + + public IsoscelesTrapezoid(int height, int firstBase, int secondBase, String color) { + super(color); + this.height = height; + this.firstBase = firstBase; + this.secondBase = secondBase; + } + + @Override + public double getArea() { + return (double) ((firstBase + secondBase) * height) / 2; + } + + @Override + public void draw() { + System.out.println("Figure: isosceles trapezoid, area: " + + getArea() + + ", height: " + height + + ", first Base: " + firstBase + + ", secondBase: " + + secondBase + + ", color: " + color); + } +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..130368cc78 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,26 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private final int firstSide; + private final int secondSide; + + public Rectangle(int firstSide, int secondSide, String color) { + super(color); + this.firstSide = firstSide; + this.secondSide = secondSide; + } + + @Override + public double getArea() { + return firstSide * secondSide; + } + + @Override + public void draw() { + System.out.println("Figure: rectangle, area: " + getArea() + + ", firstSide: " + + firstSide + ", secondSide: " + + secondSide + ", 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..e6a8e69d6e --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,28 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private final int firstLeg; + private final int secondLeg; + + public RightTriangle(int firstLeg, int secondLeg, String color) { + super(color); + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + } + + @Override + public void draw() { + System.out.println("Figure: right triangle, area: " + + getArea() + + ", first leg: " + + firstLeg + ", second leg:" + + secondLeg + + ", color: " + + color); + } + + @Override + public double getArea() { + return (double) firstLeg * secondLeg / 2; + } +} diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java new file mode 100644 index 0000000000..c63fdb2273 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Square extends Figure { + private final int side; + + public Square(int side, String color) { + super(color); + this.side = side; + } + + @Override + public void draw() { + System.out.println("Figure: square, area: " + + getArea() + + ", side: " + + side + ", color: " + + color); + } + + @Override + public double getArea() { + return side * side; + } +} diff --git a/src/test/java/core/test/StructureTest.java b/src/test/java/core/test/StructureTest.java index 54e70eee5b..e6a60d1303 100644 --- a/src/test/java/core/test/StructureTest.java +++ b/src/test/java/core/test/StructureTest.java @@ -16,7 +16,7 @@ public class StructureTest { private static final List figureClassNames = List - .of("Circle", "Square", "IsoscelesTrapezoid", "Rectangle", "RightTriangle"); + .of("Circle", "Square", "IsoscelesTrapezoid", "Rectangle", "RightTriangle"); private static List allClasses = new ArrayList<>(); @BeforeClass @@ -25,8 +25,8 @@ public static void init() { allClasses = getClasses("core.basesyntax"); if (allClasses.size() == 0) { Assert.fail("You should not rename base core.basesyntax package " - + "and path to project and project name should not contain spaces " - + "or words in cyrillic"); + + "and path to project and project name should not contain spaces " + + "or words in cyrillic"); } } catch (Exception e) { throw new RuntimeException("Could not load classes ", e); @@ -152,4 +152,4 @@ private static List findClasses(File directory, String packageName) } return classes; } -} +} \ No newline at end of file