Add lottery: Ball, Color enum, ColorSupplier, Lottery and main test#1997
Add lottery: Ball, Color enum, ColorSupplier, Lottery and main test#1997chupa-ilona wants to merge 20 commits intomate-academy:masterfrom
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Great job implementing the core logic! The program works correctly. However, some changes are needed to meet the project's coding standards before this can be approved. Please address the following checklist violations:
- In
ColorSupplier.javaandLottery.java, you are creating newRandomandColorSupplierobjects on every method call. It's more efficient to create these as private fields and reuse the same instance. [CHECKLIST ITEM #4] - The numbers
3inApplication.javaand100inLottery.javaare 'magic numbers'. Please declare them as named constants (e.g.,private static final int MAX_BALL_NUMBER = 100;). [CHECKLIST ITEM #9] - Avoid creating temporary variables when you can return a value directly from a method, for example in
Lottery.javaandColorSupplier.java. [CHECKLIST ITEM #2] - Use the
.name()method instead of.toString()for getting the string value of an enum. [CHECKLIST ITEM #10]
Addressing these points will significantly improve your code's quality and adherence to best practices.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
| public class ColorSupplier { | ||
| public String getRandomColor() { | ||
| return null; | ||
| Colors rand_color = Colors.values()[new Random().nextInt(Colors.values().length)]; |
There was a problem hiding this comment.
This violates checklist item #4: 'Don't create several instances of the same class if you can use only one instance for your purposes'. A new Random() object is created with every method call. It is more efficient to create a single Random instance as a private field of the ColorSupplier class.
| public class ColorSupplier { | ||
| public String getRandomColor() { | ||
| return null; | ||
| Colors rand_color = Colors.values()[new Random().nextInt(Colors.values().length)]; |
There was a problem hiding this comment.
This line introduces a temporary variable rand_color, which violates checklist item #2: 'Don't create temporary variables if you can directly return value from the method'. The value could be returned directly.
| public String getRandomColor() { | ||
| return null; | ||
| Colors rand_color = Colors.values()[new Random().nextInt(Colors.values().length)]; | ||
| return rand_color.toString(); |
There was a problem hiding this comment.
This violates checklist item #10: 'Use name() for getting String representation of enum constants'. The name() method should be used instead of toString() to get the string representation of an enum constant.
|
|
||
| public class Lottery { | ||
| public Ball getRandomBall(){ | ||
| Ball ball = new Ball(new ColorSupplier().getRandomColor(), |
There was a problem hiding this comment.
This violates checklist item #4: 'Don't create several instances of the same class if you can use only one instance for your purposes'. A new ColorSupplier object is created on every call to getRandomBall(). It's more efficient to make ColorSupplier a private field of the Lottery class and initialize it once.
| public class Lottery { | ||
| public Ball getRandomBall(){ | ||
| Ball ball = new Ball(new ColorSupplier().getRandomColor(), | ||
| new Random().nextInt(100) + 1); |
There was a problem hiding this comment.
This violates checklist item #9: 'All magic numbers in your code should be constants.' The number 100 should be declared as a named constant to improve readability and maintainability, for example private static final int MAX_BALL_NUMBER = 100;.
| public class Lottery { | ||
| public Ball getRandomBall(){ | ||
| Ball ball = new Ball(new ColorSupplier().getRandomColor(), | ||
| new Random().nextInt(100) + 1); |
There was a problem hiding this comment.
This violates checklist item #4: 'Don't create several instances of the same class if you can use only one instance for your purposes'. Creating a new Random object each time the method is called is inefficient. It's better practice to create a single Random instance as a private field within the Lottery class.
| Ball ball = new Ball(new ColorSupplier().getRandomColor(), | ||
| new Random().nextInt(100) + 1); | ||
| return ball; |
There was a problem hiding this comment.
This violates checklist item #2: 'Don't create temporary variables if you can directly return value from the method'. You can create and return the Ball object in a single line, like return new Ball(...).
made BALLS_COUNT non final
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent progress on this task! You've successfully addressed almost all the feedback from the previous review, and your code is very close to being perfect.
However, one important issue from the last review remains. In src/main/java/core/basesyntax/Application.java, the number 3 is still used as a 'magic number'. This violates a core requirement to declare all such numbers as constants for better readability and maintainability [CHECKLIST ITEM #9].
Please declare it as a named constant, for example:
private static final int BALLS_COUNT = 3;
Once you make this small correction, your solution will be ready for approval. Keep up the great work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work! You've successfully addressed the feedback from the previous review, and the code is now in great shape. Your solution correctly implements all functional requirements and adheres to the specified coding standards, especially with the great use of constants for all magic numbers. I am gladly approving your solution, keep up the fantastic work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
No description provided.