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

Conversation

yaroslav-pryshchepa
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Great job on implementing the task! 🎉 Your code meets the core requirements and functions as expected. While there are a few areas for improvement, such as reusing instances of Lottery and ColorSupplier to enhance efficiency, these are minor and don't impact the overall functionality. Keep up the good work and consider these suggestions for future projects to make your code even more efficient and maintainable! 😊

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

Comment on lines 5 to 7
Ball ball1 = new Lottery().getRandomBall();
Ball ball2 = new Lottery().getRandomBall();
Ball ball3 = new Lottery().getRandomBall();

Choose a reason for hiding this comment

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

You are creating multiple instances of the Lottery class. According to the checklist, it's better to create a single instance and reuse it. Consider creating one Lottery instance and using it to generate all three balls.

public class Lottery {
Ball getRandomBall() {
Ball ball = new Ball();
ball.color = String.valueOf(new ColorSupplier().getRandomColor());

Choose a reason for hiding this comment

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

You are creating a new ColorSupplier instance each time getRandomBall() is called. Consider creating a single instance of ColorSupplier as a class member to avoid unnecessary object creation.

@@ -0,0 +1,10 @@
package core.basesyntax;

public enum Colors {

Choose a reason for hiding this comment

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

Suggested change
public enum Colors {
public enum Color {

public String getRandomColor() {
return null;
public Colors getRandomColor() {
int index = new Random().nextInt(Colors.values().length);

Choose a reason for hiding this comment

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

Random should be class-level variable

public class ColorSupplier {
public String getRandomColor() {
return null;
public Colors getRandomColor() {

Choose a reason for hiding this comment

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

Suggested change
public Colors getRandomColor() {
public String getRandomColor() {

Ball getRandomBall() {
Ball ball = new Ball();
ball.setColor(String.valueOf(colorSupplier.getRandomColor()));
ball.setNumber(new Random().nextInt(100));

Choose a reason for hiding this comment

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

Random should be class-level variable
create constant for magic number 100

@@ -2,6 +2,9 @@

public class Application {
public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console
for (int i = 0; i < 3; i++) {

Choose a reason for hiding this comment

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

3 is a magic number, create constant

public class Lottery {
private final ColorSupplier colorSupplier = new ColorSupplier();

Ball getRandomBall() {

Choose a reason for hiding this comment

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

Suggested change
Ball getRandomBall() {
public Ball getRandomBall() {

@@ -2,6 +2,9 @@

public class Application {
public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console
for (int i = 0; i < 3; i++) {
Ball ball = new Lottery().getRandomBall();

Choose a reason for hiding this comment

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

don't create new Lottery in for loop

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.

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.

Comment on lines 6 to 12
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


public class Lottery {
private static final int maxNumber = 100;
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();

Copy link

@okuzan okuzan left a comment

Choose a reason for hiding this comment

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

You're almost there!


public class Lottery {
private static final int MAX_NUMBER = 100;
private static final Random RANDOM = new Random();
Copy link

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();

let's not overuse static - you will be using java with Spring Boot, and we don't use static to inject dependencies there
also, beware that final + static doesn't always mean constants (it's debatable, some say object must be immutable too)

Comment on lines 12 to 13
ball.setColor(colorSupplier.getRandomColor());
ball.setNumber(RANDOM.nextInt(MAX_NUMBER));
Copy link

Choose a reason for hiding this comment

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

minor thing - consider creating a constructor for those two fields

Copy link

@okuzan okuzan left a comment

Choose a reason for hiding this comment

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

😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants