Here are the instructions and then the code in a sec.
**Please Resubmit the revised and updated Code (GamerService) with Comments added
explaining please. RIGHT***** JAVA. HOWEVER IF YOU NEED TO CHANGE THE
ENTIRE THING IT IS FINE. I AM DESPERATE.
Part II. Java Application: Use the code you submitted in Project One Milestone to continue
developing the game application in this project. Be sure to correct errors and incorporate
feedback before submitting Project One.
Please note: The starter code for this project was provided in the Project One Milestone. If you
have not completed the Project One Milestone, you will not be penalized in this assignment, but
you will have additional steps to complete to ensure you meet all the components of Project One.
Review the class files provided 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 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.
Be sure your code demonstrates industry standard best practices to enhance the readability of
your code, including appropriate naming conventions and in-line comments that describe the
functionality.
Here is my answer that I need a little improvement on its code since I am having trouble making
it right.
Here I am attaching code for these Files:
Entity.java
Game.java
Team.
Here are the instructions and then the code in a sec. Please R.pdf
1. Here are the instructions and then the code in a sec.
**Please Resubmit the revised and updated Code (GamerService) with Comments added
explaining please. RIGHT***** JAVA. HOWEVER IF YOU NEED TO CHANGE THE
ENTIRE THING IT IS FINE. I AM DESPERATE.
Part II. Java Application: Use the code you submitted in Project One Milestone to continue
developing the game application in this project. Be sure to correct errors and incorporate
feedback before submitting Project One.
Please note: The starter code for this project was provided in the Project One Milestone. If you
have not completed the Project One Milestone, you will not be penalized in this assignment, but
you will have additional steps to complete to ensure you meet all the components of Project One.
Review the class files provided 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 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
2. and test the game application to ensure it is functioning properly.
Be sure your code demonstrates industry standard best practices to enhance the readability of
your code, including appropriate naming conventions and in-line comments that describe the
functionality.
Here is my answer that I need a little improvement on its code since I am having trouble making
it right.
Here I am attaching code for these Files:
Entity.java
Game.java
Team.java
Player.java
GameService.java
ProgramDriver.java
SingletonTester.java
Source Code for Entity.java
package com.gamingroom;
public abstract class Entity implements Comparable {
private long id;
private String name;
/*
* Constructor with an identifier and name
*/
public Entity(long id, String name) {
this.id = id;
this.name = name;
}
/*
* @return id
*/
3. public long getId() {
return id;
}
/*
* @return name
*/
public String getName() {
return name;
}
@Override
public String toString() {
return "Entity [id=" + id + ", name=" + name + "]";
}
@Override
public int compareTo(Entity other) {
return Long.compare(this.id, other.id);
}
}
Source Code for Game.Java
package com.gamingroom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Game extends Entity {
private static List teams = new ArrayList<>();
/*
* Constructor with an identifier and name
4. */
public Game(long id, String name) {
super(id, name);
}
public Team addTeam(String name) {
Iterator teamIterator = teams.iterator();
while (teamIterator.hasNext()) {
Team team = teamIterator.next();
if (name.equalsIgnoreCase(team.getName())) {
return team;
}
}
Team team = new Team(teams.size(), name);
teams.add(team);
return team;
}
@Override
public String toString() {
return "Game [id=" + getId() + ", name=" + getName() + "]";
}
}
Source Code For Team Java
package com.gamingroom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Team extends Entity {
5. private static List players = new ArrayList<>();
/*
* Constructor with an identifier and name
*/
public Team(long id, String name) {
super(id, name);
}
public Player addPlayer(String name) {
Iterator playerIterator = players.iterator();
while (playerIterator.hasNext()) {
Player player = playerIterator.next();
if (name.equalsIgnoreCase(player.getName())) {
return player;
}
}
Player player = new Player(players.size(), name);
players.add(player);
return player;
}
@Override
public String toString() {
return "Team [id=" + getId() + ", name=" + getName() + "]";
}
}
Source Code for PLAYER JAVA
package com.gamingroom;
public class Player extends Entity {
/*
* Constructor with an identifier and name
6. */
public Player(long id, String name) {
super(id, name);
}
@Override
public String toString() {
return "Player [id=" + getId() + ", name=" + getName() + "]";
}
}
Source Code for Game Service
package com.gamingroom;
import java.util.ArrayList;
import java.util.List;
/**
* A singleton service for the game engine
*
* @author Bryan Molina******
*/
public class GameService {
/**
* A list of the active games
*/
private static List games = new ArrayList();
/*
* Holds the next game identifier
*/
private static long nextGameId = 1;
7. private static long nextTeamId = 1;
private static long nextPlayerId = 1;
// Creating a local instance of this class. Since our constructor is private,
// we know that we will only have this one instance of this object making it a singleton.
private static GameService instance = new GameService();
// it is normal for a singleton to have a private constructor so that we don't make additional
instances outside the class.
private GameService() {
}
//Public accessor for our instance will allow outside classes to access objects in this Singleton
Class
public static GameService getInstance() {
return instance;
}
//This is the new line for the assignment ** Bryan Molina
/**
* 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(int i = 0; i < getGameCount(); ++i ) {
if (name.equalsIgnoreCase(games.get(i).getName())) {
game = games.get(i);
}
}
8. if (game == null) {
game = new Game(nextGameId++, name);
games.add(game);
}
return game;
}
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;
// FIXME: Use iterator to look for existing game with same id
// if found, simply assign that instance to the local variable
for (int i = 0; i < getGameCount(); ++i) {
if (games.get(i).getId() == id) {
game = games.get(i);
}
}
return game;
}
9. /**
* 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;
// FIXME: Use iterator to look for existing game with same name
// if found, simply assign that instance to the local variable
for(int i = 0; i< getGameCount(); ++i ) {
if(name.equalsIgnoreCase(games.get(i).getName())) {
game = games.get(i);
}
}
Source Code for Program Driver Java
package com.gamingroom;
/**
* Application start-up program
*
* @author Bryan Molina ***
*/
public class ProgramDriver {
/**
* The one-and-only main() method
*
* @param args command line arguments
*/
public static void main(String[] args) {
10. // FIXME: obtain reference to the singleton instance
GameService service = GameService.getInstance();
System.out.println("nAbout 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();
}
}
return game;
}
/**
* Returns the number of games currently active
*
* @return the number of games currently active
*/
public int getGameCount() {
return games.size();
}
public long getNextPlayerId() {
return nextPlayerId++;
}
public long getNextTeamId() {
return nextTeamId++;
}
}
11. Source Code for SingletonTester JAVA
package com.gamingroom;
/**
* A class to test a singleton's behavior
*
* @author Bryan Molina *****
*/
public class SingletonTester {
public void testSingleton() {
System.out.println("nAbout to test the singleton...");
// FIXME: obtain local reference to the singleton instance
GameService service = GameService.getInstance();
// a simple for loop to print the games
for (int i = 0; i < service.getGameCount(); i++) {
System.out.println(service.getGame(i));
}
}
}
The expectation I would need is this
ProgramDriver (1) [Java Application] CiProgram FilesJavaljdk-19 binljavaw.exe (Mar 18,
2023, 9:32:50 PM - 9:32:52 PM) [pid: 44352] About to test initializing game data... Game [id=1,
name=Game #1] Game [ id =2, name = Game #2] About to test the singleton... Game [ id =1,
name=Game #1] Game [ id =2, name = Game #2]
fun: New Game Service created. About to test initializing game data... Game [id=1, name=Game
#1] Game [id=2, name=Game #2] About to test the singleton... Game Service already
instantiated. Game [id=1, name=Game #1] Game [id=2, name=Game #2] BUILD