-
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
done #1735
base: master
Are you sure you want to change the base?
done #1735
Changes from 1 commit
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 |
---|---|---|
|
@@ -3,5 +3,9 @@ | |
public class Application { | ||
public static void main(String[] args) { | ||
// create three balls using class Lottery and print information about them in console | ||
Lottery[] balls = { new Lottery(), new Lottery(), new Lottery() }; | ||
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. Instead of creating multiple |
||
for (Lottery ball : balls) { | ||
System.out.println(ball.getRandomBall()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
public class Ball { | ||
private Colors color; | ||
private int number; | ||
|
||
public void setColor(Colors color) { | ||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return this.color.toString(); | ||
} | ||
|
||
public void setNumber(int number) { | ||
this.number = number; | ||
} | ||
|
||
public int getNumber() { | ||
return this.number; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return super.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. The |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class ColorSupplier { | ||
public String getRandomColor() { | ||
return null; | ||
public String getRandomColor(Ball ball) { | ||
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. The method |
||
Random random = new Random(); | ||
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. random should be a private final class level variable |
||
int index = random.nextInt(Colors.values().length); | ||
ball.setColor(Colors.values()[index]); | ||
return ball.getColor(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,10 @@ | ||||||
package core.basesyntax; | ||||||
|
||||||
public enum Colors { | ||||||
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.
Suggested change
|
||||||
Red, | ||||||
Blue, | ||||||
Green, | ||||||
Yellow, | ||||||
Orange, | ||||||
Black | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class Lottery { | ||
public String getRandomBall() { | ||
Ball ball = new Ball(); | ||
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. also, you can create a constructor with parameters for the Ball model and simplify your solution. |
||
ColorSupplier colorSupplier = new ColorSupplier(); | ||
colorSupplier.getRandomColor(ball); | ||
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. The |
||
int randomValue = new Random().nextInt(100); | ||
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. make magic number constant field |
||
ball.setNumber(randomValue); | ||
return ball.getColor() + " " + ball.getNumber(); | ||
} | ||
} |
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 manually creating each
Lottery
instance, consider using a loop to create the instances. This approach is recommended in the checklist to avoid redundancy and improve code maintainability .