Skip to content

Commit c3479ac

Browse files
committed
Created classes Ball, ColorSupplier,Lottery. Created enum Colors.
Added solution to task.
1 parent b478459 commit c3479ac

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
public class Application {
44
public static void main(String[] args) {
5-
// create three balls using class Lottery and print information about them in console
5+
Lottery lottery = new Lottery();
6+
lottery.getRandomBall();
7+
lottery.getRandomBall();
8+
lottery.getRandomBall();
69
}
710
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package core.basesyntax;
2+
3+
public class Ball {
4+
private String color;
5+
private int number;
6+
7+
public String getColor() {
8+
return color;
9+
}
10+
11+
public void setColor(String color) {
12+
this.color = color;
13+
}
14+
15+
public int getNumber() {
16+
return number;
17+
}
18+
19+
public void setNumber(int number) {
20+
this.number = number;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "Ball{"
26+
+ "color='" + color + '\''
27+
+ ", number=" + number
28+
+ '}';
29+
}
30+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package core.basesyntax;
22

3+
import java.util.Random;
4+
35
public class ColorSupplier {
6+
47
public String getRandomColor() {
5-
return null;
8+
int index = new Random().nextInt(Colors.values().length);
9+
String randomColor = String.valueOf(Colors.values()[index]);
10+
return randomColor;
611
}
712
}
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 Colors {
4+
RED,
5+
ORANGE,
6+
YELLOW,
7+
GREEN,
8+
BLUE,
9+
INDIGO,
10+
VIOLET
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 extends Ball {
6+
7+
public void getRandomBall() {
8+
ColorSupplier colorSupplier = new ColorSupplier();
9+
setNumber(new Random().nextInt(100));
10+
setColor(colorSupplier.getRandomColor());
11+
System.out.println("The ball with number " + getNumber() + " is " + getColor());
12+
}
13+
}

0 commit comments

Comments
 (0)