From 007329cb0911a321eb6a5b6a1a7e2d7aa1191a12 Mon Sep 17 00:00:00 2001 From: Hanna Voloshko Date: Fri, 10 Jan 2025 23:33:44 +0200 Subject: [PATCH] created a lottery class with the getRandomBall method, which returns ball. --- .../java/core/basesyntax/Application.java | 7 +++++++ src/main/java/core/basesyntax/Ball.java | 20 ++++++++++++++++--- .../java/core/basesyntax/ColorSupplier.java | 2 +- src/main/java/core/basesyntax/Lottery.java | 11 ++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) 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..493c7872e 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 + Ball firstBall = new Lottery().getRandomBall(); + Ball secondBall = new Lottery().getRandomBall(); + Ball thirdBall = new Lottery().getRandomBall(); + + System.out.println(firstBall.toString()); + System.out.println(secondBall.toString()); + System.out.println(thirdBall.toString()); } } diff --git a/src/main/java/core/basesyntax/Ball.java b/src/main/java/core/basesyntax/Ball.java index 9a979ec09..4c118de5e 100644 --- a/src/main/java/core/basesyntax/Ball.java +++ b/src/main/java/core/basesyntax/Ball.java @@ -1,11 +1,25 @@ package core.basesyntax; public class Ball { - private String color; - private int number; + final String color; + final int number; + + public Ball(String color, int number) { + this.color = color; + this.number = number; + } + + public String getColor() { + return color; + } + + public int getNumber() { + return number; + } @Override public String toString() { - return super.toString(); + return "Information about ball:\n color: " + getColor().toLowerCase() + " number: " + getNumber(); } + } diff --git a/src/main/java/core/basesyntax/ColorSupplier.java b/src/main/java/core/basesyntax/ColorSupplier.java index 22d770932..dd407d589 100644 --- a/src/main/java/core/basesyntax/ColorSupplier.java +++ b/src/main/java/core/basesyntax/ColorSupplier.java @@ -3,7 +3,7 @@ import java.util.Random; public class ColorSupplier { - public String getRandomColor() { + public static String getRandomColor() { int index = new Random().nextInt(Color.values().length); Color color = Color.values()[index]; return color.toString(); diff --git a/src/main/java/core/basesyntax/Lottery.java b/src/main/java/core/basesyntax/Lottery.java new file mode 100644 index 000000000..22de75557 --- /dev/null +++ b/src/main/java/core/basesyntax/Lottery.java @@ -0,0 +1,11 @@ +package core.basesyntax; + +import java.util.Random; + +public class Lottery { + public Ball getRandomBall() { + String newBallColor = ColorSupplier.getRandomColor(); + int newBallNumber = new Random().nextInt(100); + return new Ball(newBallColor, newBallNumber); + } +}