File tree Expand file tree Collapse file tree 5 files changed +20
-21
lines changed
src/main/java/core/basesyntax Expand file tree Collapse file tree 5 files changed +20
-21
lines changed Original file line number Diff line number Diff line change 1
1
package core .basesyntax ;
2
2
3
3
public class Application {
4
+ private static final int NUMBER_OF_BALLS = 3 ;
4
5
public static void main (String [] args ) {
5
6
Lottery lottery = new Lottery ();
6
- Ball [] balls = new Ball [3 ];
7
+ Ball [] balls = new Ball [NUMBER_OF_BALLS ];
7
8
for (int i = 0 ; i < 3 ; i ++) {
8
9
balls [i ] = lottery .getRandomBall ();
9
10
System .out .println (balls [i ].toString ());
Original file line number Diff line number Diff line change 1
1
package core .basesyntax ;
2
2
3
3
public class Ball {
4
- private String color ;
4
+ private Color color ;
5
5
private int number ;
6
- public void setColor ( String color ) {
6
+ public Ball ( Color color , int number ) {
7
7
this .color = color ;
8
- }
9
- public void setNumber (int number ) {
10
8
this .number = number ;
11
9
}
12
10
public String getColor () {
13
- return color ;
11
+ return color . toString () ;
14
12
}
15
13
public int getNumber () {
16
14
return number ;
Original file line number Diff line number Diff line change 1
1
package core .basesyntax ;
2
2
3
3
public enum Color {
4
- pink ,
5
- blue ,
6
- yellow ,
7
- green ,
8
- red ,
9
- purple
4
+ PINK ,
5
+ BLUE ,
6
+ YELLOW ,
7
+ GREEN ,
8
+ RED ,
9
+ PURPLE
10
10
}
Original file line number Diff line number Diff line change 3
3
import java .util .Random ;
4
4
5
5
public class ColorSupplier {
6
- public String getRandomColor () {
7
- int index = new Random ().nextInt (Color .values ().length );
8
- return Color .values ()[index ].toString ();
6
+ private Random random ;
7
+ public Color getRandomColor () {
8
+ int index = random .nextInt (Color .values ().length );
9
+ return Color .values ()[index ];
9
10
}
10
11
}
Original file line number Diff line number Diff line change 3
3
import java .util .Random ;
4
4
5
5
public class Lottery {
6
+ private Random random ;
7
+ private static final int MAX_BALL_NUMBER = 100 ;
6
8
ColorSupplier colorSupplier = new ColorSupplier ();
7
9
public Ball getRandomBall () {
8
- Random random = new Random ();
9
- int number = random .nextInt (100 );
10
- String color = colorSupplier .getRandomColor ();
11
- Ball ball = new Ball ();
12
- ball .setColor (color );
13
- ball .setNumber (number );
10
+ int number = random .nextInt (MAX_BALL_NUMBER );
11
+ Color color = colorSupplier .getRandomColor ();
12
+ Ball ball = new Ball (color , number );
14
13
return ball ;
15
14
}
16
15
}
You can’t perform that action at this time.
0 commit comments