Skip to content

Consolidate Console

Do This First...

Be sure to pull the latest code from the repository before starting.

Start With Passing Tests

Make sure all tests pass before getting started on this lab.

Goal

Move all console-specific behavior to the new ConsoleGame class.

A. Re-package

To see more clearly the roles of the classes, move the classes to one of the following packages:

  • com.jitterted.ebp.blackjack.domain -- domain classes containing core entities with behavior.
  • com.jitterted.ebp.blackjack.adapter.in.console -- classes responsible for dealing with console I/O

Make sure to move the Test classes into their respective, matching packages as well.

B. Move Console I/O

Your goal is to purify the Game class by moving all code relating to Console I/O into the new ConsoleGame class.

  • For example, in determineOutcome, change the System.out.println to return a String and adjust the calling code as needed.
  • You will likely need to expose Game information via Query methods. Use the guidelines below when creating them.

    Query Method Guidelines

    When exposing data with new Query Methods, make sure they follow these guidelines:

    • Snapshot: The returned data is a point-in-time view of the data.

    • Integrity: Prevent consumers of the data from changing the object's internal state. It helps if the return value doesn't mislead clients into thinking it is providing access to internal state, therefore avoid returning mutable objects.

    • Safe: Calling the Query should not change the observable state of the object. (This includes the idempotent property, calling the Query multiple times should be no different than calling it once.)

Use automated refactorings as much as you can, such as Make Static (Refactor menu > Make Static...) and Move Members (shortcut key is F6).

Run Tests...

Don't forget to run all tests as you refactor.

C. The "Tidy Up" Phase

Once you've moved all Console-related code out of the Game class, don't forget to tidy up the code in both the Game class as well as the ConsoleGame class.

Things to think about:

  • What does the public API of Game need to be?

  • What code is now no longer necessary in Game?

  • Turn any static methods in ConsoleGame into instance methods.

D. Play the Game!

From a console/terminal, run the game to make sure it still works by doing a Maven "package":

./mvnw clean package
mvnw clean package

If the package process succeeds, you can run the resulting JAR file this way:

java -jar target/blackjack-1.0.1.jar
java -jar target\blackjack-1.0.1.jar

You are done!