-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: master
Are you sure you want to change the base?
Created program that return balls with a random color and random number #1724
Conversation
There was a problem hiding this 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! ✨
Ball ball1 = new Lottery().getRandomBall(); | ||
Ball ball2 = new Lottery().getRandomBall(); | ||
Ball ball3 = new Lottery().getRandomBall(); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public enum Colors { | |
public enum Color { |
public String getRandomColor() { | ||
return null; | ||
public Colors getRandomColor() { | ||
int index = new Random().nextInt(Colors.values().length); |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public Colors getRandomColor() { | |
public String getRandomColor() { |
Ball getRandomBall() { | ||
Ball ball = new Ball(); | ||
ball.setColor(String.valueOf(colorSupplier.getRandomColor())); | ||
ball.setNumber(new Random().nextInt(100)); |
There was a problem hiding this comment.
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++) { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(); |
There was a problem hiding this comment.
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]); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()); | |
} |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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(); | |
private final Random random = new Random(); |
There was a problem hiding this 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(); |
There was a problem hiding this comment.
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(); | |
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)
ball.setColor(colorSupplier.getRandomColor()); | ||
ball.setNumber(RANDOM.nextInt(MAX_NUMBER)); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😎
No description provided.