Question
Can you create a UML Class Diagram for the code below?: package com.gamingroom; /** * A class to hold information about a game */ public
Can you create a UML Class Diagram for the code below?:
package com.gamingroom;
/** * A class to hold information about a game */ public class Game { long id; String name;
/** * Hide the default constructor to prevent creating empty instances. */ private Game() { }
/** * Constructor with an identifier and name */ public Game(long id, String name) { this(); this.id = id; this.name = name; }
/** * @return the id */ public long getId() { return id; }
/** * @return the name */ public String getName() { return name; }
@Override public String toString() {
return "Game [id=" + id + ", name=" + name + "]"; }
}
package com.gamingroom;
import java.util.ArrayList; import java.util.List;
/** * A service for the game engine */ public class GameService {
/** * A list of the active games */ private static List
/* * Holds the next game identifier */ private static long nextGameId = 1;
/* * Create a local instance of this class. Since the constructor is private, * you know that it will only ever have this one instance of this object * making it a singleton. */ private static GameService instance = new GameService();
/* * A private constructor. This private constructor exists so that the program * doesn't instantiate any additional instances outside of this class */ private void GameService(){ }
/* * A public accessor for the instance. This allows outside classes to * access objects in this singleton class. */ public static GameService getInstance(){ return instance; }
/** * Construct a new game instance * * @param name the unique name of the game * @return the game instance (new or existing) */ public Game addGame(String name) {
// a local game instance Game game = null;
// if Game with name already exists, simply return the existing instance // without adding anything to the games datastructure. for (Game currentGame : games) { if (currentGame.getName().equals(name)) { return currentGame; } }
// if not found, make a new game instance and add to list of games if (game == null) { game = new Game(nextGameId++, name); games.add(game); }
// return the new/existing game instance to the caller return game; }
/** * Returns the game instance at the specified index. *
* Scope is package/local for testing purposes. *
* @param index index position in the list to return * @return requested game instance */ Game getGame(int index) { return games.get(index); }/** * Returns the game instance with the specified id. * * @param id unique identifier of game to search for * @return requested game instance */ public Game getGame(long id) {
// a local game instance Game game = null;
// Parse the current list of games. If one already exists // with this id, return that Game instance. for (Game currentGame : games) { if (currentGame.getId() == id){ game = currentGame; } }
return game; }
/** * Returns the game instance with the specified name. * * @param name unique name of game to search for * @return requested game instance */ public Game getGame(String name) {
// a local game instance Game game = null;
// Parse the current list of games. If one already exists // with this name, return that Game instance. for (Game currentGame : games) { if (currentGame.getName().equals(name)){ game = currentGame; } }
return game; }
/** * Returns the number of games currently active * * @return the number of games currently active */ public int getGameCount() { return games.size(); } }
package com.gamingroom;
/** * A class to hold information about a player */ public class Player { long id; String name;
/* * Constructor with an identifier and name */ public Player(long id, String name) { this.id = id; this.name = name; }
/** * @return the id */ public long getId() { return id; }
/** * @return the name */ public String getName() { return name; }
@Override public String toString() { return "Player [id=" + id + ", name=" + name + "]"; } }
package com.gamingroom;
/** * Application start-up program */ public class ProgramDriver {
/** * The one-and-only main() method * * @param args command line arguments */ public static void main(String[] args) {
// Get reference to the instance of the GameService class. GameService service = GameService.getInstance();
System.out.println(" About to test initializing game data...");
// initialize with some game data Game game1 = service.addGame("Game #1"); System.out.println(game1); Game game2 = service.addGame("Game #2"); System.out.println(game2);
// use another class to prove there is only one instance SingletonTester tester = new SingletonTester(); tester.testSingleton(); } }
package com.gamingroom;
/** * A class to test a singleton's behavior */ public class SingletonTester {
public void testSingleton() { System.out.println(" About to test the singleton..."); // FIXME: obtain local reference to the singleton instance GameService service = null; // replace null with ??? // a simple for loop to print the games for (int i = 0; i < service.getGameCount(); i++) { System.out.println(service.getGame(i)); }
} }
package com.gamingroom;
/** * A class to hold information about a team */ public class Team { long id; String name;
/* * Constructor with an identifier and name */ public Team(long id, String name) { this.id = id; this.name = name; }
/** * @return the id */ public long getId() { return id; }
/** * @return the name */ public String getName() { return name; }
@Override public String toString() { return "Team [id=" + id + ", name=" + name + "]"; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started