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 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
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 (Ball ball: balls) {
ball = new Lottery().getRandomBall();
System.out.println(ball);
}

Choose a reason for hiding this comment

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

The enhanced for-loop does not update the balls array with the new Ball objects. Instead, it assigns the new Ball to the ball variable, which is a copy of the array element. To fix this, use a traditional for-loop to iterate over the array indices and assign the new Ball objects directly to the array elements.

}
}
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;

import java.awt.*;
import java.util.Random;

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.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() to get the string representation of the enum constant, use the name() method. This is a more reliable way to get the string representation of an enum constant, as toString() can be overridden and may not return the expected result.

}
}
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
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/Lottery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax;

import java.lang.reflect.Array;
import java.util.Arrays;
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