Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maked lottery task #1693

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/core/basesyntax/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
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 i = 0; i < 3; i++) {
System.out.println(lottery.getRandomBall());
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax;

public class Ball {
private int number;
private String color;

public void setNumber (int number) {
this.number = number;
}

public void setColor (String color) {
this.color = color;
}

@Override
public String toString() {
return "Color - " + this.color + ", number - " + this.number;
}
}
5 changes: 4 additions & 1 deletion src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
public String getRandomColor() {
return null;
int index = new Random().nextInt(Colors.values().length);
return Colors.values()[index].toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using toString(), use the name() method to get the string representation of the enum constants. This is a more reliable approach as toString() can be overridden, potentially leading to unexpected results.

}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

public enum Colors {
RED,
BLACK,
WHITE,
GREEN,
BLUE,
YELLOW,
ORANGE
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/Lottery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax;

import java.util.Random;

public class Lottery {
public Ball getRandomBall() {
Ball ball = new Ball();
ColorSupplier colorSupplier = new ColorSupplier();
ball.setColor(colorSupplier.getRandomColor());
ball.setNumber(new Random().nextInt(100));
return ball;
}
}
Loading