Skip to content
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: 6 additions & 1 deletion src/main/java/core/basesyntax/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

public class Application {
public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console
Lottery ball1 = new Lottery();
System.out.println(ball1.getRandomBall().toString());
Lottery ball2 = new Lottery();
System.out.println(ball2.getRandomBall().toString());
Lottery ball3 = new Lottery();
System.out.println(ball3.getRandomBall().toString());
Comment on lines +5 to +10
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Use for loop for creating several objects and output of the same data

}
}
35 changes: 35 additions & 0 deletions src/main/java/core/basesyntax/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package core.basesyntax;

public class Ball {
String ballColor;
int ballNumber;
Comment on lines +4 to +5
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

use the private access modifier for internal variables


public Ball() {
}

public Ball(String color, int number) {
this.ballColor = color;
this.ballNumber = number;
}

public String getBallColor() {
return this.ballColor;
}

public void setBallColor(String ballColor) {
this.ballColor = ballColor;
}

public int getBallNumber() {
return this.ballNumber;
}

public void setBallNumber(int ballNumber) {
this.ballNumber = ballNumber;
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

add annotation @Override

public String toString() {
String ball = "ball";
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

avoid variables you don't use

return "Ball with number: " + ballNumber + " and color: " + ballColor;
}
}
9 changes: 7 additions & 2 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
public String getRandomColor() {
return null;


public static String getRandomColor() {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Static methods are in general a bad practice. Let’s better create an instance of a class which method you want to call

int index = new Random().nextInt(Colors.values().length);
return Colors.values()[index].toString();
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

It’s better to use standard method of enum name() that will be returning always String representation of concrete enum constant.

}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax;

public enum Colors {
black,
white,
red,
blue
Comment on lines +4 to +7
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

As a rule, constants and Enums have names in upper case.

}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/Lottery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax;

import java.util.Random;

public class Lottery extends ColorSupplier {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

there is no benefit from this "extends"

public Ball getRandomBall() {

Ball ball = new Ball();
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

you can add parameters to the constructor and avoid calling setters

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Don’t create temporary variables if you can directly return value from the method


ball.setBallColor(ColorSupplier.getRandomColor());

ball.setBallNumber(new Random().nextInt(100));
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

All magic numbers in your code should be constants.


Comment on lines +7 to +13
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Please don’t add redundant empty lines to your code.

return ball;
}
}