From 09fea1029110b8f08c8200b705cf02a8e769af1d Mon Sep 17 00:00:00 2001 From: Bohdan Lysachenko Date: Tue, 19 Nov 2024 12:11:52 +0200 Subject: [PATCH] Implement Lottery --- .../java/core/basesyntax/Application.java | 4 ++- src/main/java/core/basesyntax/Ball.java | 27 +++++++++++++++++++ .../java/core/basesyntax/ColorSupplier.java | 6 +++-- src/main/java/core/basesyntax/Colors.java | 14 ++++++++++ src/main/java/core/basesyntax/Lottery.java | 9 +++++++ 5 files changed, 57 insertions(+), 3 deletions(-) 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..5eefa9d34 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -2,6 +2,8 @@ public class Application { public static void main(String[] args) { - // create three balls using class Lottery and print information about them in console + for (int i = 0; i < 3; i++) { + System.out.println(Lottery.getRandomBall()); + } } } diff --git a/src/main/java/core/basesyntax/Ball.java b/src/main/java/core/basesyntax/Ball.java new file mode 100644 index 000000000..e2ae89937 --- /dev/null +++ b/src/main/java/core/basesyntax/Ball.java @@ -0,0 +1,27 @@ +package core.basesyntax; + +public class Ball { + private final Colors color; + private final int number; + + public Ball(Colors randomColor, int number) { + this.color = randomColor; + this.number = number; + } + + @Override + public String toString() { + return "Ball{" + + "color='" + getColor() + '\'' + + ", number=" + getNumber() + + '}'; + } + + public Colors getColor() { + return color; + } + + public int getNumber() { + return number; + } +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 25e8b23b1..9230253d4 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -1,7 +1,9 @@ package core.basesyntax; +import java.util.Random; + public class ColorSupplier { - public String getRandomColor() { - return null; + public static Colors getRandomColor() { + return Colors.values()[new Random().nextInt(Colors.values().length)]; } } diff --git a/src/main/java/core/basesyntax/Colors.java b/src/main/java/core/basesyntax/Colors.java new file mode 100644 index 000000000..6b55cfe62 --- /dev/null +++ b/src/main/java/core/basesyntax/Colors.java @@ -0,0 +1,14 @@ +package core.basesyntax; + +public enum Colors { + RED, + GREEN, + BLUE, + YELLOW, + ORANGE, + PURPLE, + CYAN, + MAGENTA, + BLACK, + WHITE +} diff --git a/src/main/java/core/basesyntax/Lottery.java b/src/main/java/core/basesyntax/Lottery.java new file mode 100644 index 000000000..55de50ff6 --- /dev/null +++ b/src/main/java/core/basesyntax/Lottery.java @@ -0,0 +1,9 @@ +package core.basesyntax; + +import java.util.Random; + +public class Lottery { + public static Ball getRandomBall() { + return new Ball(ColorSupplier.getRandomColor(), new Random().nextInt(101)); + } +}