Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ball class with fields color and number created with toString() metho… #1737

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
public class Application {
public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console
Lottery lottery = new Lottery();
Ball ballOne = lottery.getRandomBall();
Ball ballTwo = lottery.getRandomBall();
Ball ballThree = lottery.getRandomBall();
Comment on lines +7 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a loop to create the three Ball objects. This approach is recommended in the checklist to avoid repetitive code when creating multiple instances of the same class .

System.out.println(ballOne.toString());
System.out.println(ballTwo.toString());
System.out.println(ballThree.toString());
}
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax;

public class Ball {
private String color;
private int number;

public Ball(String newColor, int newNumber) {
this.color = newColor;
this.number = newNumber;
}

@Override
public String toString() {
return "Color is " + color + ", number is " + number;
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax;

public enum Color {
BLACK,
WHITE,
GREEN,
BLUE,
YELLOW
}
5 changes: 4 additions & 1 deletion src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
public String getRandomColor() {
return null;
int index = new Random().nextInt(Color.values().length);
return Color.values()[index].toString();
}
}
17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/Lottery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax;

import java.util.Random;

public class Lottery {
public Ball getRandomBall() {
ColorSupplier supplier = new ColorSupplier();
int index;
String color;

color = supplier.getRandomColor();
index = new Random().nextInt(100);

Ball ball = new Ball(color, index);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider changing the Ball constructor to accept a Color type instead of a String for the color parameter. This will improve type safety and align with the use of the Color enum.

return ball;
}
}
Loading