From 34f4bd313a26f3335ddbdb559ce9583121f40a36 Mon Sep 17 00:00:00 2001 From: denDy1224 Date: Fri, 10 Jan 2025 00:29:50 +0100 Subject: [PATCH 1/5] add_lottery --- .../java/core/basesyntax/Application.java | 7 +++++- .../java/core/basesyntax/ColorSupplier.java | 10 ++++++-- .../java/core/basesyntax/classes/Ball.java | 23 +++++++++++++++++++ .../java/core/basesyntax/classes/Color.java | 10 ++++++++ .../java/core/basesyntax/classes/Lottery.java | 14 +++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/main/java/core/basesyntax/classes/Ball.java create mode 100644 src/main/java/core/basesyntax/classes/Color.java create mode 100644 src/main/java/core/basesyntax/classes/Lottery.java diff --git a/src/main/java/core/basesyntax/Application.java b/src/main/java/core/basesyntax/Application.java index 42ed4ef86..fc2b08e0b 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -1,7 +1,12 @@ package core.basesyntax; +import core.basesyntax.classes.Lottery; + public class Application { public static void main(String[] args) { - // create three balls using class Lottery and print information about them in console + Lottery lottery = new Lottery(); + for (int order = 0; order < 3; order++) { + System.out.println(lottery.getRandomBall().getBallInfo()); + } } } diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 25e8b23b1..36f40fbc4 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -1,7 +1,13 @@ package core.basesyntax; +import core.basesyntax.classes.Color; + +import java.util.Random; + public class ColorSupplier { - public String getRandomColor() { - return null; + public Color getRandomColor() { + Color[] colors = Color.values(); + Random random = new Random(); + return colors[random.nextInt(colors.length)]; } } diff --git a/src/main/java/core/basesyntax/classes/Ball.java b/src/main/java/core/basesyntax/classes/Ball.java new file mode 100644 index 000000000..3b6989768 --- /dev/null +++ b/src/main/java/core/basesyntax/classes/Ball.java @@ -0,0 +1,23 @@ +package core.basesyntax.classes; + +public class Ball { + private Color color; + private int number; + + Ball(Color color, int number) { + this.color = color; + this.number = number; + } + + public Color getColor() { + return color; + } + + public int getNumber() { + return number; + } + + public String getBallInfo() { + return String.format("Ball of %s color, with number %d", this.color, this.number); + } +} diff --git a/src/main/java/core/basesyntax/classes/Color.java b/src/main/java/core/basesyntax/classes/Color.java new file mode 100644 index 000000000..133250c33 --- /dev/null +++ b/src/main/java/core/basesyntax/classes/Color.java @@ -0,0 +1,10 @@ +package core.basesyntax.classes; + +public enum Color { + RED, GREEN, BLUE, YELLOW, VIOLET, BLACK; + + @Override + public String toString() { + return name(); + } +} diff --git a/src/main/java/core/basesyntax/classes/Lottery.java b/src/main/java/core/basesyntax/classes/Lottery.java new file mode 100644 index 000000000..9e10dcab2 --- /dev/null +++ b/src/main/java/core/basesyntax/classes/Lottery.java @@ -0,0 +1,14 @@ +package core.basesyntax.classes; + +import core.basesyntax.ColorSupplier; + +import java.util.Random; + +public class Lottery { + + public Ball getRandomBall() { + Random random = new Random(); + ColorSupplier color = new ColorSupplier(); + return new Ball(color.getRandomColor(), random.nextInt(100)); + } +} From 7e715dc4be07d46a91535f601a68caa2e1827a73 Mon Sep 17 00:00:00 2001 From: denDy1224 Date: Fri, 10 Jan 2025 00:31:22 +0100 Subject: [PATCH 2/5] added homework with random bals generation --- src/main/java/core/basesyntax/ColorSupplier.java | 1 - src/main/java/core/basesyntax/classes/Lottery.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 36f40fbc4..001802066 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -1,7 +1,6 @@ package core.basesyntax; import core.basesyntax.classes.Color; - import java.util.Random; public class ColorSupplier { diff --git a/src/main/java/core/basesyntax/classes/Lottery.java b/src/main/java/core/basesyntax/classes/Lottery.java index 9e10dcab2..462cf6cd9 100644 --- a/src/main/java/core/basesyntax/classes/Lottery.java +++ b/src/main/java/core/basesyntax/classes/Lottery.java @@ -1,7 +1,6 @@ package core.basesyntax.classes; import core.basesyntax.ColorSupplier; - import java.util.Random; public class Lottery { From 5985eb6224ad0741bfc8d657936fc2daeb5b9a53 Mon Sep 17 00:00:00 2001 From: denDy1224 Date: Fri, 10 Jan 2025 00:41:05 +0100 Subject: [PATCH 3/5] resolve conversation - removed magic numbers --- src/main/java/core/basesyntax/Application.java | 3 ++- src/main/java/core/basesyntax/classes/Ball.java | 5 +++++ src/main/java/core/basesyntax/classes/Color.java | 5 ----- src/main/java/core/basesyntax/classes/Lottery.java | 3 ++- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/Application.java b/src/main/java/core/basesyntax/Application.java index fc2b08e0b..afdb70390 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -4,8 +4,9 @@ public class Application { public static void main(String[] args) { + final int NUMBER_OF_BALLS = 3; Lottery lottery = new Lottery(); - for (int order = 0; order < 3; order++) { + for (int order = 0; order < NUMBER_OF_BALLS; order++) { System.out.println(lottery.getRandomBall().getBallInfo()); } } diff --git a/src/main/java/core/basesyntax/classes/Ball.java b/src/main/java/core/basesyntax/classes/Ball.java index 3b6989768..9ef146724 100644 --- a/src/main/java/core/basesyntax/classes/Ball.java +++ b/src/main/java/core/basesyntax/classes/Ball.java @@ -9,6 +9,11 @@ public class Ball { this.number = number; } + @Override + public String toString() { + return null; + } + public Color getColor() { return color; } diff --git a/src/main/java/core/basesyntax/classes/Color.java b/src/main/java/core/basesyntax/classes/Color.java index 133250c33..3fee60711 100644 --- a/src/main/java/core/basesyntax/classes/Color.java +++ b/src/main/java/core/basesyntax/classes/Color.java @@ -2,9 +2,4 @@ public enum Color { RED, GREEN, BLUE, YELLOW, VIOLET, BLACK; - - @Override - public String toString() { - return name(); - } } diff --git a/src/main/java/core/basesyntax/classes/Lottery.java b/src/main/java/core/basesyntax/classes/Lottery.java index 462cf6cd9..bd052d7c9 100644 --- a/src/main/java/core/basesyntax/classes/Lottery.java +++ b/src/main/java/core/basesyntax/classes/Lottery.java @@ -6,8 +6,9 @@ public class Lottery { public Ball getRandomBall() { + int randomBounce = 100; Random random = new Random(); ColorSupplier color = new ColorSupplier(); - return new Ball(color.getRandomColor(), random.nextInt(100)); + return new Ball(color.getRandomColor(), random.nextInt(randomBounce)); } } From 89ca97a0ac5eb2dd32bd2c1d15597582335db577 Mon Sep 17 00:00:00 2001 From: denDy1224 Date: Fri, 10 Jan 2025 00:50:16 +0100 Subject: [PATCH 4/5] resolve conversation - remove magic numbers --- src/main/java/core/basesyntax/classes/Ball.java | 2 +- src/main/java/core/basesyntax/classes/Lottery.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/core/basesyntax/classes/Ball.java b/src/main/java/core/basesyntax/classes/Ball.java index 9ef146724..e41372f13 100644 --- a/src/main/java/core/basesyntax/classes/Ball.java +++ b/src/main/java/core/basesyntax/classes/Ball.java @@ -11,7 +11,7 @@ public class Ball { @Override public String toString() { - return null; + return String.format("Ball of %s color, with number %d", this.color, this.number); } public Color getColor() { diff --git a/src/main/java/core/basesyntax/classes/Lottery.java b/src/main/java/core/basesyntax/classes/Lottery.java index bd052d7c9..a57e347e5 100644 --- a/src/main/java/core/basesyntax/classes/Lottery.java +++ b/src/main/java/core/basesyntax/classes/Lottery.java @@ -6,9 +6,9 @@ public class Lottery { public Ball getRandomBall() { - int randomBounce = 100; + final int RANDOM_BOUNCE = 100; Random random = new Random(); ColorSupplier color = new ColorSupplier(); - return new Ball(color.getRandomColor(), random.nextInt(randomBounce)); + return new Ball(color.getRandomColor(), random.nextInt(RANDOM_BOUNCE)); } } From 34430755263a49b04f3235434c364917c8621d6c Mon Sep 17 00:00:00 2001 From: denDy1224 Date: Fri, 10 Jan 2025 00:51:28 +0100 Subject: [PATCH 5/5] added constructor class modifier --- src/main/java/core/basesyntax/classes/Ball.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/classes/Ball.java b/src/main/java/core/basesyntax/classes/Ball.java index e41372f13..c3d81b940 100644 --- a/src/main/java/core/basesyntax/classes/Ball.java +++ b/src/main/java/core/basesyntax/classes/Ball.java @@ -4,7 +4,7 @@ public class Ball { private Color color; private int number; - Ball(Color color, int number) { + public Ball(Color color, int number) { this.color = color; this.number = number; }