From 1ed4d69da6f2710f56ba599a8ea196067251db4f Mon Sep 17 00:00:00 2001 From: ivanlavrinenko Date: Thu, 26 Dec 2024 23:27:20 +0100 Subject: [PATCH 1/2] implemented lottery class with colorSupplier and Ball class --- src/main/java/core/basesyntax/Application.java | 10 +++++++++- src/main/java/core/basesyntax/Ball.java | 16 ++++++++++++++++ src/main/java/core/basesyntax/Color.java | 9 +++++++++ src/main/java/core/basesyntax/ColorSupplier.java | 9 +++++++-- src/main/java/core/basesyntax/Lottery.java | 14 ++++++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/main/java/core/basesyntax/Ball.java create mode 100644 src/main/java/core/basesyntax/Color.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..528ece1f9 100644 --- a/src/main/java/core/basesyntax/Application.java +++ b/src/main/java/core/basesyntax/Application.java @@ -2,6 +2,14 @@ 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(); + + Ball ball1 = lottery.getRandomBall(); + Ball ball2 = lottery.getRandomBall(); + Ball ball3 = lottery.getRandomBall(); + + System.out.println(ball1.toString()); + System.out.println(ball2.toString()); + System.out.println(ball3.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..9203f832f --- /dev/null +++ b/src/main/java/core/basesyntax/Ball.java @@ -0,0 +1,16 @@ +package core.basesyntax; + +public class Ball { + Color color; + Integer number; + + public Ball(Color color, Integer number) { + this.color = color; + this.number = number; + } + + @Override + public String toString() { + return this.color.toString() + this.number.toString(); + } +} diff --git a/src/main/java/core/basesyntax/Color.java b/src/main/java/core/basesyntax/Color.java new file mode 100644 index 000000000..e3d3385d1 --- /dev/null +++ b/src/main/java/core/basesyntax/Color.java @@ -0,0 +1,9 @@ +package core.basesyntax; + +public enum Color { + RED, + BROWN, + YELLOW, + GREEN, + BLUE +} diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 25e8b23b1..19becf00a 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; + public Color getRandomColor() { + Random random = new Random(); + int index = random.nextInt(Color.values().length); + + return Color.values()[index]; } } diff --git a/src/main/java/core/basesyntax/Lottery.java b/src/main/java/core/basesyntax/Lottery.java new file mode 100644 index 000000000..bbd8c680c --- /dev/null +++ b/src/main/java/core/basesyntax/Lottery.java @@ -0,0 +1,14 @@ +package core.basesyntax; + +import java.util.Random; + +public class Lottery { + public Ball getRandomBall() { + ColorSupplier colorSupplier = new ColorSupplier(); + + Color color = colorSupplier.getRandomColor(); + Integer number = new Random().nextInt(101); + + return new Ball(color, number); + } +} From acd98836a272788d220318ca4ab8dd3e6e98bfc7 Mon Sep 17 00:00:00 2001 From: ivanlavrinenko Date: Thu, 26 Dec 2024 23:31:33 +0100 Subject: [PATCH 2/2] added getter and setter --- src/main/java/core/basesyntax/Ball.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/Ball.java b/src/main/java/core/basesyntax/Ball.java index 9203f832f..382039095 100644 --- a/src/main/java/core/basesyntax/Ball.java +++ b/src/main/java/core/basesyntax/Ball.java @@ -1,8 +1,8 @@ package core.basesyntax; public class Ball { - Color color; - Integer number; + private Color color; + private Integer number; public Ball(Color color, Integer number) { this.color = color; @@ -13,4 +13,20 @@ public Ball(Color color, Integer number) { public String toString() { return this.color.toString() + this.number.toString(); } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + + public Integer getNumber() { + return number; + } + + public void setNumber(Integer number) { + this.number = number; + } }