From a200077bb2308e6e75c8c512e35f301ae8e24127 Mon Sep 17 00:00:00 2001 From: danX Date: Sat, 7 Oct 2023 13:55:42 +0200 Subject: [PATCH 1/3] created classes, interface and abstract class for them, class ColorSupplier with public String getRandomColor(), class FigureSupplier with public Figure getRandomFigure() and getDefaultFigure(), also created main with random and default figures --- src/main/java/core/basesyntax/Circle.java | 16 +++++++++ .../java/core/basesyntax/ColorSupplier.java | 12 +++++++ src/main/java/core/basesyntax/Figure.java | 13 +++++++ .../java/core/basesyntax/FigureBehaviour.java | 5 +++ .../java/core/basesyntax/FigureSupplier.java | 35 +++++++++++++++++++ src/main/java/core/basesyntax/HelloWorld.java | 8 ----- .../core/basesyntax/IsoscelesTrapezoid.java | 21 +++++++++++ src/main/java/core/basesyntax/Main.java | 21 +++++++++++ src/main/java/core/basesyntax/Rectangle.java | 18 ++++++++++ .../java/core/basesyntax/RightTriangle.java | 19 ++++++++++ src/main/java/core/basesyntax/Square.java | 16 +++++++++ 11 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 src/main/java/core/basesyntax/Circle.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/FigureBehaviour.java create mode 100644 src/main/java/core/basesyntax/FigureSupplier.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/Main.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/Circle.java b/src/main/java/core/basesyntax/Circle.java new file mode 100644 index 0000000000..2ddfeaedc3 --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,16 @@ +package core.basesyntax; + +public class Circle extends Figure { + private final double radius; + + public Circle(String color, double radius) { + super(color); + this.radius = radius; + } + + @Override + public void printfInfoAboutFigure() { + System.out.println("Figure: circle, area: " + Math.PI * radius * radius + + " sq.units, radius: " + radius + " units, color: " + getColor()); + } +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..25ff7e1ef2 --- /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 final Random random = new Random(); + private final String[] colors = new String[]{"Blue", "Red", "Yellow", "Green", "Black"}; + + public String getRandomColor() { + return colors[random.nextInt(colors.length)]; + } +} diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java new file mode 100644 index 0000000000..448050bf75 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +public abstract class Figure implements FigureBehaviour { + private final String color; + + public Figure(String color) { + this.color = color; + } + + public String getColor() { + return color; + } +} diff --git a/src/main/java/core/basesyntax/FigureBehaviour.java b/src/main/java/core/basesyntax/FigureBehaviour.java new file mode 100644 index 0000000000..42c3c9030a --- /dev/null +++ b/src/main/java/core/basesyntax/FigureBehaviour.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface FigureBehaviour { + void printfInfoAboutFigure(); +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..1964ab7a5f --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,35 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private final Random random = new Random(); + private final ColorSupplier colorSupplier = new ColorSupplier(); + + public Figure getRandomFigure() { + String figureColor = colorSupplier.getRandomColor(); + int randomFigure = random.nextInt(5); + + switch (randomFigure) { + case 0: + return new Circle(figureColor, random.nextDouble() * 9.0 + 1.0); + case 1: + return new IsoscelesTrapezoid(figureColor, random.nextDouble() * 9.0 + 1.0, + random.nextDouble() * 9.0 + 1.0, random.nextDouble() * 9.0 + 1.0); + case 2: + return new Rectangle(figureColor, random.nextDouble() * 9.0 + 1.0, + random.nextDouble() * 9.0 + 1.0); + case 3: + return new RightTriangle(figureColor, random.nextDouble() * 9.0 + 1.0, + random.nextDouble() * 9.0 + 1.0); + case 4: + return new Square(figureColor, random.nextDouble() * 9.0 + 1.0); + default: + return null; + } + } + + 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..524a40319a --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,21 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure { + private final double side1; + private final double side2; + private final double height; + + public IsoscelesTrapezoid(String color, double side1, double side2, double height) { + super(color); + this.side1 = side1; + this.side2 = side2; + this.height = height; + } + + @Override + public void printfInfoAboutFigure() { + System.out.println("Figure: trapezoid, area: " + 0.5 * (side1 + side2) * height + + " sq.units, side1: " + side1 + " sq.units, side2: " + side2 + + " sq.units, height: " + height + " units, color: " + 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..a32256032b --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,21 @@ +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 < 3; i++) { + figures[i] = figureSupplier.getRandomFigure(); + } + + for (int i = 3; i < 6; i++) { + figures[i] = figureSupplier.getDefaultFigure(); + } + + for (Figure figure : figures) { + figure.printfInfoAboutFigure(); + } + } +} diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java new file mode 100644 index 0000000000..cb56493ed2 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,18 @@ +package core.basesyntax; + +public class Rectangle extends Figure { + private final double length; + private final double width; + + public Rectangle(String color, double width, double length) { + super(color); + this.width = width; + this.length = length; + } + + @Override + public void printfInfoAboutFigure() { + System.out.println("Figure: rectangle, area: " + length * width + " sq.units, length: " + + length + " sq.units, width: " + width + " units, color: " + 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..3dc009c8cd --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,19 @@ +package core.basesyntax; + +public class RightTriangle extends Figure { + private final double firstLeg; + private final double secondLeg; + + public RightTriangle(String color, double firstLeg, double secondLeg) { + super(color); + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + } + + @Override + public void printfInfoAboutFigure() { + System.out.println("Figure: triangle, area: " + 0.5 * firstLeg * secondLeg + + " sq.units, firstLeg: " + firstLeg + " sq.units, secondLeg: " + secondLeg + + " units, color: " + 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..d91cce4c7e --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,16 @@ +package core.basesyntax; + +public class Square extends Figure { + private final double side; + + public Square(String color, double side) { + super(color); + this.side = side; + } + + @Override + public void printfInfoAboutFigure() { + System.out.println("Figure: square, area: " + side * side + " sq.units, side: " + + side + " units, color: " + getColor()); + } +} From eba5c7b99f582b80d4a0e09e5222b0ebb7e2a15a Mon Sep 17 00:00:00 2001 From: danX Date: Sat, 7 Oct 2023 18:04:32 +0200 Subject: [PATCH 2/3] created Enum Color, added one behavior(new interface), changed the name of 2 interfaces, used constants instead of numbers and Strings, used one loop for in Main --- .../java/core/basesyntax/AreaCalculator.java | 5 ++++ src/main/java/core/basesyntax/Circle.java | 9 +++++-- src/main/java/core/basesyntax/Color.java | 9 +++++++ .../java/core/basesyntax/ColorSupplier.java | 5 ++-- src/main/java/core/basesyntax/Drawable.java | 5 ++++ src/main/java/core/basesyntax/Figure.java | 2 +- .../java/core/basesyntax/FigureBehaviour.java | 5 ---- .../java/core/basesyntax/FigureSupplier.java | 25 +++++++++++-------- .../core/basesyntax/IsoscelesTrapezoid.java | 9 +++++-- src/main/java/core/basesyntax/Main.java | 11 +++----- src/main/java/core/basesyntax/Rectangle.java | 9 +++++-- .../java/core/basesyntax/RightTriangle.java | 9 +++++-- src/main/java/core/basesyntax/Square.java | 9 +++++-- 13 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 src/main/java/core/basesyntax/AreaCalculator.java create mode 100644 src/main/java/core/basesyntax/Color.java create mode 100644 src/main/java/core/basesyntax/Drawable.java delete mode 100644 src/main/java/core/basesyntax/FigureBehaviour.java diff --git a/src/main/java/core/basesyntax/AreaCalculator.java b/src/main/java/core/basesyntax/AreaCalculator.java new file mode 100644 index 0000000000..61ea1d760d --- /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 index 2ddfeaedc3..a418041f9c 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -9,8 +9,13 @@ public Circle(String color, double radius) { } @Override - public void printfInfoAboutFigure() { - System.out.println("Figure: circle, area: " + Math.PI * radius * radius + public void draw() { + System.out.println("Figure: circle, area: " + getArea() + " sq.units, radius: " + radius + " units, color: " + getColor()); } + + @Override + public double getArea() { + return Math.PI * 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..4c8dc6b3c4 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,9 @@ +package core.basesyntax; + +public enum Color { + Blue, + Red, + Yellow, + Green, + Black +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 25ff7e1ef2..13c30708dd 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -4,9 +4,10 @@ public class ColorSupplier { private final Random random = new Random(); - private final String[] colors = new String[]{"Blue", "Red", "Yellow", "Green", "Black"}; + private final Color[] colors = new Color[]{Color.Blue, Color.Red, + Color.Yellow, Color.Green, Color.Black}; public String getRandomColor() { - return colors[random.nextInt(colors.length)]; + return colors[random.nextInt(colors.length)].toString(); } } diff --git a/src/main/java/core/basesyntax/Drawable.java b/src/main/java/core/basesyntax/Drawable.java new file mode 100644 index 0000000000..13fe470ec9 --- /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 index 448050bf75..486696152d 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 implements FigureBehaviour { +public abstract class Figure implements Drawable, AreaCalculator { private final String color; public Figure(String color) { diff --git a/src/main/java/core/basesyntax/FigureBehaviour.java b/src/main/java/core/basesyntax/FigureBehaviour.java deleted file mode 100644 index 42c3c9030a..0000000000 --- a/src/main/java/core/basesyntax/FigureBehaviour.java +++ /dev/null @@ -1,5 +0,0 @@ -package core.basesyntax; - -public interface FigureBehaviour { - void printfInfoAboutFigure(); -} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 1964ab7a5f..b8ce0fe292 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,33 +3,36 @@ import java.util.Random; public class FigureSupplier { + static final int MAX_FIGURE_QUANTITY = 5; + static final double FIGURE_RADIUS = 9.0 + 1.0; + static final String DEFAULT_CIRCLE_COLOUR = "White"; private final Random random = new Random(); private final ColorSupplier colorSupplier = new ColorSupplier(); public Figure getRandomFigure() { String figureColor = colorSupplier.getRandomColor(); - int randomFigure = random.nextInt(5); + int randomFigure = random.nextInt(MAX_FIGURE_QUANTITY); switch (randomFigure) { case 0: - return new Circle(figureColor, random.nextDouble() * 9.0 + 1.0); + return new Circle(figureColor, random.nextDouble() * FIGURE_RADIUS); case 1: - return new IsoscelesTrapezoid(figureColor, random.nextDouble() * 9.0 + 1.0, - random.nextDouble() * 9.0 + 1.0, random.nextDouble() * 9.0 + 1.0); + return new IsoscelesTrapezoid(figureColor, random.nextDouble() * FIGURE_RADIUS, + random.nextDouble() * FIGURE_RADIUS, random.nextDouble() * FIGURE_RADIUS); case 2: - return new Rectangle(figureColor, random.nextDouble() * 9.0 + 1.0, - random.nextDouble() * 9.0 + 1.0); + return new Rectangle(figureColor, random.nextDouble() * FIGURE_RADIUS, + random.nextDouble() * FIGURE_RADIUS); case 3: - return new RightTriangle(figureColor, random.nextDouble() * 9.0 + 1.0, - random.nextDouble() * 9.0 + 1.0); + return new RightTriangle(figureColor, random.nextDouble() * FIGURE_RADIUS, + random.nextDouble() * FIGURE_RADIUS); case 4: - return new Square(figureColor, random.nextDouble() * 9.0 + 1.0); + return new Square(figureColor, random.nextDouble() * FIGURE_RADIUS); default: - return null; + return getDefaultFigure(); } } public Figure getDefaultFigure() { - return new Circle("White", 10); + return new Circle(DEFAULT_CIRCLE_COLOUR, FIGURE_RADIUS); } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index 524a40319a..9d8f33a8c8 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -13,9 +13,14 @@ public IsoscelesTrapezoid(String color, double side1, double side2, double heigh } @Override - public void printfInfoAboutFigure() { - System.out.println("Figure: trapezoid, area: " + 0.5 * (side1 + side2) * height + public void draw() { + System.out.println("Figure: trapezoid, area: " + getArea() + " sq.units, side1: " + side1 + " sq.units, side2: " + side2 + " sq.units, height: " + height + " units, color: " + getColor()); } + + @Override + public double getArea() { + return 0.5 * (side1 + side2) * height; + } } diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index a32256032b..6c54f04cee 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -6,16 +6,13 @@ public static void main(String[] args) { Figure[] figures = new Figure[6]; - for (int i = 0; i < 3; i++) { - figures[i] = figureSupplier.getRandomFigure(); - } - - for (int i = 3; i < 6; i++) { - figures[i] = figureSupplier.getDefaultFigure(); + for (int i = 0; i < figures.length; i++) { + figures[i] = (i < 3) ? figureSupplier.getRandomFigure() + : figureSupplier.getDefaultFigure(); } for (Figure figure : figures) { - figure.printfInfoAboutFigure(); + figure.draw(); } } } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index cb56493ed2..10c80f6836 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -11,8 +11,13 @@ public Rectangle(String color, double width, double length) { } @Override - public void printfInfoAboutFigure() { - System.out.println("Figure: rectangle, area: " + length * width + " sq.units, length: " + public void draw() { + System.out.println("Figure: rectangle, area: " + getArea() + " sq.units, length: " + length + " sq.units, width: " + width + " units, color: " + getColor()); } + + @Override + public double getArea() { + return length * width; + } } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 3dc009c8cd..8aac998a7b 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -11,9 +11,14 @@ public RightTriangle(String color, double firstLeg, double secondLeg) { } @Override - public void printfInfoAboutFigure() { - System.out.println("Figure: triangle, area: " + 0.5 * firstLeg * secondLeg + public void draw() { + System.out.println("Figure: triangle, area: " + getArea() + " sq.units, firstLeg: " + firstLeg + " sq.units, secondLeg: " + secondLeg + " units, color: " + getColor()); } + + @Override + public double getArea() { + return 0.5 * firstLeg * secondLeg; + } } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index d91cce4c7e..7e1128a1f4 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -9,8 +9,13 @@ public Square(String color, double side) { } @Override - public void printfInfoAboutFigure() { - System.out.println("Figure: square, area: " + side * side + " sq.units, side: " + public void draw() { + System.out.println("Figure: square, area: " + getArea() + " sq.units, side: " + side + " units, color: " + getColor()); } + + @Override + public double getArea() { + return side * side; + } } From b58e1de87607e502ab81c7081c2da6bc0d1082e7 Mon Sep 17 00:00:00 2001 From: danX Date: Sat, 7 Oct 2023 18:31:22 +0200 Subject: [PATCH 3/3] fixed --- src/main/java/core/basesyntax/Color.java | 10 +++++----- src/main/java/core/basesyntax/ColorSupplier.java | 3 +-- src/main/java/core/basesyntax/FigureSupplier.java | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java index 4c8dc6b3c4..81982b78a1 100644 --- a/src/main/java/core/basesyntax/Color.java +++ b/src/main/java/core/basesyntax/Color.java @@ -1,9 +1,9 @@ package core.basesyntax; public enum Color { - Blue, - Red, - Yellow, - Green, - Black + BLUE, + RED, + YELLOW, + GREEN, + BLACK } diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 13c30708dd..641a2039be 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -4,8 +4,7 @@ public class ColorSupplier { private final Random random = new Random(); - private final Color[] colors = new Color[]{Color.Blue, Color.Red, - Color.Yellow, Color.Green, Color.Black}; + private final Color[] colors = Color.values(); public String getRandomColor() { return colors[random.nextInt(colors.length)].toString(); diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index b8ce0fe292..d66d04cd68 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -5,7 +5,7 @@ public class FigureSupplier { static final int MAX_FIGURE_QUANTITY = 5; static final double FIGURE_RADIUS = 9.0 + 1.0; - static final String DEFAULT_CIRCLE_COLOUR = "White"; + static final String DEFAULT_CIRCLE_COLOUR = Color.BLUE.name(); private final Random random = new Random(); private final ColorSupplier colorSupplier = new ColorSupplier();