-
Notifications
You must be signed in to change notification settings - Fork 1.5k
lottety-hw-01 #1575
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
base: master
Are you sure you want to change the base?
lottety-hw-01 #1575
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 Application and Ball classes! 😊 Your use of the enum's name() method in the Ball class is spot-on for getting the color representation. As suggested, consider using loops to handle repetitive tasks in the future to make your code even more efficient and maintainable. Keep up the good work! 👍
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
Ball ball1 = lottery.getRandomBall(); | ||
Ball ball2 = lottery.getRandomBall(); | ||
Ball ball3 = lottery.getRandomBall(); | ||
|
||
System.out.println(ball1); | ||
System.out.println(ball2); | ||
System.out.println(ball3); |
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.
Instead of creating multiple Ball instances separately, consider using a for-loop to create and print the balls. This will make the code more concise and easier to maintain if you need to generate a different number of balls in the future. Refer to the checklist about using a for loop for creating several objects of the same class.
public String getRandomColor() { | ||
return null; | ||
public Color getRandomColor() { | ||
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.
Creating a new instance of 'Random' each time the method is called can be inefficient if the method is called multiple times. Consider making 'random' a class-level instance variable.
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.
fix comment
|
||
import java.util.Random; | ||
|
||
class Lottery { |
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.
Classes must have access modifiers; otherwise, the default will be used, which isn't a good practice. Consider adding an access modifier to the Lottery class.
|
||
class Lottery { | ||
public Ball getRandomBall() { | ||
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.
The instance of Random could be a class-level field instead of being created every time the 'getRandomBall' method is called. This would be more efficient as it avoids unnecessary object creation.
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.
fix comment
class Lottery { | ||
public Ball getRandomBall() { | ||
Random random = new Random(); | ||
Color color = 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.
Creating a new instance of ColorSupplier every time may be inefficient if the getRandomColor method does not depend on instance state. Consider making ColorSupplier a class-level field or its method static if there's no state.
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.
fix comment
public Ball getRandomBall() { | ||
Random random = new Random(); | ||
Color color = new ColorSupplier().getRandomColor(); | ||
int number = random.nextInt(101); |
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.
Magic numbers should be avoided and replaced with named constants for better code readability. Consider creating a constant for the number 101, giving it a meaningful name that explains what it represents.
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.
fix comment
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.
not fixed
public static void main(String[] args) { | ||
// create three balls using class Lottery and print information about them in console | ||
Lottery lottery = new Lottery(); | ||
Ball ball1 = 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.
Please use for loop.
if you have 100 balls, you won't need to create 100 lines of code
package core.basesyntax; | ||
|
||
public class Ball { | ||
private Color color; |
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 Color color; | |
private String color; |
package core.basesyntax; | ||
|
||
public enum Color { | ||
RED, GREEN, BLUE, YELLOW, ORANGE, BLACK, WHITE |
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.
RED, GREEN, BLUE, YELLOW, ORANGE, BLACK, WHITE | |
RED, | |
GREEN, | |
BLUE, | |
YELLOW, | |
ORANGE, | |
BLACK, | |
WHITE |
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.
not fixed
public class ColorSupplier { | ||
public String getRandomColor() { | ||
return null; | ||
public Color 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 Color getRandomColor() { | |
public String 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.
not fixed
public String getRandomColor() { | ||
return null; | ||
public Color getRandomColor() { | ||
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.
fix comment
public Ball getRandomBall() { | ||
Random random = new Random(); | ||
Color color = new ColorSupplier().getRandomColor(); | ||
int number = random.nextInt(101); |
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.
fix comment
|
||
class Lottery { | ||
public Ball getRandomBall() { | ||
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.
fix comment
class Lottery { | ||
public Ball getRandomBall() { | ||
Random random = new Random(); | ||
Color color = 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.
fix comment
|
||
Lottery lottery = new Lottery(); | ||
|
||
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
Ball ball = lottery.getRandomBall(); | ||
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 ball = lottery.getRandomBall(); | |
System.out.println(ball); | |
System.out.println(lottery.getRandomBall()); |
private String color; | ||
private int number; | ||
|
||
public Ball(Color color, int 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.
public Ball(Color color, int number) { | |
public Ball(String color, int number) { |
|
||
public Ball(Color color, int number) { | ||
this.number = number; | ||
this.color = String.valueOf(color); |
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.
this.color = String.valueOf(color); | |
this.color = color; |
public class ColorSupplier { | ||
public String getRandomColor() { | ||
return null; | ||
public Color 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.
not fixed
public Ball getRandomBall() { | ||
Random random = new Random(); | ||
Color color = new ColorSupplier().getRandomColor(); | ||
int number = random.nextInt(101); |
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.
not fixed
|
||
class Lottery { | ||
private Random random = new Random(); | ||
private Color color = 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.
private Color color = new ColorSupplier().getRandomColor(); | |
private ColorSupplier colorSupplier = new ColorSupplier(); |
|
||
public Ball getRandomBall() { | ||
int number = random.nextInt(101); | ||
return new Ball(color, 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.
return new Ball(color, number); | |
String randomColor = colorSupplier.getRandomColor(); | |
return new Ball(randomColor, number); |
public Color getRandomColor() { | ||
Color[] colors = Color.values(); | ||
return colors[random.nextInt(colors.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.
return in String type
|
||
public Ball getRandomBall() { | ||
int number = random.nextInt(101); |
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.
Save to constant
|
||
public Ball getRandomBall() { | ||
int number = random.nextInt(maxValue); | ||
String randomColor = String.valueOf(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.
String randomColor = String.valueOf(colorSupplier.getRandomColor()); | |
String randomColor = colorSupplier.getRandomColor(); |
public class Application { | ||
public static void main(String[] args) { | ||
// create three balls using class Lottery and print information about them in console | ||
int ballsNumber = 3; |
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.
it should be constant
package core.basesyntax; | ||
|
||
public enum Color { | ||
RED, GREEN, BLUE, YELLOW, ORANGE, BLACK, WHITE |
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.
not fixed
|
||
public String getRandomColor() { | ||
return null; | ||
Color[] colors = Color.values(); |
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 can create it as class-level variable
|
||
import java.util.Random; | ||
|
||
class Lottery { |
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.
class Lottery { | |
public class Lottery { |
import java.util.Random; | ||
|
||
class Lottery { | ||
private int maxValue = 101; |
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.
make it constant
No description provided.