forked from mate-academy/jv-lottery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLottery.java
More file actions
55 lines (44 loc) · 1.31 KB
/
Lottery.java
File metadata and controls
55 lines (44 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Random;
enum Color {
GREEN, BLUE, PURPLE, WHITE, BLACK, YELLOW, ORANGE
}
class ColorSupplier {
private final Random random = new Random();
public String getRandomColor() {
Color[] colors = Color.values();
int index = random.nextInt(colors.length);
return colors[index].name();
}
}
class Ball {
private String color;
private int number;
public Ball(String color, int number) {
this.color = color;
this.number = number;
}
@Override
public String toString() {
return "Ball" + color + ", number:" + number;
}
}
class Lottery {
private final ColorSupplier colorSupplier = new ColorSupplier();
private final Random random = new Random();
private static final int MAX_NUMBER = 100;
public Ball getRandomBall() {
String color = colorSupplier.getRandomColor();
int number = random.nextInt(MAX_NUMBER + 1);
return new Ball(color, number);
}
}
public class Main {
public static void main(String[] args) {
Lottery lottery = new Lottery();
System.out.println("Drawing 3 balls");
for (int i = 1; i <= 3; i++) {
Ball ball = lottery.getRandomBall();
System.out.println("Draw #" + i + ": " + ball);
}
}
}