Question
Need help finishing this Java program (UML, instructions, and already completed portions below) Review the class files provided (below) and complete the following tasks to
Need help finishing this Java program (UML, instructions, and already completed portions below)
Review the class files provided (below) and complete the following tasks to create a functional game application that meets your clients requirements. You will submit the completed game application code for review.
Begin by reviewing the base Entity class. It contains the attributes id and name, implying that all entities in the application will have an identifier and name.
Software Design Patterns: Review the GameService class. Notice the static variables holding the next identifier to be assigned for game id, team id, and player id.
Referring back to the Project One Milestone, be sure that you use the singleton pattern to adapt an ordinary class, so only one instance of the GameService class can exist in memory at any given time. This can be accomplished by creating unique identifiers for each instance of a game, team, or player.
Your client has requested that the game and team names be unique to allow users to check whether a name is in use when choosing a team name. Referring back to the Project One Milestone, be sure that you use the iterator pattern to complete the addGame() and getGame() methods.
Create a base class called Entity. The Entity class must hold the common attributes and behaviors (as shown in the UML diagram provided in the Supporting Materials section below).
Refactor the Game class to inherit from this new Entity class.
Complete the code for the Player and Team classes. Each class must derive from the Entity class, as demonstrated in the UML diagram.
Every team and player must have a unique name by searching for the supplied name prior to adding the new instance. Use the iterator pattern in the addTeam() and addPlayer() methods.
Functionality and Best Practices
Once you are finished coding, use the main() method provided in the ProgramDriver class to run and test the game application to ensure it is functioning properly.
=====Game.java=====
package com.gamingroom;
/**
* A simple class to hold information about a game
*
*
* Notice the overloaded constructor that requires
* an id and name to be passed when creating.
* Also note that no mutators (setters) defined so
* these values cannot be changed once a game is
* created.
*
*
*
*
*/
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 + "]";
}
}
=====GameService.java=====
package com.gamingroom;
import java.util.ArrayList; import java.util.List;
/** * A singleton 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;
private static GameService instance = new GameService(); // Private constructor so that we can be confident there are no instances outside this class, so we just have the one.
// a private constructor is typical for a singleton as we don't want the possibilty of creating any additional instances outside this class. // This ensures there is only the one, therefore making it a singleton. private void GameService() { } // Public accessor for our instance! 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 found, simply return the existing instance 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;
for (Game currentGame : games) { // iterate through the current game instances if (currentGame.getId() == id) { // checking if the found id matches the current game id game = currentGame; } } // if found, simply assign that instance to the local variable
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;
for (Game currentGame : games) { // iterate through the array of games if (currentGame.getName().equals(name)) { // if the current game name is equal to the found name game = currentGame; } } // if found, simply assign that instance to the local variable
return game; }
/** * Returns the number of games currently active * * @return the number of games currently active */ public int getGameCount() { return games.size(); } }
=====Player.java=====
package com.gamingroom;
/**
* A simple class to hold information about a player
*
* Notice the overloaded constructor that requires
* an id and name to be passed when creating.
* Also note that no mutators (setters) defined so
* these values cannot be changed once a player is
* created.
*
*/
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;
}
public Player(long id, String name, Team team) { // Team team, or team Team? UML says team Team, but that doesn't make syntax sense as far as i can tell.
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 + "]";
}
}
=====ProgramDriver.java=====
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 instance of the singleton 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();
}
}
=====SingletonTester.java=====
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...");
// obtain local reference to the singleton instance
GameService service = GameService.getInstance();
// a simple for loop to print the games
for (int i = 0; i
System.out.println(service.getGame(i));
}
}
}
=====Team.java=====
package com.gamingroom;
import java.util.List;
/**
* A simple class to hold information about a team
*
* Notice the overloaded constructor that requires
* an id and name to be passed when creating.
* Also note that no mutators (setters) defined so
* these values cannot be changed once a team is
* created.
*
*/
public class Team {
long id;
String name;
List
/*
* 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