#Is this assignment legal or is it preferred to make a constructor for it?

1 messages · Page 1 of 1 (latest)

atomic geyser
#
public class Game {
    private final UserInterface ui = new UserInterface();
    private final Calculator calc = new Calculator(ui);

    public void start() {
        System.out.println("The game has started, have fun!");
        int userChoice = ui.getUserMenuOption();

        System.out.println("User selected " + userChoice);
        playOneRound();
    }

    public void playOneRound() {
        calc.playOneRound();
        System.out.println("Playing one round");
    }
}

What I'm trying: Make the calculator object instance use the same user userinterface instance (DI).

distant hamletBOT
# atomic geyser ```Java public class Game { private final UserInterface ui = new UserInterfa...

Detected code, here are some useful tools:

Formatted code
public class Game {
  private final UserInterface ui = new UserInterface();
  private final Calculator calc = new Calculator(ui);
  public void start() {
    System.out.println("The game has started, have fun!");
    int userChoice = ui.getUserMenuOption();
    System.out.println("User selected " + userChoice);
    playOneRound();
  }
  public void playOneRound() {
    calc.playOneRound();
    System.out.println("Playing one round");
  }
}
#

<@&987246399047479336> please have a look, thanks.

distant hamletBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

raw elbow
#

Yes its fine for now, begs the question in this example do you really need calculator? Calculator is essentially serving as an extended service layer to userInterface. If this is the option you want to go with. I'd suggest using an interface->serviceimpl to abstract this functionality.

atomic geyser
#

I'll be honest I have no idea 😄 the reason why I'm making a simple cli application is to get used to object oriented design. Things like "which class is responsible for printing messages", "which is responsible for starting the game". I'll share my full code for review when I'm done with the basics.

#

What I'm making is a game where the user sees an operation (e.g. an addition) then has to type the answer, so I have a Calculator class that's responsible for the logic, a UserInterface class that's responsible for printing the messages, and a Game class responsible for the game flow (round start, history of results, score, etc.)

#

And to think that last week I completed a Udemy course about Spring Boot and was thinking to myself "I don't need to learn Java without Spring Boot this is fine" 🤣

#

I'd suggest using an interface->serviceimpl to abstract this functionality. that's an awesome suggestion actually! That would also force me to think which stuff a class should do before I even start implementing them 😭

distant hamletBOT
#

Closed the thread.