Skip to content
Open

2 #2013

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
5 changes: 4 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,9 @@

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();
for (int i = 0; i < 3; i++) {

Choose a reason for hiding this comment

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

This violates checklist item #9: "All magic numbers in your code should be constants." Declare a constant like private static final int BALL_COUNT = 3; in the Application class and use i < BALL_COUNT in the loop instead of the literal 3.

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

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

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

public String getColor() {
return color;
}

public int getNumber() {
return number;
}

@Override
public String toString() {
return "Color is " + color + " and number is " + number;
}
}
14 changes: 13 additions & 1 deletion src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package core.basesyntax;

import java.util.Random;

Choose a reason for hiding this comment

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

This file has a redundant empty line after the package declaration/imports. This violates checklist item #1: "Please don't add redundant empty lines to your code (e.g., after class declaration or method signature)." Remove the unnecessary blank line here.

public class ColorSupplier {
public enum Colors {
RED,
YELLOW,
WHITE,
BLACK,
BLUE
}

Choose a reason for hiding this comment

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

There is a redundant empty line after the enum closing }. This violates checklist item #1: "Please don't add redundant empty lines to your code (e.g., after class declaration or method signature)." Remove the blank line to keep the file compact.

private final Random random = new Random();

Choose a reason for hiding this comment

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

There is a redundant empty line after the random field declaration. This violates checklist item #1: "Please don't add redundant empty lines to your code (e.g., after class declaration or method signature)." Removing this blank line will satisfy the checklist requirement.

public String getRandomColor() {
return null;
return Colors.values()[random.nextInt(Colors.values().length)].name();
}
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/Lottery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax;

import java.util.Random;

public class Lottery {
private static final int MAX_NUMBER = 100;
private final Random random = new Random();
private final ColorSupplier colorSupplier = new ColorSupplier();

public Ball getRandomBall() {
int number = random.nextInt(MAX_NUMBER) + 1; // 1..100
String color = colorSupplier.getRandomColor();
return new Ball(color, number);
Comment on lines +11 to +13

Choose a reason for hiding this comment

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

This creates temporary local variables (number and color) even though you can return the constructed Ball directly. This violates checklist item #2: "Don't create temporary variables if you can directly return value from the method". Consider returning new Ball(colorSupplier.getRandomColor(), random.nextInt(MAX_NUMBER) + 1); to avoid unnecessary locals.

}
}