Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

Review the UML diagram provided for a game software application. A text version is available: Text Version for UML diagram Word Document. You may notice that it is incomplete - it is missing attributes and methods, and only includes a small portion of a complete game application. This is quite common! As a software developer, typically you will be given pieces of the puzzle or tasks to complete as part of a larger project and as a member of a larger team. It takes practice to focus on the information you are given to determine what you have and what steps you need to take to complete the task.

Specifically, the game application requires that only one instance of the game be able to exist in memory at any given time. This can be accomplished by creating unique identifiers for each instance of the game.

  • Complete the UML diagram to accurately represent a singleton pattern for the Game Service class. You should replace the question marks in the UML diagram with appropriate static and instance attributes and/or methods to make the ordinary class into a singleton. You may create the diagram using Lucidchart or a tool of your choice, such as draw.io or Visio. A tutorial for creating UML diagrams is available: Lucidchart Tutorial for Creating Class Diagrams PDF.
  • Code below just need help completing the UML chart:

package com.gamingroom;

import java.util.ArrayList;
import java.util.List;

/**
 * A singleton service for the game engine
 * 
 * @author coce@snhu.edu
 */
public class GameService {

    /**
     * A list of the active games
     */
    private static List<Game> games = new ArrayList<Game>();

    /*
     * Holds the next game identifier
     */
    private static long nextGameId = 1;

    // FIXME: Add missing pieces to turn this class a singleton 
    private static GameService instance = new GameService();
    
    private void GameService() {
    }
    
    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;
        
        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.
     * <p>
     * Scope is package/local for testing purposes.
     * </p>
     * @param index index position in the list to return
     * @return requested game instance
     */
    Game getGame(int index) {
        return games.get(index);
    }
    
    //Returns game instance with specific id
    public Game getGame(long id) {

        // a local game instance
        Game game = null;

        // if found, simply assign that instance to the local variable
        // If a game with name exists it returns the existing instance without adding to data structure
        for (Game currentGame: games) {
            if (currentGame.getId() == id) {
                return currentGame;
            }
        }
        
        return game;
    }

    

    
    public Game getGame(String name) {

        // a local game instance
        Game game = null;
        //if a game already exists with this name, return 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();
    }
}

com.gamingroom
GameService
-games: List<Game>
-nextGameld: long
???
???
???
+ addGame(name: String): Game
+ getGame(index: int): Game
+ getGameCount(): int
ProgramDriver
+ main()
<< uses >>
Game
- id: long
-name: String
+ getId(): long
+ getName(): String
+ toString(): String
Singleton Tester
+ testSingleton()
expand button
Transcribed Image Text:com.gamingroom GameService -games: List<Game> -nextGameld: long ??? ??? ??? + addGame(name: String): Game + getGame(index: int): Game + getGameCount(): int ProgramDriver + main() << uses >> Game - id: long -name: String + getId(): long + getName(): String + toString(): String Singleton Tester + testSingleton()
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education