Skip to content

Conversation

xli1iax
Copy link

@xli1iax xli1iax commented Jul 30, 2024

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Great job on defining the Color enum according to Java conventions! 🎉 While there are some suggestions for improvements, none of them are critical issues that would require a change request. Keep up the good work, and consider the feedback to enhance your code for the future. 👍 Remember, using static fields for shared resources and creating instances for non-static methods can help make your code more efficient and object-oriented. Happy coding!


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

Choose a reason for hiding this comment

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

The color field should be of the enum type Color, not String, to enforce the use of predefined color values.

return number;
}

@Override

Choose a reason for hiding this comment

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

The toString() method should return a string that represents the entire Ball object, not just the color. Consider including both the number and the color in the returned string.

Comment on lines 6 to 8
for (int i = 0; i < 3; i++) {
System.out.println(Lottery.getRandomBall());
}

Choose a reason for hiding this comment

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

Don't use static methods in your solution. Instead of calling a static method on the Lottery class, you should create an instance of the Lottery class and call the method on that instance.


public class Application {
public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console

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. There's no need for an empty line after the main method signature.

Comment on lines 8 to 13
public static String getRandomColor() {
int index = new Random().nextInt(Color.values().length);
Ball ball = new Ball();
ball.setColor(Color.values()[index].toString());
return ball.getColor();
}

Choose a reason for hiding this comment

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

Avoid using static methods as they are generally considered bad practice in object-oriented programming. You should create an instance of ColorSupplier in the class where you want to use it.

return null;
public static String getRandomColor() {
int index = new Random().nextInt(Color.values().length);
Ball ball = new Ball();

Choose a reason for hiding this comment

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

It's unnecessary to create a new instance of Ball in this method. You can directly return the color as a string using Color.values()[index].name().

Choose a reason for hiding this comment

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

good advice

Comment on lines 11 to 12
ball.setColor(Color.values()[index].toString());
return ball.getColor();

Choose a reason for hiding this comment

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

Instead of using toString(), use name() for getting the String representation of enum constants to avoid issues if toString() is overridden in the enum.

Choose a reason for hiding this comment

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

not fixed

Comment on lines 6 to 12
public class Lottery {
public static String getRandomBall() {
Ball ball = new Ball();
ball.setColor(ColorSupplier.getRandomColor());
ball.setNumber(new Random().nextInt(100));
return "Ball color is " + ball.getColor() + " and the number is " + ball.getNumber();
}

Choose a reason for hiding this comment

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

Don't use static methods in your solution. Instead, create an instance of the class whose method you want to call. This will make your code more flexible and object-oriented.

Choose a reason for hiding this comment

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

nice hint

import java.util.Random;

public class Lottery {
public static String getRandomBall() {

Choose a reason for hiding this comment

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

The method name getRandomBall suggests that it should return a Ball object, but it currently returns a String. Consider changing the return type to Ball and moving the string representation to a toString method in the Ball class.

public static String getRandomBall() {
Ball ball = new Ball();
ball.setColor(ColorSupplier.getRandomColor());
ball.setNumber(new Random().nextInt(100));

Choose a reason for hiding this comment

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

Instead of creating a new instance of Random every time the method is called, consider defining it as a private static final field in the class. This will be more efficient as you will reuse the same Random instance.

Choose a reason for hiding this comment

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

not fixed

public String getRandomColor() {
return null;
public static String getRandomColor() {
int index = new Random().nextInt(Color.values().length);

Choose a reason for hiding this comment

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

not fixed

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

Choose a reason for hiding this comment

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

Suggested change
public static String getRandomColor() {
public String getRandomColor() {

Copy link

Choose a reason for hiding this comment

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

not fixed - we should avoid static methods, this will become evident in Spring section

Comment on lines 11 to 12
ball.setColor(Color.values()[index].toString());
return ball.getColor();

Choose a reason for hiding this comment

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

not fixed

return null;
public static String getRandomColor() {
int index = new Random().nextInt(Color.values().length);
Ball ball = new Ball();

Choose a reason for hiding this comment

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

good advice

Comment on lines 9 to 10
ball.setColor(ColorSupplier.getRandomColor());
ball.setNumber(new Random().nextInt(100));

Choose a reason for hiding this comment

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

try to create ball using constructor

public static String getRandomBall() {
Ball ball = new Ball();
ball.setColor(ColorSupplier.getRandomColor());
ball.setNumber(new Random().nextInt(100));

Choose a reason for hiding this comment

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

not fixed

Comment on lines 6 to 12
public class Lottery {
public static String getRandomBall() {
Ball ball = new Ball();
ball.setColor(ColorSupplier.getRandomColor());
ball.setNumber(new Random().nextInt(100));
return "Ball color is " + ball.getColor() + " and the number is " + ball.getNumber();
}

Choose a reason for hiding this comment

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

nice hint

Comment on lines 7 to 14
private final int randomNum = new Random().nextInt(100);
private final ColorSupplier colorSupplier = new ColorSupplier();
private final Ball ball = new Ball(randomNum, colorSupplier.getRandomColor());

public Ball getRandomBall() {
return ball;
}
}
Copy link

Choose a reason for hiding this comment

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

100 should be a constant
Random should be a private field
random num should a local variable be generated in getRandomBall method. Same for Ball. Right now you only create one unique ball per each lottery instance - it doesn't fulfil our requirements, we want lottery to generate a new Ball every time the method is called

public String getRandomColor() {
return null;
public static String getRandomColor() {
int index = new Random().nextInt(Color.values().length);
Copy link

Choose a reason for hiding this comment

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

same for this Random, it should be a field

for (int i = 0; i < 3; i++) {
Lottery lottery = new Lottery();
Ball ball = new Ball(lottery.getRandomBall());
System.out.println(ball.toString());
Copy link

Choose a reason for hiding this comment

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

you can remove .toString() from println, it's called under the hood anyway

Copy link
Author

@xli1iax xli1iax Aug 1, 2024

Choose a reason for hiding this comment

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

yes, but it make up a sentence at once, is it bad?
I thought that I should use .toString() somewhere, or I just need to override it?

Copy link

Choose a reason for hiding this comment

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

Sorry I didn't really get your question :) You should override toString in the Ball.java class, but when you put something in system out println, .toString of this object will be called

@xli1iax xli1iax requested a review from okuzan August 1, 2024 15:40
import java.util.Random;

public class Lottery {
public static final int border = 100;
Copy link

Choose a reason for hiding this comment

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

why public?
BORDER - user uppercase for constants

public class ColorSupplier {
public String getRandomColor() {
return null;
public static String getRandomColor() {
Copy link

Choose a reason for hiding this comment

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

not fixed - we should avoid static methods, this will become evident in Spring section

@xli1iax xli1iax requested a review from okuzan August 2, 2024 18:25
Copy link

@okuzan okuzan left a comment

Choose a reason for hiding this comment

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

put ball package inside of core/basesyntax, everything should be under one root package

import java.util.Random;

public class Lottery {
static final int BORDER = 100;
Copy link

Choose a reason for hiding this comment

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

Suggested change
static final int BORDER = 100;
private static final int BORDER = 100;

public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console
for (int i = 0; i < 3; i++) {
Lottery lottery = new Lottery();
Copy link

Choose a reason for hiding this comment

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

Don't create new lottery instance in a loop - do it outside of the loop

@xli1iax xli1iax requested a review from okuzan August 3, 2024 15:02
// 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.

3 is a magic number, create constant

Comment on lines 9 to 10
Ball ball = new Ball(lottery.getRandomBall());
System.out.println(ball);

Choose a reason for hiding this comment

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

Suggested change
Ball ball = new Ball(lottery.getRandomBall());
System.out.println(ball);
System.out.println(lottery.getRandomBall()));

this.number = number;
}

public Ball(Ball ball) {

Choose a reason for hiding this comment

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

remove

@xli1iax xli1iax requested a review from Elena-Bruyako August 4, 2024 14:06
public static void main(String[] args) {
// create three balls using class Lottery and print information about them in console
Lottery lottery = new Lottery();
int amountBalls = 3;

Choose a reason for hiding this comment

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

this is not constant
make it constant

@xli1iax xli1iax requested a review from Elena-Bruyako August 4, 2024 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants