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

Created program that return balls with a random color and random number #1724

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
9 changes: 8 additions & 1 deletion src/main/java/core/basesyntax/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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();
Ball[] balls = {ball1, ball2, ball3};
for (Ball ball: balls) {
System.out.println(ball);
}

Choose a reason for hiding this comment

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

Suggested change
Ball ball1 = lottery.getRandomBall();
Ball ball2 = lottery.getRandomBall();
Ball ball3 = lottery.getRandomBall();
Ball[] balls = {ball1, ball2, ball3};
for (Ball ball: balls) {
System.out.println(ball);
}
Lottery lottery = new Lottery();
for (int i = 0; i < NUMBER_OF_BALLS; i++) {
System.out.println(lottery.getRandomBall());
}

Choose a reason for hiding this comment

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

create constant NUMBER_OF_BALLS

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

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

public int getNumber() {
return number;
}

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

public String getColor() {
return color;
}

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

@Override
public String toString() {
return "Ball{"
+ "color='" + color + '\''
+ ", number=" + number
+ '}';
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax;

public enum Color {
BLUE,
YELLOW,
BLACK,
BROWN,
PINK,
GREEN
}
7 changes: 6 additions & 1 deletion src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
private static final Random random = new Random();

public String getRandomColor() {
return null;
int index = random.nextInt(Color.values().length);
return String.valueOf(Color.values()[index]);

Choose a reason for hiding this comment

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

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

import java.util.Random;

public class Lottery {
private static final int maxNumber = 100;

Choose a reason for hiding this comment

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

private static final Random random = new Random();

Choose a reason for hiding this comment

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

Suggested change
private static final Random random = new Random();
private final Random random = new Random();

private final ColorSupplier colorSupplier = new ColorSupplier();

public Ball getRandomBall() {
Ball ball = new Ball();
ball.setColor(colorSupplier.getRandomColor());
ball.setNumber(random.nextInt(maxNumber));
return ball;
}
}
Loading