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

add getRandomColor and getRandomBall foo #1729

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 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,11 @@
public class Application {
public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console
Ball[] balls = new Ball[3];

for (int i = 0; i < balls.length;) {
balls[i] = new Lottery().getRandomBall();
System.out.println(balls[i]);
}
Comment on lines +8 to +11

Choose a reason for hiding this comment

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

The loop does not increment the index variable i, which results in an infinite loop. You should increment i in each iteration to ensure the loop progresses and initializes all elements of the balls array.

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

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

@Override
public String toString() {
return this.color + " ball with number: " + this.number;
}

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

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

public String getColor() {
return color;
}

public int getNumber() {
return number;
}
}
9 changes: 7 additions & 2 deletions 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 {
public String getRandomColor() {
return null;
public final String getRandomColor() {
int index = new Random().nextInt(Colors.values().length);
Colors color = Colors.values()[index];

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

public enum Colors {
WHITE,
BLACK,
RED,
YELLOW,
BLUE,
ORANGE,
GREEN,
BROWN
}
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 final Ball getRandomBall() {
Ball ball = new Ball();
ball.setNumber(new Random().nextInt(100));
ball.setColor(new ColorSupplier().getRandomColor());

return ball;
}
}
Loading