Skip to content

Conversation

Oleg021
Copy link

@Oleg021 Oleg021 commented Aug 6, 2024

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 Color enum correctly! 👍 There are some suggestions for improving your Lottery class, such as using local variables, removing unnecessary methods, and reusing instances. These are minor issues that don't impact the approval of your Pull Request. Keep these tips in mind for future projects to write even more efficient and clean code! 😊👨‍💻

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


public class Application {
public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console

Choose a reason for hiding this comment

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

Please don't add redundant empty lines to your code. We don't need them after class declaration or method signature. You can remove the empty line at the start of the main method.

Comment on lines 6 to 10
public String getRandomColor() {
return null;
int index = new Random().nextInt(Color.values().length);
Color color = Color.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.

Don't create several instances of the same class if you can use only one instance for your purposes. In this case, a new Random object is created every time the getRandomColor method is called, which is unnecessary. Instead, you should create a single Random instance as a class field and reuse it.

return null;
int index = new Random().nextInt(Color.values().length);
Color color = Color.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.

Use name() for getting String representation of enum constants. Instead of using color.toString(), you should use color.name() to ensure the correct string representation is returned, especially if toString() is overridden in the enum.

Choose a reason for hiding this comment

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

fix comment

import java.util.Random;

public class Lottery {
private Random rand = new Random();

Choose a reason for hiding this comment

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

Avoid using class-level fields when local variables will suffice. The rand field can be made into a local variable within the getRandomBall method, as it does not need to be shared across multiple methods or instances.

Comment on lines 8 to 14
public Random getRand() {
return rand;
}

public void setRand(Random rand) {
this.rand = rand;
}

Choose a reason for hiding this comment

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

The getter and setter for rand are not necessary and should be removed. If the Random instance is moved to a local variable within getRandomBall, there would be no need for these methods.

}

public Ball getRandomBall() {
return new Ball(new ColorSupplier().getRandomColor(), rand.nextInt(100));

Choose a reason for hiding this comment

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

Don't create several instances of the same class if you can use only one instance for your purposes. Here, new ColorSupplier() creates a new ColorSupplier instance on every method call. It's better to create a single ColorSupplier instance and reuse it.

}

public Ball getRandomBall() {
return new Ball(new ColorSupplier().getRandomColor(), rand.nextInt(100));

Choose a reason for hiding this comment

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

Magic numbers should be avoided; consider defining a constant for the number 100 to give it meaning within the context of this method.

public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console
Lottery lottery = new Lottery();
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.

What can you use to make the same action for n objects?
What if you need to print 100 balls, it would be 100 lines of code?

public class ColorSupplier {
public String getRandomColor() {
return null;
int index = new Random().nextInt(Color.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

return null;
int index = new Random().nextInt(Color.values().length);
Color color = Color.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.

fix comment

Comment on lines 8 to 14
public Random getRand() {
return rand;
}

public void setRand(Random rand) {
this.rand = rand;
}

Choose a reason for hiding this comment

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

Suggested change
public Random getRand() {
return rand;
}
public void setRand(Random rand) {
this.rand = rand;
}

import java.util.Random;

public class Lottery {
private Random rand = 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 Random rand = new Random();
private final Random random = new Random();
private final ColorSupplier colorSupplier = new ColorSupplier();

}

public Ball getRandomBall() {
return new Ball(new ColorSupplier().getRandomColor(), rand.nextInt(100));

Choose a reason for hiding this comment

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

100 is a magic number, create constant

Comment on lines 15 to 29
public String getColor() {
return color;
}

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

public int getNumber() {
return number;
}

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

Choose a reason for hiding this comment

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

Suggested change
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}

You don't use these get and set methods

@Oleg021 Oleg021 requested a review from Elena-Bruyako August 6, 2024 10:31
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.

Great job!

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