From 6a441c651690409594b186afb34e82be2b878738 Mon Sep 17 00:00:00 2001 From: Julianevinska Date: Sun, 10 Dec 2023 23:17:59 +0100 Subject: [PATCH 1/4] added solution --- .../java/core/basesyntax/Application.java | 7 +++++ src/main/java/core/basesyntax/Ball.java | 30 +++++++++++++++++++ .../java/core/basesyntax/ColorSupplier.java | 7 ++++- src/main/java/core/basesyntax/Colors.java | 10 +++++++ src/main/java/core/basesyntax/Lottery.java | 18 +++++++++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/main/java/core/basesyntax/Ball.java create mode 100644 src/main/java/core/basesyntax/Colors.java create mode 100644 src/main/java/core/basesyntax/Lottery.java diff --git a/src/main/java/core/basesyntax/Application.java b/src/main/java/core/basesyntax/Application.java index 42ed4ef86..f94c33d4a 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -3,5 +3,12 @@ public class Application { public static void main(String[] args) { // create three balls using class Lottery and print information about them in console + Lottery ballOne = new Lottery(); + Lottery ballTwo = new Lottery(); + Lottery ballThree = new Lottery(); + + System.out.println(ballOne.getRandomBall().toString()); + System.out.println(ballTwo.getRandomBall().toString()); + System.out.println(ballThree.getRandomBall().toString()); } } diff --git a/src/main/java/core/basesyntax/Ball.java b/src/main/java/core/basesyntax/Ball.java new file mode 100644 index 000000000..72c496bab --- /dev/null +++ b/src/main/java/core/basesyntax/Ball.java @@ -0,0 +1,30 @@ +package core.basesyntax; + +public class Ball { + + private String color; + private int number; + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public int getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } + + @Override + public String toString() { + return "Ball: " + + "color - " + color + + "; number - " + number; + } +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 25e8b23b1..4594d7989 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -1,7 +1,12 @@ package core.basesyntax; +import java.util.Random; + public class ColorSupplier { public String getRandomColor() { - return null; + Random random = new Random(); + Colors[] colors = Colors.values(); + int indexOfRandom = random.nextInt(colors.length); + return colors[indexOfRandom].toString(); } } diff --git a/src/main/java/core/basesyntax/Colors.java b/src/main/java/core/basesyntax/Colors.java new file mode 100644 index 000000000..04ed6fa0b --- /dev/null +++ b/src/main/java/core/basesyntax/Colors.java @@ -0,0 +1,10 @@ +package core.basesyntax; + +public enum Colors { + RED, + WHITE, + BLACK, + GREEN, + BLUE, + ORANGE; +} diff --git a/src/main/java/core/basesyntax/Lottery.java b/src/main/java/core/basesyntax/Lottery.java new file mode 100644 index 000000000..de91ccd39 --- /dev/null +++ b/src/main/java/core/basesyntax/Lottery.java @@ -0,0 +1,18 @@ +package core.basesyntax; + +import java.util.Random; + +public class Lottery { + + Ball getRandomBall() { + Ball ball = new Ball(); + ColorSupplier colorSupplier = new ColorSupplier(); + String color = colorSupplier.getRandomColor(); + ball.setColor(color); + + Random random = new Random(); + int ballIndex = random.nextInt(101); + ball.setNumber(ballIndex); + return ball; + } +} From b07bfef8451cfeab24f9cb64fc05b28c6938f49f Mon Sep 17 00:00:00 2001 From: Julianevinska Date: Mon, 4 Mar 2024 22:13:38 +0100 Subject: [PATCH 2/4] fixed --- src/main/java/core/basesyntax/Application.java | 10 ++++------ .../java/core/basesyntax/{Colors.java => Color.java} | 2 +- src/main/java/core/basesyntax/ColorSupplier.java | 5 +++-- 3 files changed, 8 insertions(+), 9 deletions(-) rename src/main/java/core/basesyntax/{Colors.java => Color.java} (81%) diff --git a/src/main/java/core/basesyntax/Application.java b/src/main/java/core/basesyntax/Application.java index f94c33d4a..f33a323c6 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -3,12 +3,10 @@ public class Application { public static void main(String[] args) { // create three balls using class Lottery and print information about them in console - Lottery ballOne = new Lottery(); - Lottery ballTwo = new Lottery(); - Lottery ballThree = new Lottery(); + Lottery lottery = new Lottery(); - System.out.println(ballOne.getRandomBall().toString()); - System.out.println(ballTwo.getRandomBall().toString()); - System.out.println(ballThree.getRandomBall().toString()); + System.out.println(lottery.getRandomBall().toString()); + System.out.println(lottery.getRandomBall().toString()); + System.out.println(lottery.getRandomBall().toString()); } } diff --git a/src/main/java/core/basesyntax/Colors.java b/src/main/java/core/basesyntax/Color.java similarity index 81% rename from src/main/java/core/basesyntax/Colors.java rename to src/main/java/core/basesyntax/Color.java index 04ed6fa0b..b052cfc9d 100644 --- a/src/main/java/core/basesyntax/Colors.java +++ b/src/main/java/core/basesyntax/Color.java @@ -1,6 +1,6 @@ package core.basesyntax; -public enum Colors { +public enum Color { RED, WHITE, BLACK, diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 4594d7989..aac1b1d43 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -3,9 +3,10 @@ import java.util.Random; public class ColorSupplier { + private Random random = new Random(); + public String getRandomColor() { - Random random = new Random(); - Colors[] colors = Colors.values(); + Color[] colors = Color.values(); int indexOfRandom = random.nextInt(colors.length); return colors[indexOfRandom].toString(); } From 8742ef3f3b2276ff0f397301aa93b7139bec2825 Mon Sep 17 00:00:00 2001 From: Julianevinska Date: Sun, 17 Nov 2024 17:41:37 +0100 Subject: [PATCH 3/4] corrections in the checkstyle --- src/main/java/core/basesyntax/Application.java | 1 - src/main/java/core/basesyntax/Ball.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/core/basesyntax/Application.java b/src/main/java/core/basesyntax/Application.java index f33a323c6..f7489e0d4 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -2,7 +2,6 @@ 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(); System.out.println(lottery.getRandomBall().toString()); diff --git a/src/main/java/core/basesyntax/Ball.java b/src/main/java/core/basesyntax/Ball.java index 72c496bab..78381d9c2 100644 --- a/src/main/java/core/basesyntax/Ball.java +++ b/src/main/java/core/basesyntax/Ball.java @@ -1,7 +1,6 @@ package core.basesyntax; public class Ball { - private String color; private int number; From e724412537735a86dde519f8c77ecd7358e238de Mon Sep 17 00:00:00 2001 From: Julianevinska Date: Sat, 7 Dec 2024 20:35:26 +0100 Subject: [PATCH 4/4] Refactored project - Optimized Lottery - Added loops in Application - Added access modifiers --- README.md | 4 ++-- src/main/java/core/basesyntax/Application.java | 8 +++++--- src/main/java/core/basesyntax/Ball.java | 6 +++--- src/main/java/core/basesyntax/ColorSupplier.java | 10 +++++----- src/main/java/core/basesyntax/Lottery.java | 14 ++++++-------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 40ce61b0d..a8fd1dd0a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # jv-lottery -Let's create a simple lottery that will return balls with a random color and random number. +``Let's create a simple lottery that will return balls with a random color and random number. Your tasks are: - create a `Ball` class with the following fields: `color` and `number` and override the `toString()` method @@ -12,6 +12,6 @@ Then create a `Lottery` class with the `getRandomBall()` method, which will retu This method should create a ball with a random color and a random number (the maximum possible number should be 100) and return this ball from the method. To test the program, in the main() method, create three balls using the `getRandomBall()` method from the `Lottery` class. -And print information about them in the console. +And print information about them in the console.`` #### [Try to avoid these common mistakes, while solving task](./checklist.md) diff --git a/src/main/java/core/basesyntax/Application.java b/src/main/java/core/basesyntax/Application.java index f7489e0d4..aff85f8b3 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -1,11 +1,13 @@ package core.basesyntax; public class Application { + private static final int NUMBER_OF_BALLS = 3; + public static void main(String[] args) { Lottery lottery = new Lottery(); - System.out.println(lottery.getRandomBall().toString()); - System.out.println(lottery.getRandomBall().toString()); - System.out.println(lottery.getRandomBall().toString()); + for (int i = 0; i < NUMBER_OF_BALLS; i++) { + System.out.println(lottery.getRandomBall().toString()); + } } } diff --git a/src/main/java/core/basesyntax/Ball.java b/src/main/java/core/basesyntax/Ball.java index 78381d9c2..7759ac859 100644 --- a/src/main/java/core/basesyntax/Ball.java +++ b/src/main/java/core/basesyntax/Ball.java @@ -1,14 +1,14 @@ package core.basesyntax; public class Ball { - private String color; + private Color color; private int number; - public String getColor() { + public Color getColor() { return color; } - public void setColor(String color) { + public void setColor(Color color) { this.color = color; } diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index aac1b1d43..3e17bb9b5 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -3,11 +3,11 @@ import java.util.Random; public class ColorSupplier { - private Random random = new Random(); + private static final Color[] COLORS = Color.values(); + private final Random random = new Random(); - public String getRandomColor() { - Color[] colors = Color.values(); - int indexOfRandom = random.nextInt(colors.length); - return colors[indexOfRandom].toString(); + public Color getRandomColor() { + int indexOfRandom = random.nextInt(COLORS.length); + return COLORS[indexOfRandom]; } } diff --git a/src/main/java/core/basesyntax/Lottery.java b/src/main/java/core/basesyntax/Lottery.java index de91ccd39..c981b58a8 100644 --- a/src/main/java/core/basesyntax/Lottery.java +++ b/src/main/java/core/basesyntax/Lottery.java @@ -4,15 +4,13 @@ public class Lottery { - Ball getRandomBall() { - Ball ball = new Ball(); - ColorSupplier colorSupplier = new ColorSupplier(); - String color = colorSupplier.getRandomColor(); - ball.setColor(color); + private final Ball ball = new Ball(); + private final ColorSupplier colorSupplier = new ColorSupplier(); + private final Random random = new Random(); - Random random = new Random(); - int ballIndex = random.nextInt(101); - ball.setNumber(ballIndex); + public Ball getRandomBall() { + ball.setColor(colorSupplier.getRandomColor()); + ball.setNumber(random.nextInt(101)); return ball; } }