From 3e14e28f1875954d398c061233c576b89de18a0f Mon Sep 17 00:00:00 2001 From: Andrii Rastorhuiev <114357806+arastorhuiev@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:01:14 +0200 Subject: [PATCH 1/7] add base classes, add base loop --- src/main/java/core/basesyntax/App.java | 18 ++++++ src/main/java/core/basesyntax/Circle.java | 39 ++++++++++++ src/main/java/core/basesyntax/Color.java | 5 ++ .../java/core/basesyntax/ColorSupplier.java | 14 +++++ src/main/java/core/basesyntax/Figure.java | 28 +++++++++ .../java/core/basesyntax/FigureSupplier.java | 32 ++++++++++ src/main/java/core/basesyntax/FigureType.java | 9 +++ src/main/java/core/basesyntax/HelloWorld.java | 8 --- .../core/basesyntax/IsoscelesTrapezoid.java | 60 +++++++++++++++++++ src/main/java/core/basesyntax/Rectangle.java | 54 +++++++++++++++++ .../java/core/basesyntax/RightTriangle.java | 54 +++++++++++++++++ src/main/java/core/basesyntax/Square.java | 43 +++++++++++++ 12 files changed, 356 insertions(+), 8 deletions(-) create mode 100644 src/main/java/core/basesyntax/App.java create mode 100644 src/main/java/core/basesyntax/Circle.java create mode 100644 src/main/java/core/basesyntax/Color.java create mode 100644 src/main/java/core/basesyntax/ColorSupplier.java create mode 100644 src/main/java/core/basesyntax/Figure.java create mode 100644 src/main/java/core/basesyntax/FigureSupplier.java create mode 100644 src/main/java/core/basesyntax/FigureType.java delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/main/java/core/basesyntax/IsoscelesTrapezoid.java create mode 100644 src/main/java/core/basesyntax/Rectangle.java create mode 100644 src/main/java/core/basesyntax/RightTriangle.java create mode 100644 src/main/java/core/basesyntax/Square.java diff --git a/src/main/java/core/basesyntax/App.java b/src/main/java/core/basesyntax/App.java new file mode 100644 index 0000000000..183d7bb7a1 --- /dev/null +++ b/src/main/java/core/basesyntax/App.java @@ -0,0 +1,18 @@ +package core.basesyntax; + +/** + * Feel free to remove this class and create your own. + */ +public class App { + final static int FIGURES_LENGTH = 6; + + public static void main(String[] args) { + Figure[] figures = new Figure[FIGURES_LENGTH]; + FigureSupplier figureSupplier = new FigureSupplier(); + + for (int i = 0; i < figures.length; i++) { + figures[i] = figureSupplier.getRandomFigure(i <= 2); + figures[i].drawDetails(); + } + } +} diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..de3e2c4eb4 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,39 @@ +package core.basesyntax; + +public class Circle extends Figure { + private int radius; + + public Circle(int radius, Color color) { + setFigure(FigureType.CIRCLE); + setRadius(radius); + setColor(color); + double area = Math.round(getCalculatedArea()); + setArea(area); + } + + public Circle() { + setFigure(FigureType.CIRCLE); + setRadius(2); + setColor(Color.BLUE); + double area = Math.round(getCalculatedArea()); + setArea(area); + } + + @Override + public double getCalculatedArea() { + return Math.PI * (radius * 2); + } + + @Override + public void drawDetails() { + System.out.println(String.format("Figure: %s, area: %s sq.units, radius: %s units, color: %s", getFigure().toLowerCase(), getArea(), getRadius(), getColor().toString().toLowerCase())); + } + + public void setRadius(int radius) { + this.radius = radius; + } + + public int getRadius() { + return 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..29e562c872 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public enum Color { + RED, BLUE, WHITE, GREEN, PURPLE, BLACK, YELLOW, ORANGE, OCEAN, PINK, ZINC +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..756af54450 --- /dev/null +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -0,0 +1,14 @@ +package core.basesyntax; +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +public class ColorSupplier { + private final List availableColors = Arrays.asList(Color.values()); + private final Random random = new Random(); + + public Color getRandomColor() { + int randomIndex = random.nextInt(availableColors.size() - 1); + return availableColors.get(randomIndex); + } +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..eeee461835 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,28 @@ +package core.basesyntax; + +public abstract class Figure { + private FigureType figure; + private Color color; + private double area; + + public void setFigure(FigureType figure) { + this.figure = figure; + } + public void setColor(Color color) { + this.color = color; + } + public void setArea(double area) { this.area = area; } + + public String getFigure() { + return figure.name().toLowerCase(); + } + public Color getColor() { + return color; + } + public double getArea() { + return area; + } + + public abstract double getCalculatedArea(); + public abstract void drawDetails(); +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..fda9aa6e19 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,32 @@ +package core.basesyntax; + +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +public class FigureSupplier { + private final List availableFigures = Arrays.asList(FigureType.values()); + private final Random random = new Random(); + private final ColorSupplier color = new ColorSupplier(); + + public Figure getDefaultFigure() { + return new Circle(10, Color.WHITE); + } + + public Figure getRandomFigure(boolean randomParameters) { + int figuresSize = availableFigures.size(); + int randomIndex = random.nextInt(figuresSize - 1); + + FigureType randomFigure = availableFigures.get(randomIndex); + Color randomColor = color.getRandomColor(); + + switch(randomFigure) { + case CIRCLE: return new Circle(random.nextInt(10), randomColor); + case SQUARE: return new Square(random.nextInt(10), randomColor); + case RECTANGLE: return new Rectangle(random.nextInt(10), random.nextInt(10), randomColor); + case RIGHT_RECTANGLE: return new RightTriangle(random.nextInt(10), random.nextInt(10), randomColor); + case ISOSCELES_TRAPEZOID: return new IsoscelesTrapezoid(random.nextInt(10), random.nextInt(10), random.nextInt(10), randomColor); + default: return getDefaultFigure(); + } + } +} diff --git a/src/main/java/core/basesyntax/FigureType.java b/src/main/java/core/basesyntax/FigureType.java new file mode 100644 index 0000000000..56369eb165 --- /dev/null +++ b/src/main/java/core/basesyntax/FigureType.java @@ -0,0 +1,9 @@ +package core.basesyntax; + +public enum FigureType { + SQUARE, + RECTANGLE, + RIGHT_RECTANGLE, + CIRCLE, + ISOSCELES_TRAPEZOID +} 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..d21ab9bbb1 --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,60 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private int topLength; + private int bottomLength; + private int height; + + public IsoscelesTrapezoid(int topLength, int bottomLength, int height, Color color) { + setFigure(FigureType.ISOSCELES_TRAPEZOID); + setTopLength(topLength); + setBottomLength(bottomLength); + setHeight(height); + setColor(color); + } + + public IsoscelesTrapezoid() { + setFigure(FigureType.ISOSCELES_TRAPEZOID); + setTopLength(2); + setBottomLength(2); + setHeight(2); + setColor(Color.BLUE); + } + + @Override + public double getCalculatedArea() { + return ((bottomLength * topLength) * height) / 0.5; + } + @Override + public void drawDetails() { + System.out.println( + String.format("Figure: %s, area: %s sq.units, top: %s units, bottom: %s units, height: %s units, color: %s", + getFigure().toLowerCase(), + getArea(), + getTopLength(), + getBottomLength(), + getHeight(), + getColor().toString().toLowerCase()) + ); + } + + public void setBottomLength(int bottomLength) { + this.bottomLength = bottomLength; + } + public void setTopLength(int topLength) { + this.topLength = topLength; + } + public void setHeight(int height) { + this.height = height; + } + + public int getBottomLength() { + return bottomLength; + } + public int getTopLength() { + return topLength; + } + public int getHeight() { + return height; + } +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..f680c96c23 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,54 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private int width; + private int length; + + public Rectangle(int width, int length, Color color) { + setFigure(FigureType.RECTANGLE); + setWidth(width); + setLength(length); + double area = getCalculatedArea(); + setArea(area); + setColor(color); + } + + public Rectangle() { + setFigure(FigureType.RECTANGLE); + setWidth(2); + setLength(2); + double area = getCalculatedArea(); + setArea(area); + setColor(Color.BLUE); + } + + @Override + public double getCalculatedArea() { + return width * length; + } + @Override + public void drawDetails() { + System.out.println( + String.format("Figure: %s, area: %s sq.units, width: %s units, length: %s units, color: %s", + getFigure().toLowerCase(), + getArea(), + getWidth(), + getLength(), + getColor().toString().toLowerCase()) + ); + } + + public void setWidth(int width) { + this.width = width; + } + public void setLength(int length) { + this.length = length; + } + + public int getWidth() { + return width; + } + public int getLength() { + return length; + } +} diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java new file mode 100644 index 0000000000..082c72f9af --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,54 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private int firstLeg; + private int secondLeg; + + public RightTriangle(int firstLeg, int secondLeg, Color color) { + setFigure(FigureType.RIGHT_RECTANGLE); + setFirstLeg(firstLeg); + setSecondLeg(secondLeg); + setColor(color); + double area = getCalculatedArea(); + setArea(area); + } + + public RightTriangle() { + setFigure(FigureType.RIGHT_RECTANGLE); + setFirstLeg(2); + setSecondLeg(2); + setColor(Color.BLUE); + double area = getCalculatedArea(); + setArea(area); + } + + @Override + public double getCalculatedArea() { + return firstLeg * secondLeg / 2.0; + } + @Override + public void drawDetails() { + System.out.println( + String.format("Figure: %s, area: %s sq.units, firstLeg: %s units, secondLeg: %s units, color: %s", + getFigure().toLowerCase(), + getArea(), + getFirstLeg(), + getSecondLeg(), + getColor().toString().toLowerCase()) + ); + } + + public void setFirstLeg(int firstLeg) { + this.firstLeg = firstLeg; + } + public void setSecondLeg(int secondLeg) { + this.secondLeg = secondLeg; + } + + public int getFirstLeg() { + return firstLeg; + } + public int getSecondLeg() { + return 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..2c68d32e78 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,43 @@ +package core.basesyntax; + +public class Square extends Figure { + private int side; + + public Square(int side, Color color) { + setFigure(FigureType.SQUARE); + setSide(side); + setColor(color); + double area = getCalculatedArea(); + setArea(area); + } + + public Square() { + setFigure(FigureType.SQUARE); + setSide(2); + setColor(Color.BLUE); + double area = getCalculatedArea(); + setArea(area); + } + + @Override + public double getCalculatedArea() { + return side * side; + } + @Override + public void drawDetails() { + System.out.println( + String.format("Figure: %s, area: %s sq.units, side: %s units, color: %s", + getFigure().toLowerCase(), + getArea(), + getSide(), + getColor().toString().toLowerCase()) + ); + } + + public void setSide(int side) { + this.side = side; + } + public int getSide() { + return side; + } +} From 294ce093b7c6fde4947a71e7126679e2c95d6a32 Mon Sep 17 00:00:00 2001 From: Andrii Rastorhuiev <114357806+arastorhuiev@users.noreply.github.com> Date: Sun, 10 Nov 2024 12:52:38 +0200 Subject: [PATCH 2/7] Fixed getCalculatedArea into Circle, Trapezoid, rename RIGHT_RECTANGLE into RIGHT_TRIANGLE, fixed max index, in random functions --- src/main/java/core/basesyntax/App.java | 9 +++++---- src/main/java/core/basesyntax/Circle.java | 2 +- src/main/java/core/basesyntax/ColorSupplier.java | 3 ++- src/main/java/core/basesyntax/Figure.java | 4 +++- src/main/java/core/basesyntax/FigureSupplier.java | 9 +++++---- src/main/java/core/basesyntax/FigureType.java | 2 +- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 2 +- src/main/java/core/basesyntax/RightTriangle.java | 10 ++++++---- 8 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/main/java/core/basesyntax/App.java b/src/main/java/core/basesyntax/App.java index 183d7bb7a1..aef1a2bc50 100644 --- a/src/main/java/core/basesyntax/App.java +++ b/src/main/java/core/basesyntax/App.java @@ -4,15 +4,16 @@ * Feel free to remove this class and create your own. */ public class App { - final static int FIGURES_LENGTH = 6; + public static final int FIGURES_LENGTH = 6; public static void main(String[] args) { Figure[] figures = new Figure[FIGURES_LENGTH]; FigureSupplier figureSupplier = new FigureSupplier(); - for (int i = 0; i < figures.length; i++) { - figures[i] = figureSupplier.getRandomFigure(i <= 2); - figures[i].drawDetails(); + boolean isFirstPart = i < (FIGURES_LENGTH / 2); + + figures[i] = figureSupplier.getRandomFigure(isFirstPart); + figures[i].drawDetails(); } } } diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index de3e2c4eb4..4fd28f1703 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -21,7 +21,7 @@ public Circle() { @Override public double getCalculatedArea() { - return Math.PI * (radius * 2); + return Math.PI * radius * 2; } @Override diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 756af54450..fdd9bd6778 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -8,7 +8,8 @@ public class ColorSupplier { private final Random random = new Random(); public Color getRandomColor() { - int randomIndex = random.nextInt(availableColors.size() - 1); + int colorsMaxIndex = availableColors.size(); + int randomIndex = random.nextInt(colorsMaxIndex); return availableColors.get(randomIndex); } } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index eeee461835..0d466272f1 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -11,7 +11,9 @@ public void setFigure(FigureType figure) { public void setColor(Color color) { this.color = color; } - public void setArea(double area) { this.area = area; } + public void setArea(double area) { + this.area = area; + } public String getFigure() { return figure.name().toLowerCase(); diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index fda9aa6e19..d84445a38c 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -8,14 +8,15 @@ public class FigureSupplier { private final List availableFigures = Arrays.asList(FigureType.values()); private final Random random = new Random(); private final ColorSupplier color = new ColorSupplier(); + public static final int DEFAULT_CIRCLE_RADIUS = 10; public Figure getDefaultFigure() { - return new Circle(10, Color.WHITE); + return new Circle(DEFAULT_CIRCLE_RADIUS, Color.WHITE); } public Figure getRandomFigure(boolean randomParameters) { - int figuresSize = availableFigures.size(); - int randomIndex = random.nextInt(figuresSize - 1); + int figuresMaxIndex = availableFigures.size(); + int randomIndex = random.nextInt(figuresMaxIndex); FigureType randomFigure = availableFigures.get(randomIndex); Color randomColor = color.getRandomColor(); @@ -24,7 +25,7 @@ public Figure getRandomFigure(boolean randomParameters) { case CIRCLE: return new Circle(random.nextInt(10), randomColor); case SQUARE: return new Square(random.nextInt(10), randomColor); case RECTANGLE: return new Rectangle(random.nextInt(10), random.nextInt(10), randomColor); - case RIGHT_RECTANGLE: return new RightTriangle(random.nextInt(10), random.nextInt(10), randomColor); + case RIGHT_TRIANGLE: return new RightTriangle(random.nextInt(10), random.nextInt(10), randomColor); case ISOSCELES_TRAPEZOID: return new IsoscelesTrapezoid(random.nextInt(10), random.nextInt(10), random.nextInt(10), randomColor); default: return getDefaultFigure(); } diff --git a/src/main/java/core/basesyntax/FigureType.java b/src/main/java/core/basesyntax/FigureType.java index 56369eb165..aad4f5b509 100644 --- a/src/main/java/core/basesyntax/FigureType.java +++ b/src/main/java/core/basesyntax/FigureType.java @@ -3,7 +3,7 @@ public enum FigureType { SQUARE, RECTANGLE, - RIGHT_RECTANGLE, + RIGHT_TRIANGLE, CIRCLE, ISOSCELES_TRAPEZOID } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index d21ab9bbb1..0c5b8c38da 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -23,7 +23,7 @@ public IsoscelesTrapezoid() { @Override public double getCalculatedArea() { - return ((bottomLength * topLength) * height) / 0.5; + return ((bottomLength * topLength) * height) / 2.0; } @Override public void drawDetails() { diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 082c72f9af..afc81b22ee 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -4,8 +4,10 @@ public class RightTriangle extends Figure { private int firstLeg; private int secondLeg; + private static final int DEFAULT_LEG = 2; + public RightTriangle(int firstLeg, int secondLeg, Color color) { - setFigure(FigureType.RIGHT_RECTANGLE); + setFigure(FigureType.RIGHT_TRIANGLE); setFirstLeg(firstLeg); setSecondLeg(secondLeg); setColor(color); @@ -14,9 +16,9 @@ public RightTriangle(int firstLeg, int secondLeg, Color color) { } public RightTriangle() { - setFigure(FigureType.RIGHT_RECTANGLE); - setFirstLeg(2); - setSecondLeg(2); + setFigure(FigureType.RIGHT_TRIANGLE); + setFirstLeg(DEFAULT_LEG); + setSecondLeg(DEFAULT_LEG); setColor(Color.BLUE); double area = getCalculatedArea(); setArea(area); From da3d91cfefd1c1393e035f9358c11be634453999 Mon Sep 17 00:00:00 2001 From: Andrii Rastorhuiev <114357806+arastorhuiev@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:06:27 +0200 Subject: [PATCH 3/7] fix syntax rules --- src/main/java/core/basesyntax/Circle.java | 8 ++++- .../java/core/basesyntax/ColorSupplier.java | 1 + src/main/java/core/basesyntax/Figure.java | 5 +++ .../java/core/basesyntax/FigureSupplier.java | 33 +++++++++++++++---- .../core/basesyntax/IsoscelesTrapezoid.java | 8 ++++- src/main/java/core/basesyntax/Rectangle.java | 6 +++- .../java/core/basesyntax/RightTriangle.java | 10 ++++-- src/main/java/core/basesyntax/Square.java | 2 ++ 8 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 4fd28f1703..1fc35016e5 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -26,7 +26,13 @@ public double getCalculatedArea() { @Override public void drawDetails() { - System.out.println(String.format("Figure: %s, area: %s sq.units, radius: %s units, color: %s", getFigure().toLowerCase(), getArea(), getRadius(), getColor().toString().toLowerCase())); + System.out.println( + String.format("Figure: %s, area: %s sq.units, radius: %s units, color: %s", + getFigure().toLowerCase(), + getArea(), + getRadius(), + getColor().toString().toLowerCase()) + ); } public void setRadius(int radius) { diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index fdd9bd6778..47e8b1bf4a 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -1,4 +1,5 @@ package core.basesyntax; + import java.util.Arrays; import java.util.List; import java.util.Random; diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index 0d466272f1..d30b866c37 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -8,9 +8,11 @@ public abstract class Figure { public void setFigure(FigureType figure) { this.figure = figure; } + public void setColor(Color color) { this.color = color; } + public void setArea(double area) { this.area = area; } @@ -18,13 +20,16 @@ public void setArea(double area) { public String getFigure() { return figure.name().toLowerCase(); } + public Color getColor() { return color; } + public double getArea() { return area; } public abstract double getCalculatedArea(); + public abstract void drawDetails(); } diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index d84445a38c..4a390ac639 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -5,10 +5,11 @@ import java.util.Random; public class FigureSupplier { + private static final int DEFAULT_CIRCLE_RADIUS = 10; + private static final int MAX_RANDOM_INTEGER = 10; private final List availableFigures = Arrays.asList(FigureType.values()); private final Random random = new Random(); private final ColorSupplier color = new ColorSupplier(); - public static final int DEFAULT_CIRCLE_RADIUS = 10; public Figure getDefaultFigure() { return new Circle(DEFAULT_CIRCLE_RADIUS, Color.WHITE); @@ -22,12 +23,32 @@ public Figure getRandomFigure(boolean randomParameters) { Color randomColor = color.getRandomColor(); switch(randomFigure) { - case CIRCLE: return new Circle(random.nextInt(10), randomColor); - case SQUARE: return new Square(random.nextInt(10), randomColor); - case RECTANGLE: return new Rectangle(random.nextInt(10), random.nextInt(10), randomColor); - case RIGHT_TRIANGLE: return new RightTriangle(random.nextInt(10), random.nextInt(10), randomColor); - case ISOSCELES_TRAPEZOID: return new IsoscelesTrapezoid(random.nextInt(10), random.nextInt(10), random.nextInt(10), randomColor); + case CIRCLE: return new Circle( + random.nextInt(MAX_RANDOM_INTEGER), + randomColor + ); + case SQUARE: return new Square( + random.nextInt(MAX_RANDOM_INTEGER), + randomColor + ); + case RECTANGLE: return new Rectangle( + random.nextInt(MAX_RANDOM_INTEGER), + random.nextInt(MAX_RANDOM_INTEGER), + randomColor + ); + case RIGHT_TRIANGLE: return new RightTriangle( + random.nextInt(MAX_RANDOM_INTEGER), + random.nextInt(MAX_RANDOM_INTEGER), + randomColor + ); + case ISOSCELES_TRAPEZOID: return new IsoscelesTrapezoid( + random.nextInt(MAX_RANDOM_INTEGER), + random.nextInt(MAX_RANDOM_INTEGER), + random.nextInt(MAX_RANDOM_INTEGER), + randomColor + ); default: return getDefaultFigure(); } + } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 0c5b8c38da..a67154a156 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -25,10 +25,12 @@ public IsoscelesTrapezoid() { public double getCalculatedArea() { return ((bottomLength * topLength) * height) / 2.0; } + @Override public void drawDetails() { System.out.println( - String.format("Figure: %s, area: %s sq.units, top: %s units, bottom: %s units, height: %s units, color: %s", + String.format( + "Figure: %s, area: %s sq.units, top: %s units, bottom: %s units, height: %s units, color: %s", getFigure().toLowerCase(), getArea(), getTopLength(), @@ -41,9 +43,11 @@ public void drawDetails() { public void setBottomLength(int bottomLength) { this.bottomLength = bottomLength; } + public void setTopLength(int topLength) { this.topLength = topLength; } + public void setHeight(int height) { this.height = height; } @@ -51,9 +55,11 @@ public void setHeight(int height) { public int getBottomLength() { return bottomLength; } + public int getTopLength() { return topLength; } + public int getHeight() { return height; } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index f680c96c23..1e59c61e3e 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -26,10 +26,12 @@ public Rectangle() { public double getCalculatedArea() { return width * length; } + @Override public void drawDetails() { System.out.println( - String.format("Figure: %s, area: %s sq.units, width: %s units, length: %s units, color: %s", + String.format( + "Figure: %s, area: %s sq.units, width: %s units, length: %s units, color: %s", getFigure().toLowerCase(), getArea(), getWidth(), @@ -41,6 +43,7 @@ public void drawDetails() { public void setWidth(int width) { this.width = width; } + public void setLength(int length) { this.length = length; } @@ -48,6 +51,7 @@ public void setLength(int length) { public int getWidth() { return width; } + public int getLength() { return length; } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index afc81b22ee..872bc7822a 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -1,11 +1,11 @@ package core.basesyntax; public class RightTriangle extends Figure { + private static final int DEFAULT_LEG = 2; + private int firstLeg; private int secondLeg; - private static final int DEFAULT_LEG = 2; - public RightTriangle(int firstLeg, int secondLeg, Color color) { setFigure(FigureType.RIGHT_TRIANGLE); setFirstLeg(firstLeg); @@ -28,10 +28,12 @@ public RightTriangle() { public double getCalculatedArea() { return firstLeg * secondLeg / 2.0; } + @Override public void drawDetails() { System.out.println( - String.format("Figure: %s, area: %s sq.units, firstLeg: %s units, secondLeg: %s units, color: %s", + String.format( + "Figure: %s, area: %s sq.units, firstLeg: %s units, secondLeg: %s units, color: %s", getFigure().toLowerCase(), getArea(), getFirstLeg(), @@ -43,6 +45,7 @@ public void drawDetails() { public void setFirstLeg(int firstLeg) { this.firstLeg = firstLeg; } + public void setSecondLeg(int secondLeg) { this.secondLeg = secondLeg; } @@ -50,6 +53,7 @@ public void setSecondLeg(int secondLeg) { public int getFirstLeg() { return firstLeg; } + public int getSecondLeg() { return secondLeg; } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 2c68d32e78..a47bf0ab1c 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -23,6 +23,7 @@ public Square() { public double getCalculatedArea() { return side * side; } + @Override public void drawDetails() { System.out.println( @@ -37,6 +38,7 @@ public void drawDetails() { public void setSide(int side) { this.side = side; } + public int getSide() { return side; } From 0f3dfa8ff20b56383eb1071be4150dca8ae4b8ca Mon Sep 17 00:00:00 2001 From: Andrii Rastorhuiev <114357806+arastorhuiev@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:16:24 +0200 Subject: [PATCH 4/7] fix maven build (syntax errors) --- src/main/java/core/basesyntax/FigureSupplier.java | 2 +- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 5 ++++- src/main/java/core/basesyntax/Rectangle.java | 4 +++- src/main/java/core/basesyntax/RightTriangle.java | 5 ++++- src/main/java/core/basesyntax/Square.java | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 4a390ac639..82df16bc5e 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -22,7 +22,7 @@ public Figure getRandomFigure(boolean randomParameters) { FigureType randomFigure = availableFigures.get(randomIndex); Color randomColor = color.getRandomColor(); - switch(randomFigure) { + switch (randomFigure) { case CIRCLE: return new Circle( random.nextInt(MAX_RANDOM_INTEGER), randomColor diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index a67154a156..ddc63884ec 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -1,6 +1,9 @@ package core.basesyntax; public class IsoscelesTrapezoid extends Figure { + private static final String DEFAULT_DRAW_MESSAGE = + "Figure: %s, area: %s sq.units, top: %s units, " + + "bottom: %s units, height: %s units, color: %s"; private int topLength; private int bottomLength; private int height; @@ -30,7 +33,7 @@ public double getCalculatedArea() { public void drawDetails() { System.out.println( String.format( - "Figure: %s, area: %s sq.units, top: %s units, bottom: %s units, height: %s units, color: %s", + DEFAULT_DRAW_MESSAGE, getFigure().toLowerCase(), getArea(), getTopLength(), diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 1e59c61e3e..fcf6c2aff2 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -1,6 +1,8 @@ package core.basesyntax; public class Rectangle extends Figure { + private static final String DEFAULT_DRAW_MESSAGE = + "Figure: %s, area: %s sq.units, width: %s units, length: %s units, color: %s"; private int width; private int length; @@ -31,7 +33,7 @@ public double getCalculatedArea() { public void drawDetails() { System.out.println( String.format( - "Figure: %s, area: %s sq.units, width: %s units, length: %s units, color: %s", + DEFAULT_DRAW_MESSAGE, getFigure().toLowerCase(), getArea(), getWidth(), diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 872bc7822a..fa581456d1 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -2,6 +2,8 @@ public class RightTriangle extends Figure { private static final int DEFAULT_LEG = 2; + private static final String DEFAULT_DRAW_MESSAGE = + "Figure: %s, area: %s sq.units, firstLeg: %s units, secondLeg: %s units, color: %s"; private int firstLeg; private int secondLeg; @@ -31,9 +33,10 @@ public double getCalculatedArea() { @Override public void drawDetails() { + System.out.println( String.format( - "Figure: %s, area: %s sq.units, firstLeg: %s units, secondLeg: %s units, color: %s", + DEFAULT_DRAW_MESSAGE, getFigure().toLowerCase(), getArea(), getFirstLeg(), diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index a47bf0ab1c..716f838f37 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -38,7 +38,7 @@ public void drawDetails() { public void setSide(int side) { this.side = side; } - + public int getSide() { return side; } From 584271888bee190d590ed567f6bd398986d64023 Mon Sep 17 00:00:00 2001 From: Andrii Rastorhuiev <114357806+arastorhuiev@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:23:53 +0200 Subject: [PATCH 5/7] separate draw & figure, --- src/main/java/core/basesyntax/Draw.java | 5 +++++ src/main/java/core/basesyntax/Figure.java | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 src/main/java/core/basesyntax/Draw.java diff --git a/src/main/java/core/basesyntax/Draw.java b/src/main/java/core/basesyntax/Draw.java new file mode 100644 index 0000000000..6cd4ddf38b --- /dev/null +++ b/src/main/java/core/basesyntax/Draw.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Draw { + public void drawDetails(); +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index d30b866c37..9b0e134147 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,6 +1,6 @@ package core.basesyntax; -public abstract class Figure { +public abstract class Figure implements Draw { private FigureType figure; private Color color; private double area; @@ -30,6 +30,4 @@ public double getArea() { } public abstract double getCalculatedArea(); - - public abstract void drawDetails(); } From 2809d8e92bf3a56d403a39a36c2dc3f58d66652c Mon Sep 17 00:00:00 2001 From: Andrii Rastorhuiev <114357806+arastorhuiev@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:42:20 +0200 Subject: [PATCH 6/7] fix loo, remove default parameter --- src/main/java/core/basesyntax/App.java | 6 +++++- src/main/java/core/basesyntax/FigureSupplier.java | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/App.java b/src/main/java/core/basesyntax/App.java index aef1a2bc50..88edaf3ee5 100644 --- a/src/main/java/core/basesyntax/App.java +++ b/src/main/java/core/basesyntax/App.java @@ -11,8 +11,12 @@ public static void main(String[] args) { FigureSupplier figureSupplier = new FigureSupplier(); for (int i = 0; i < figures.length; i++) { boolean isFirstPart = i < (FIGURES_LENGTH / 2); + if (isFirstPart) { + figures[i] = figureSupplier.getRandomFigure(); + } else { + figures[i] = figureSupplier.getDefaultFigure(); + } - figures[i] = figureSupplier.getRandomFigure(isFirstPart); figures[i].drawDetails(); } } diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 82df16bc5e..9b5615d373 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -15,7 +15,7 @@ public Figure getDefaultFigure() { return new Circle(DEFAULT_CIRCLE_RADIUS, Color.WHITE); } - public Figure getRandomFigure(boolean randomParameters) { + public Figure getRandomFigure() { int figuresMaxIndex = availableFigures.size(); int randomIndex = random.nextInt(figuresMaxIndex); From 47051ff481a38147ea8921133a19ab3dd94f3353 Mon Sep 17 00:00:00 2001 From: Andrii Rastorhuiev <114357806+arastorhuiev@users.noreply.github.com> Date: Sun, 10 Nov 2024 13:53:18 +0200 Subject: [PATCH 7/7] Fix circle, trapezoid formula. Remove default parameters in Figures --- src/main/java/core/basesyntax/Circle.java | 10 +--------- src/main/java/core/basesyntax/IsoscelesTrapezoid.java | 10 +--------- src/main/java/core/basesyntax/Rectangle.java | 9 --------- src/main/java/core/basesyntax/RightTriangle.java | 9 --------- src/main/java/core/basesyntax/Square.java | 8 -------- 5 files changed, 2 insertions(+), 44 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 1fc35016e5..3578bc32f3 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -11,17 +11,9 @@ public Circle(int radius, Color color) { setArea(area); } - public Circle() { - setFigure(FigureType.CIRCLE); - setRadius(2); - setColor(Color.BLUE); - double area = Math.round(getCalculatedArea()); - setArea(area); - } - @Override public double getCalculatedArea() { - return Math.PI * radius * 2; + return Math.PI * radius * radius; } @Override diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index ddc63884ec..60f242e0dc 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -16,17 +16,9 @@ public IsoscelesTrapezoid(int topLength, int bottomLength, int height, Color col setColor(color); } - public IsoscelesTrapezoid() { - setFigure(FigureType.ISOSCELES_TRAPEZOID); - setTopLength(2); - setBottomLength(2); - setHeight(2); - setColor(Color.BLUE); - } - @Override public double getCalculatedArea() { - return ((bottomLength * topLength) * height) / 2.0; + return ((topLength + bottomLength) * height) / 2.0; } @Override diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index fcf6c2aff2..bdfe7592ba 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -15,15 +15,6 @@ public Rectangle(int width, int length, Color color) { setColor(color); } - public Rectangle() { - setFigure(FigureType.RECTANGLE); - setWidth(2); - setLength(2); - double area = getCalculatedArea(); - setArea(area); - setColor(Color.BLUE); - } - @Override public double getCalculatedArea() { return width * length; diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index fa581456d1..bf083ec6ab 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -17,15 +17,6 @@ public RightTriangle(int firstLeg, int secondLeg, Color color) { setArea(area); } - public RightTriangle() { - setFigure(FigureType.RIGHT_TRIANGLE); - setFirstLeg(DEFAULT_LEG); - setSecondLeg(DEFAULT_LEG); - setColor(Color.BLUE); - double area = getCalculatedArea(); - setArea(area); - } - @Override public double getCalculatedArea() { return firstLeg * secondLeg / 2.0; diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 716f838f37..32025a8211 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -11,14 +11,6 @@ public Square(int side, Color color) { setArea(area); } - public Square() { - setFigure(FigureType.SQUARE); - setSide(2); - setColor(Color.BLUE); - double area = getCalculatedArea(); - setArea(area); - } - @Override public double getCalculatedArea() { return side * side;