Skip to content

Commit fc7db62

Browse files
created a class-level variable, created a for loop, created a separate class for enum
1 parent 4398193 commit fc7db62

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

src/main/java/core/basesyntax/Application.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
public class Application {
44
public static void main(String[] args) {
55
Lottery lottery = new Lottery();
6-
Ball ball1 = lottery.getRandomBall();
7-
Ball ball2 = lottery.getRandomBall();
8-
Ball ball3 = lottery.getRandomBall();
96

10-
System.out.println(ball1);
11-
System.out.println(ball2);
12-
System.out.println(ball3);
7+
for (int i = 0; i < 3; i++) {
8+
Ball ball = lottery.getRandomBall();
9+
System.out.println(ball);
10+
}
1311
}
1412
}

src/main/java/core/basesyntax/Ball.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,4 @@ public String toString() {
1818
+ number
1919
+ " }";
2020
}
21-
22-
public enum Color {
23-
RED, BLUE, GREEN, YELLOW, ORANGE, PURPLE, PINK;
24-
private static Color[] colors = Color.values();
25-
26-
public static Color getByIndex(int index) {
27-
return colors[index];
28-
}
29-
}
3021
}
31-
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package core.basesyntax;
2+
3+
public enum Color {
4+
RED, BLUE, GREEN, YELLOW, ORANGE, PURPLE, PINK;
5+
private static Color[] colors = Color.values();
6+
7+
public static Color getByIndex(int index) {
8+
return colors[index];
9+
}
10+
}

src/main/java/core/basesyntax/ColorSupplier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import java.util.Random;
44

55
public class ColorSupplier {
6+
private int index = new Random().nextInt(Color.values().length);
67

78
public String getRandomColor() {
8-
int index = new Random().nextInt(Ball.Color.values().length);
9-
return Ball.Color.getByIndex(index).name();
9+
return Color.getByIndex(index).name();
1010
}
1111
}

src/main/java/core/basesyntax/Lottery.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import java.util.Random;
44

55
class Lottery {
6+
private int maxValue = 101;
67
private Random random = new Random();
78
private ColorSupplier colorSupplier = new ColorSupplier();
89

910
public Ball getRandomBall() {
1011
String color = colorSupplier.getRandomColor();
11-
int number = random.nextInt(101);
12+
int number = random.nextInt(maxValue);
1213
return new Ball(color, number);
1314
}
1415
}

0 commit comments

Comments
 (0)