-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Added balls, random color, random number. #1998
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?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #9: 'All magic numbers in your code should be constants.' The number |
||
| System.out.println(new Lottery().getRandomBall()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #4: 'Don't create several instances of the same class if you can use only one instance for your purposes'. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #4: 'Don't create several instances of the same class if you can use only one instance for your purposes'. You should create the |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class Ball { | ||
| private String color; | ||
| private int number; | ||
|
|
||
| public String getColor() { | ||
| return this.color; | ||
| } | ||
|
|
||
| public void setColor(String color) { | ||
| this.color = color; | ||
| } | ||
|
|
||
| public int getNumber() { | ||
| return this.number; | ||
| } | ||
|
|
||
| public void setNumber(int number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Ball{ " | ||
| + "color='" + getColor() + '\'' | ||
| + "number='" + getNumber() + '\'' | ||
| + '}'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class ColorSupplier { | ||
| private final Random random = new Random(); | ||
|
|
||
| public String getRandomColor() { | ||
| return null; | ||
| return Colors.values()[random.nextInt(Colors.values().length)].toString(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #10: "Use name() for getting String representation of enum constants". The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #10: 'Use name() for getting String representation of enum constants'. As mentioned in the previous review, it's better to use the |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public enum Colors { | ||
| BLACK, | ||
| BLUE, | ||
| RED, | ||
| WHITE, | ||
| PURPLE, | ||
| PINK | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class Lottery { | ||
| private static final int FIGURE_COUNT = 101; | ||
| private final Random random = new Random(); | ||
| private Ball ball = new Ball(); | ||
|
||
|
|
||
| public Ball getRandomBall() { | ||
| String randomColor = new ColorSupplier().getRandomColor(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line has two issues:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #5: 'Dependencies like services should be class-level fields rather than local variables in a method'. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #5: 'Think about which variables should be local in the method and which should be class-level'. The |
||
| ball.setColor(randomColor); | ||
| ball.setNumber(random.nextInt(FIGURE_COUNT)); | ||
| return ball; | ||
| } | ||
|
|
||
| public Ball getBall() { | ||
| return ball; | ||
| } | ||
|
|
||
| public void setBall(Ball ball) { | ||
| this.ball = 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.
This violates checklist item #9: 'All magic numbers in your code should be constants.' The number
3should be declared as a named constant to improve readability and make the code easier to maintain.