Skip to content

Commit f08d98f

Browse files
create lottery with a random balls
1 parent b478459 commit f08d98f

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
public class Application {
44
public static void main(String[] args) {
55
// create three balls using class Lottery and print information about them in console
6+
Lottery lottery = new Lottery();
7+
Ball firstBall = lottery.getRandomBall();
8+
Ball secondBall = lottery.getRandomBall();
9+
Ball thirdBall = lottery.getRandomBall();
10+
System.out.println(firstBall.toString());
11+
System.out.println(secondBall.toString());
12+
System.out.println(thirdBall.toString());
613
}
714
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package core.basesyntax;
2+
3+
public class Ball {
4+
5+
private String colour;
6+
7+
private int number;
8+
9+
public Ball(String colour, int number) {
10+
this.colour = colour;
11+
this.number = number;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return "Ball colour is " + colour
17+
+ " number is " + number;
18+
}
19+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package core.basesyntax;
22

3+
import java.util.Random;
4+
35
public class ColorSupplier {
46
public String getRandomColor() {
5-
return null;
7+
int index = new Random().nextInt(Colours.values().length);
8+
Colours colour = Colours.values()[index];
9+
return colour.toString();
610
}
711
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package core.basesyntax;
2+
3+
public enum Colours {
4+
PINK,
5+
PURPLE,
6+
BLUE,
7+
RED,
8+
GREEN,
9+
BLACK,
10+
WHITE
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package core.basesyntax;
2+
3+
import java.util.Random;
4+
5+
public class Lottery {
6+
private final ColorSupplier colorSupplier = new ColorSupplier();
7+
8+
public Ball getRandomBall() {
9+
String randomColour = colorSupplier.getRandomColor();
10+
int randomNumber = new Random().nextInt(100);
11+
return new Ball(randomColour, randomNumber);
12+
}
13+
}

0 commit comments

Comments
 (0)