Instructions
Requirements and Specifications
Source Code
ENTITY
package com.gamingroom;
public abstract class Entity {
private long id;
private String name;
/**
* Hide the default constructor to prevent creating empty instances.
*/
private Entity() {}
public Entity(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 "Entity [id=" + getId() + ", name=" + getName() + "]";
}
}
GAME
package com.gamingroom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* A simple class to hold information about a game
*
* <p>
* 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.
* </p>
*
* @author coce@snhu.edu
*
*/
public class Game extends Entity {
private final List<Team> teams;
/**
* Constructor with an identifier and name
*/
public Game(long id, String name) {
super(id, name);
teams = new ArrayList<>();
}
/**
* Construct a new team instance and adds it to the team list
*
* @param name the unique name of the team
* @return the team instance (new or existing)
*/
public Team addTeam(String name) {
// a local team instance
Team team = null;
// if found, simply return the existing instance
/*
* The following line creates the Iterator and uses the Iterator Pattern
*/
Iterator<Team> iterator = teams.iterator();
while(iterator.hasNext())
{
Team t = iterator.next();
if(t.getName().compareTo(name) == 0) // same name
{
team = t;
break;
}
}
/*
* End of iterator pattern
*/
// if not found, make a new team instance and add to list of teams
if (team == null) {
team = new Team(GameService.getInstance().getNextTeamId(), name);
teams.add(team);
}
// return the new/existing team instance to the caller
return team;
}
@Override
public String toString() {
return "Game [id=" + getId() + ", name=" + getName() + "]";
}
}
GAME SERVICE
package com.gamingroom;
import java.util.ArrayList;
import java.util.Iterator;
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 final List<Game> games = new ArrayList<>();
/*
* Holds the next game identifier
*/
private long nextGameId = 1;
/*
* Holds the next player identifier
*/
private long nextPlayerId = 1;
/*
* Holds the next team identifier
*/
private long nextTeamId = 1;
/*
* Define required fields and function for the singleton pattern
*/
private static final GameService service = new GameService();
private GameService() {
}
public static GameService getInstance() {
return service;
}
/**
* Construct a new game instance and adds it to the game list
*
* @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
/*
* The following line creates the Iterator and uses the Iterator Pattern
*/
Iterator<Game> iterator = games.iterator();
while(iterator.hasNext())
{
Game g = iterator.next();
if(g.getName().compareTo(name) == 0) // same name
{
game = g;
break;
}
}
/*
* End of iterator pattern
*/
// 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 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;
/*
* The following line creates the Iterator and uses the Iterator Pattern
*/
Iterator<Game> iterator = games.iterator();
while(iterator.hasNext())
{
Game g = iterator.next();
if(g.getId() == id) // same name
{
game = g;
break;
}
}
/*
* End of iterator pattern
*/
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;
/*
* The following line creates the Iterator and uses the Iterator Pattern
*/
Iterator<Game> iterator = games.iterator();
while(iterator.hasNext())
{
Game g = iterator.next();
if(g.getName().compareTo(name) == 0) // same name
{
game = g;
break;
}
}
/*
* End of iterator pattern
*/
return game;
}
/**
* Returns the number of games currently active
*
* @return the number of games currently active
*/
public int getGameCount() {
return games.size();
}
/**
* Returns next available identifier for new player instance
* and increments it
*
* @return next available identifier for new player instance
*/
public long getNextPlayerId() {
return nextPlayerId++;
}
/**
* Returns next available identifier for new team instance
* and increments it
*
* @return next available identifier for new team instance
*/
public long getNextTeamId() {
return nextTeamId++;
}
}
PLAYER
package com.gamingroom;
import java.util.Iterator;
/**
* A simple class to hold information about a player
* <p>
* 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.
* </p>
* @author coce@snhu.edu
*
*/
public class Player extends Entity {
/*
* Constructor with an identifier and name
*/
public Player(long id, String name) {
super(id, name);
}
@Override
public String toString() {
return "Player [id=" + getId() + ", name=" + getName() + "]";
}
}
PROGRAM DRIVER
package com.gamingroom;
/**
* Application start-up program
*
* @author coce@snhu.edu
*/
public class ProgramDriver {
/**
* The one-and-only main() method
*
* @param args command line arguments
*/
public static void main(String[] args) {
GameService service = GameService.getInstance(); // replace null with ???
System.out.println("\nAbout to test initializing game data...");
// initialize with some game data
// Add the games to the GameService
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();
}
}