From 08cc25ae2228b1d25f0d3e7fb1d95d9b0f9b1af6 Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Thu, 28 Sep 2023 20:43:42 +0300 Subject: [PATCH 01/10] implemented all the methods from the task --- .../java/core/basesyntax/AreaCalculator.java | 5 +++ src/main/java/core/basesyntax/Circle.java | 28 ++++++++++++++ src/main/java/core/basesyntax/Color.java | 12 ++++++ .../java/core/basesyntax/ColorSupplier.java | 12 ++++++ src/main/java/core/basesyntax/Drawer.java | 5 +++ src/main/java/core/basesyntax/Figure.java | 15 ++++++++ .../java/core/basesyntax/FigureSupplier.java | 38 +++++++++++++++++++ src/main/java/core/basesyntax/HelloWorld.java | 8 ---- .../core/basesyntax/IsoscelesTrapezoid.java | 29 ++++++++++++++ src/main/java/core/basesyntax/Main.java | 20 ++++++++++ src/main/java/core/basesyntax/Rectangle.java | 27 +++++++++++++ .../java/core/basesyntax/RightTriangle.java | 28 ++++++++++++++ src/main/java/core/basesyntax/Square.java | 24 ++++++++++++ 13 files changed, 243 insertions(+), 8 deletions(-) create mode 100644 src/main/java/core/basesyntax/AreaCalculator.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/Drawer.java create mode 100644 src/main/java/core/basesyntax/Figure.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/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 new file mode 100644 index 0000000000..a83c4b400d --- /dev/null +++ b/src/main/java/core/basesyntax/Circle.java @@ -0,0 +1,28 @@ +package core.basesyntax; + +public class Circle extends Figure implements AreaCalculator, Drawer{ + private int radius; + + public Circle() { + + } + + public Circle(int radius, Color color) { + this.radius = radius; + this.color = color; + } + + @Override + public double getArea() { + return Math.PI * radius * radius; + } + + @Override + public void draw() { + System.out.println("Figure: circle, " + + "area: " + getArea() + " sq. units, " + + "radius: " + radius + " units, " + + "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..af2ee7affb --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,12 @@ +package core.basesyntax; + +public enum Color { + RED, + ORANGE, + YELLOW, + GREEN, + BLUE, + INDIGO, + VIOLET, + WHITE, +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java new file mode 100644 index 0000000000..459d42b706 --- /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 Random random = new Random(); + private Color[] colors = Color.values(); + + public Color getRandomColor() { + return colors[random.nextInt(colors.length)]; + } +} diff --git a/src/main/java/core/basesyntax/Drawer.java b/src/main/java/core/basesyntax/Drawer.java new file mode 100644 index 0000000000..a9d764f713 --- /dev/null +++ b/src/main/java/core/basesyntax/Drawer.java @@ -0,0 +1,5 @@ +package core.basesyntax; + +public interface Drawer { + 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..5eddedc169 --- /dev/null +++ b/src/main/java/core/basesyntax/Figure.java @@ -0,0 +1,15 @@ +package core.basesyntax; + +public class Figure implements AreaCalculator, Drawer { + Color color; + + @Override + public double getArea() { + return 0; + } + + @Override + public void draw() { + + } +} diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java new file mode 100644 index 0000000000..b722dc056b --- /dev/null +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -0,0 +1,38 @@ +package core.basesyntax; + +import java.util.Random; + +public class FigureSupplier { + private static final int MAX_FIGURES_TYPES = 5; + private static final int SIZE_LIMIT = 5; //додано виключно щоб не генерувалися великі int + private ColorSupplier colorSupplier = new ColorSupplier(); + private Random random = new Random(); //Об"єкт рандому + + public Figure getRandomFigure() { + Color randomColor = colorSupplier.getRandomColor(); + int figureIndex = random.nextInt(MAX_FIGURES_TYPES) + 1; + int sideA = random.nextInt(SIZE_LIMIT) + 1; + switch (figureIndex) { + case 1: + return new Circle(sideA, randomColor); + case 2: + return new Square(sideA, randomColor); + case 3: + int rectangleSideB = random.nextInt(SIZE_LIMIT) + 1; + return new Rectangle(sideA, rectangleSideB, randomColor); + case 4: + int triangleSideB = random.nextInt(SIZE_LIMIT) + 1; + return new RightTriangle(sideA, triangleSideB, randomColor); + case 5: + int trapezoidSideB = random.nextInt(SIZE_LIMIT) + 1; + int trapezoidHeight = random.nextInt(SIZE_LIMIT) + 1; + return new IsoscelesTrapezoid(sideA, trapezoidSideB, trapezoidHeight, + randomColor); + } + return null; + } + + public Figure getDefaultFigure() { + return new Circle(10, Color.WHITE); + } +} 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..db9171c299 --- /dev/null +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -0,0 +1,29 @@ +package core.basesyntax; + +public class IsoscelesTrapezoid extends Figure implements AreaCalculator, Drawer{ + private int sideA; + private int sideB; + private int height; + + public IsoscelesTrapezoid(int sideA, int sideB, int height, Color color) { + this.sideA = sideA; + this.sideB = sideB; + this.height = height; + this.color = color; + } + + @Override + public double getArea() { + return (double) ((sideA + sideB) * height) / 2; + } + + @Override + public void draw() { + System.out.println("Figure: isosceles trapezoid, " + + "area: " + getArea() + " sq. units, " + + "first side: " + sideA + " units, " + + "second side: " + sideB + " units, " + + "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..8064a51e9e --- /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) { + Figure[] figures = new Figure[10]; + FigureSupplier figureSupplier = new FigureSupplier(); + + for (int i = 0; i < figures.length; i++) { + if (i < figures.length / 2) { + figures[i] = figureSupplier.getDefaultFigure(); + } else { + figures[i] = figureSupplier.getRandomFigure(); + } + } + + 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..70d0837616 --- /dev/null +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -0,0 +1,27 @@ +package core.basesyntax; + +public class Rectangle extends Figure implements AreaCalculator, Drawer{ + private int sideA; + private int sideB; + + public Rectangle(int sideA, int sideB, Color color) { + this.sideA = sideA; + this.sideB = sideB; + this.color = color; + } + + @Override + public double getArea() { + return sideA * sideB; + } + + @Override + public void draw() { + System.out.println("Figure: rectangle, " + + "area: " + getArea() + " sq. units, " + + "first side: " + sideA + " units, " + + "second side: " + sideB + " units, " + + "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..1966452316 --- /dev/null +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -0,0 +1,28 @@ +package core.basesyntax; + +public class RightTriangle extends Figure implements AreaCalculator, Drawer{ + private int firstLeg; + private int secondLeg; + + public RightTriangle(int firstLeg, int secondLeg, Color color) { + this.firstLeg = firstLeg; + this.secondLeg = secondLeg; + this.color = color; + } + + + @Override + public double getArea() { + return (double) (firstLeg * secondLeg / 2); + } + + @Override + public void draw() { + System.out.println("Figure: right triangle, " + + "area: " + getArea() + " sq. units, " + + "first leg: " + firstLeg + " units, " + + "second leg: " + secondLeg + " units, " + + "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..92d5522c46 --- /dev/null +++ b/src/main/java/core/basesyntax/Square.java @@ -0,0 +1,24 @@ +package core.basesyntax; + +public class Square extends Figure implements AreaCalculator, Drawer { + private int sideA; + + public Square(int sideA, Color color) { + this.color = color; + this.sideA = sideA; + } + + @Override + public double getArea() { + return sideA * sideA; + } + + @Override + public void draw() { + System.out.println("Figure: square, " + + "area: " + getArea() + " sq. units, " + + "side: " + sideA + " units, " + + "color: " + color + ); + } +} From d42c7bfcbc18233f9624493723a4230428efa3ea Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Thu, 28 Sep 2023 20:52:36 +0300 Subject: [PATCH 02/10] delete the AreaCalculator implementation from Figure class --- src/main/java/core/basesyntax/Circle.java | 3 ++- src/main/java/core/basesyntax/Figure.java | 7 +------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index a83c4b400d..4a9da8ed09 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -1,6 +1,7 @@ package core.basesyntax; -public class Circle extends Figure implements AreaCalculator, Drawer{ +public class Circle extends Figure implements AreaCalculator, Drawer { + private int radius; public Circle() { diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index 5eddedc169..a109f88c0b 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,13 +1,8 @@ package core.basesyntax; -public class Figure implements AreaCalculator, Drawer { +public class Figure implements Drawer { Color color; - @Override - public double getArea() { - return 0; - } - @Override public void draw() { From fc6bec67dc59a2a4512e3b53312d5dcd6a3025ad Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Thu, 28 Sep 2023 21:08:39 +0300 Subject: [PATCH 03/10] deleted interface implementation from all classes (ex. Figure), fixed codestyle --- src/main/java/core/basesyntax/Circle.java | 10 +++++----- src/main/java/core/basesyntax/Figure.java | 17 +++++++++++++++-- .../java/core/basesyntax/FigureSupplier.java | 5 ++++- .../core/basesyntax/IsoscelesTrapezoid.java | 12 ++++++------ src/main/java/core/basesyntax/Rectangle.java | 12 ++++++------ .../java/core/basesyntax/RightTriangle.java | 13 ++++++------- src/main/java/core/basesyntax/Square.java | 10 +++++----- 7 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index 4a9da8ed09..a2831f1aee 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -1,6 +1,6 @@ package core.basesyntax; -public class Circle extends Figure implements AreaCalculator, Drawer { +public class Circle extends Figure { private int radius; @@ -10,7 +10,7 @@ public Circle() { public Circle(int radius, Color color) { this.radius = radius; - this.color = color; + this.setColor(color); } @Override @@ -21,9 +21,9 @@ public double getArea() { @Override public void draw() { System.out.println("Figure: circle, " - + "area: " + getArea() + " sq. units, " - + "radius: " + radius + " units, " - + "color: " + color + + "area: " + getArea() + " sq. units, " + + "radius: " + radius + " units, " + + "color: " + this.getColor() ); } } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index a109f88c0b..6efbd655d4 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,10 +1,23 @@ package core.basesyntax; -public class Figure implements Drawer { - Color color; +public class Figure implements Drawer, AreaCalculator { + private Color color; + + public Color getColor() { + return color; + } + + public void setColor(Color 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 index b722dc056b..aad8c596bd 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,6 +3,7 @@ import java.util.Random; public class FigureSupplier { + private static final int MAX_FIGURES_TYPES = 5; private static final int SIZE_LIMIT = 5; //додано виключно щоб не генерувалися великі int private ColorSupplier colorSupplier = new ColorSupplier(); @@ -28,8 +29,10 @@ public Figure getRandomFigure() { int trapezoidHeight = random.nextInt(SIZE_LIMIT) + 1; return new IsoscelesTrapezoid(sideA, trapezoidSideB, trapezoidHeight, randomColor); + default: + return null; } - return null; + } public Figure getDefaultFigure() { diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index db9171c299..b4e49c66be 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -1,6 +1,6 @@ package core.basesyntax; -public class IsoscelesTrapezoid extends Figure implements AreaCalculator, Drawer{ +public class IsoscelesTrapezoid extends Figure { private int sideA; private int sideB; private int height; @@ -9,7 +9,7 @@ public IsoscelesTrapezoid(int sideA, int sideB, int height, Color color) { this.sideA = sideA; this.sideB = sideB; this.height = height; - this.color = color; + this.setColor(color); } @Override @@ -20,10 +20,10 @@ public double getArea() { @Override public void draw() { System.out.println("Figure: isosceles trapezoid, " - + "area: " + getArea() + " sq. units, " - + "first side: " + sideA + " units, " - + "second side: " + sideB + " units, " - + "color: " + color + + "area: " + getArea() + " sq. units, " + + "first side: " + sideA + " units, " + + "second side: " + sideB + " units, " + + "color: " + this.getColor() ); } } diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 70d0837616..410f038c84 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -1,13 +1,13 @@ package core.basesyntax; -public class Rectangle extends Figure implements AreaCalculator, Drawer{ +public class Rectangle extends Figure { private int sideA; private int sideB; public Rectangle(int sideA, int sideB, Color color) { this.sideA = sideA; this.sideB = sideB; - this.color = color; + this.setColor(color); } @Override @@ -18,10 +18,10 @@ public double getArea() { @Override public void draw() { System.out.println("Figure: rectangle, " - + "area: " + getArea() + " sq. units, " - + "first side: " + sideA + " units, " - + "second side: " + sideB + " units, " - + "color: " + color + + "area: " + getArea() + " sq. units, " + + "first side: " + sideA + " units, " + + "second side: " + sideB + " units, " + + "color: " + this.getColor() ); } } diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 1966452316..80955f926c 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -1,16 +1,15 @@ package core.basesyntax; -public class RightTriangle extends Figure implements AreaCalculator, Drawer{ +public class RightTriangle extends Figure { private int firstLeg; private int secondLeg; public RightTriangle(int firstLeg, int secondLeg, Color color) { this.firstLeg = firstLeg; this.secondLeg = secondLeg; - this.color = color; + this.setColor(color); } - @Override public double getArea() { return (double) (firstLeg * secondLeg / 2); @@ -19,10 +18,10 @@ public double getArea() { @Override public void draw() { System.out.println("Figure: right triangle, " - + "area: " + getArea() + " sq. units, " - + "first leg: " + firstLeg + " units, " - + "second leg: " + secondLeg + " units, " - + "color: " + color + + "area: " + getArea() + " sq. units, " + + "first leg: " + firstLeg + " units, " + + "second leg: " + secondLeg + " units, " + + "color: " + this.getColor() ); } } diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index 92d5522c46..a6c4355afd 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -1,10 +1,10 @@ package core.basesyntax; -public class Square extends Figure implements AreaCalculator, Drawer { +public class Square extends Figure { private int sideA; public Square(int sideA, Color color) { - this.color = color; + this.setColor(color); this.sideA = sideA; } @@ -16,9 +16,9 @@ public double getArea() { @Override public void draw() { System.out.println("Figure: square, " - + "area: " + getArea() + " sq. units, " - + "side: " + sideA + " units, " - + "color: " + color + + "area: " + getArea() + " sq. units, " + + "side: " + sideA + " units, " + + "color: " + this.getColor() ); } } From e2033b7f21bbcac81c4b506685964cb16922e55a Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Thu, 28 Sep 2023 21:17:55 +0300 Subject: [PATCH 04/10] changed the getRandomFigure to return Figure object and not null --- src/main/java/core/basesyntax/FigureSupplier.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index aad8c596bd..268728ebd4 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -30,7 +30,7 @@ public Figure getRandomFigure() { return new IsoscelesTrapezoid(sideA, trapezoidSideB, trapezoidHeight, randomColor); default: - return null; + return new Figure(); } } From 8e326371353f36ff9c286c28dc7e3aa2cb497e82 Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Thu, 28 Sep 2023 21:22:14 +0300 Subject: [PATCH 05/10] deleted comments --- src/main/java/core/basesyntax/FigureSupplier.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 268728ebd4..ee5f5aa216 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -5,9 +5,9 @@ public class FigureSupplier { private static final int MAX_FIGURES_TYPES = 5; - private static final int SIZE_LIMIT = 5; //додано виключно щоб не генерувалися великі int + private static final int SIZE_LIMIT = 5; private ColorSupplier colorSupplier = new ColorSupplier(); - private Random random = new Random(); //Об"єкт рандому + private Random random = new Random(); public Figure getRandomFigure() { Color randomColor = colorSupplier.getRandomColor(); From 33ff967db83f9ea72f04cf9a4e02661bedb01580 Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Fri, 29 Sep 2023 16:35:24 +0300 Subject: [PATCH 06/10] deleted some empty lines --- src/main/java/core/basesyntax/Circle.java | 1 - src/main/java/core/basesyntax/FigureSupplier.java | 1 - src/main/java/core/basesyntax/Main.java | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index a2831f1aee..c7fd85e417 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -1,7 +1,6 @@ package core.basesyntax; public class Circle extends Figure { - private int radius; public Circle() { diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index ee5f5aa216..7856802f31 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,7 +3,6 @@ import java.util.Random; public class FigureSupplier { - private static final int MAX_FIGURES_TYPES = 5; private static final int SIZE_LIMIT = 5; private ColorSupplier colorSupplier = new ColorSupplier(); diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index 8064a51e9e..4c5f585c85 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -13,7 +13,7 @@ public static void main(String[] args) { } } - for (Figure figure : figures) { + for (Drawer figure : figures) { figure.draw(); } } From 578c13ca912660b8170532feec8cb869a4578413 Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Fri, 29 Sep 2023 16:39:11 +0300 Subject: [PATCH 07/10] changed the Drawer interface name according to the naming convention --- src/main/java/core/basesyntax/{Drawer.java => Drawable.java} | 2 +- src/main/java/core/basesyntax/Figure.java | 2 +- src/main/java/core/basesyntax/Main.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/main/java/core/basesyntax/{Drawer.java => Drawable.java} (57%) diff --git a/src/main/java/core/basesyntax/Drawer.java b/src/main/java/core/basesyntax/Drawable.java similarity index 57% rename from src/main/java/core/basesyntax/Drawer.java rename to src/main/java/core/basesyntax/Drawable.java index a9d764f713..13fe470ec9 100644 --- a/src/main/java/core/basesyntax/Drawer.java +++ b/src/main/java/core/basesyntax/Drawable.java @@ -1,5 +1,5 @@ package core.basesyntax; -public interface Drawer { +public interface Drawable { void draw(); } diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index 6efbd655d4..a7979db148 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 class Figure implements Drawer, AreaCalculator { +public class Figure implements Drawable, AreaCalculator { private Color color; public Color getColor() { diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index 4c5f585c85..a6c6e58ab4 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -13,7 +13,7 @@ public static void main(String[] args) { } } - for (Drawer figure : figures) { + for (Drawable figure : figures) { figure.draw(); } } From 4b2dcf1267677c186640b3e86570dcec4caa458a Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Sat, 30 Sep 2023 13:24:52 +0300 Subject: [PATCH 08/10] changed array type form Figure[] to Drawable[] in psvm --- src/main/java/core/basesyntax/FigureSupplier.java | 1 + src/main/java/core/basesyntax/Main.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index 7856802f31..ee5f5aa216 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,6 +3,7 @@ import java.util.Random; public class FigureSupplier { + private static final int MAX_FIGURES_TYPES = 5; private static final int SIZE_LIMIT = 5; private ColorSupplier colorSupplier = new ColorSupplier(); diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index a6c6e58ab4..3d119fad80 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -2,7 +2,7 @@ public class Main { public static void main(String[] args) { - Figure[] figures = new Figure[10]; + Drawable[] figures = new Drawable[10]; FigureSupplier figureSupplier = new FigureSupplier(); for (int i = 0; i < figures.length; i++) { From 498a4aa0483c263911ab1406f0f382cb265d8b46 Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Sat, 30 Sep 2023 13:29:42 +0300 Subject: [PATCH 09/10] fixed code style in the FigureSupplier class --- src/main/java/core/basesyntax/FigureSupplier.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/FigureSupplier.java b/src/main/java/core/basesyntax/FigureSupplier.java index ee5f5aa216..0b1e4eefac 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -3,7 +3,6 @@ import java.util.Random; public class FigureSupplier { - private static final int MAX_FIGURES_TYPES = 5; private static final int SIZE_LIMIT = 5; private ColorSupplier colorSupplier = new ColorSupplier(); @@ -27,8 +26,7 @@ public Figure getRandomFigure() { case 5: int trapezoidSideB = random.nextInt(SIZE_LIMIT) + 1; int trapezoidHeight = random.nextInt(SIZE_LIMIT) + 1; - return new IsoscelesTrapezoid(sideA, trapezoidSideB, trapezoidHeight, - randomColor); + return new IsoscelesTrapezoid(sideA, trapezoidSideB, trapezoidHeight, randomColor); default: return new Figure(); } From d28763d14aaf2e26efb534c8fb2dcdd00b1c5047 Mon Sep 17 00:00:00 2001 From: Vlasenko Yevhenii Date: Tue, 3 Oct 2023 13:04:57 +0300 Subject: [PATCH 10/10] fixed according to comments --- src/main/java/core/basesyntax/Circle.java | 6 +---- src/main/java/core/basesyntax/Figure.java | 16 ++++--------- .../java/core/basesyntax/FigureSupplier.java | 24 +++++++++++-------- .../core/basesyntax/IsoscelesTrapezoid.java | 2 +- src/main/java/core/basesyntax/Main.java | 4 +++- src/main/java/core/basesyntax/Rectangle.java | 2 +- .../java/core/basesyntax/RightTriangle.java | 2 +- src/main/java/core/basesyntax/Square.java | 2 +- 8 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/main/java/core/basesyntax/Circle.java b/src/main/java/core/basesyntax/Circle.java index c7fd85e417..d671426d73 100644 --- a/src/main/java/core/basesyntax/Circle.java +++ b/src/main/java/core/basesyntax/Circle.java @@ -3,13 +3,9 @@ public class Circle extends Figure { private int radius; - public Circle() { - - } - public Circle(int radius, Color color) { + super(color); this.radius = radius; - this.setColor(color); } @Override diff --git a/src/main/java/core/basesyntax/Figure.java b/src/main/java/core/basesyntax/Figure.java index a7979db148..27ebf5e324 100644 --- a/src/main/java/core/basesyntax/Figure.java +++ b/src/main/java/core/basesyntax/Figure.java @@ -1,8 +1,12 @@ package core.basesyntax; -public class Figure implements Drawable, AreaCalculator { +public abstract class Figure implements Drawable, AreaCalculator { private Color color; + public Figure(Color color) { + this.color = color; + } + public Color getColor() { return color; } @@ -10,14 +14,4 @@ public Color getColor() { public void setColor(Color 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 index 0b1e4eefac..6226b162f8 100644 --- a/src/main/java/core/basesyntax/FigureSupplier.java +++ b/src/main/java/core/basesyntax/FigureSupplier.java @@ -5,35 +5,39 @@ public class FigureSupplier { private static final int MAX_FIGURES_TYPES = 5; private static final int SIZE_LIMIT = 5; + private static final int DEFAULT_CIRCLE_RADIUS = 10; + private ColorSupplier colorSupplier = new ColorSupplier(); private Random random = new Random(); + private int getRandomSize() { + return random.nextInt(SIZE_LIMIT) + 1; + } + public Figure getRandomFigure() { Color randomColor = colorSupplier.getRandomColor(); - int figureIndex = random.nextInt(MAX_FIGURES_TYPES) + 1; - int sideA = random.nextInt(SIZE_LIMIT) + 1; - switch (figureIndex) { + int sideA = getRandomSize(); + switch (random.nextInt(MAX_FIGURES_TYPES) + 1) { case 1: return new Circle(sideA, randomColor); case 2: return new Square(sideA, randomColor); case 3: - int rectangleSideB = random.nextInt(SIZE_LIMIT) + 1; + int rectangleSideB = getRandomSize(); return new Rectangle(sideA, rectangleSideB, randomColor); case 4: - int triangleSideB = random.nextInt(SIZE_LIMIT) + 1; + int triangleSideB = getRandomSize(); return new RightTriangle(sideA, triangleSideB, randomColor); case 5: - int trapezoidSideB = random.nextInt(SIZE_LIMIT) + 1; - int trapezoidHeight = random.nextInt(SIZE_LIMIT) + 1; + int trapezoidSideB = getRandomSize(); + int trapezoidHeight = getRandomSize(); return new IsoscelesTrapezoid(sideA, trapezoidSideB, trapezoidHeight, randomColor); default: - return new Figure(); + return getDefaultFigure(); } - } public Figure getDefaultFigure() { - return new Circle(10, Color.WHITE); + return new Circle(DEFAULT_CIRCLE_RADIUS, Color.WHITE); } } diff --git a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java index b4e49c66be..2d346803a2 100644 --- a/src/main/java/core/basesyntax/IsoscelesTrapezoid.java +++ b/src/main/java/core/basesyntax/IsoscelesTrapezoid.java @@ -6,10 +6,10 @@ public class IsoscelesTrapezoid extends Figure { private int height; public IsoscelesTrapezoid(int sideA, int sideB, int height, Color color) { + super(color); this.sideA = sideA; this.sideB = sideB; this.height = height; - this.setColor(color); } @Override diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index 3d119fad80..08f961dc27 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -1,8 +1,10 @@ package core.basesyntax; public class Main { + private static final int MAIN_LOOP_COUNT = 10; + public static void main(String[] args) { - Drawable[] figures = new Drawable[10]; + Drawable[] figures = new Drawable[MAIN_LOOP_COUNT]; FigureSupplier figureSupplier = new FigureSupplier(); for (int i = 0; i < figures.length; i++) { diff --git a/src/main/java/core/basesyntax/Rectangle.java b/src/main/java/core/basesyntax/Rectangle.java index 410f038c84..68d742b332 100644 --- a/src/main/java/core/basesyntax/Rectangle.java +++ b/src/main/java/core/basesyntax/Rectangle.java @@ -5,9 +5,9 @@ public class Rectangle extends Figure { private int sideB; public Rectangle(int sideA, int sideB, Color color) { + super(color); this.sideA = sideA; this.sideB = sideB; - this.setColor(color); } @Override diff --git a/src/main/java/core/basesyntax/RightTriangle.java b/src/main/java/core/basesyntax/RightTriangle.java index 80955f926c..e311ec6de5 100644 --- a/src/main/java/core/basesyntax/RightTriangle.java +++ b/src/main/java/core/basesyntax/RightTriangle.java @@ -5,9 +5,9 @@ public class RightTriangle extends Figure { private int secondLeg; public RightTriangle(int firstLeg, int secondLeg, Color color) { + super(color); this.firstLeg = firstLeg; this.secondLeg = secondLeg; - this.setColor(color); } @Override diff --git a/src/main/java/core/basesyntax/Square.java b/src/main/java/core/basesyntax/Square.java index a6c4355afd..a4dcbfbbc7 100644 --- a/src/main/java/core/basesyntax/Square.java +++ b/src/main/java/core/basesyntax/Square.java @@ -4,7 +4,7 @@ public class Square extends Figure { private int sideA; public Square(int sideA, Color color) { - this.setColor(color); + super(color); this.sideA = sideA; }