I have made a new object called Game:
public class Game {
private String gamenaam;
private String bungeenaam;
private int poort;
private int minplayers;
private int maxplayers;
private static GameState gamestate = GameState.Ingame;
public Game(String naam) {
this.gamenaam = naam;
setAlles();
}
public String getGameNaam() {
return this.gamenaam;
}
public String getBungeeNaam() {
return bungeenaam;
}
public int getPoort() {
return poort;
}
public int getMinPlayers() {
return minplayers;
}
public int getMaxPlayers() {
return maxplayers;
}
public GameState getCurrentState() {
//System.out.print(gamenaam + ":" + MySQL.getGameState(getGameNaam()) + ":" + gamestate);
return gamestate;
}
public void setCurrecntState(GameState state) {
gamestate = state;
}
private void setAlles() {
bungeenaam = MySQL.getBungeeNaam(this.gamenaam);
poort = MySQL.getPoort(this.gamenaam);
minplayers = MySQL.getMinPlayer(this.gamenaam);
maxplayers = MySQL.getMaxPlayer(this.gamenaam);
//gamestate = MySQL.getGameState(this.gamenaam);
}
}
I store everything in an public static HashMap<Location, Game> gameSigns = new HashMap<Location, Game>(); map
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("SCGameHost"), new Runnable() {
#Override
public void run() {
for(Map.Entry<Location, Game> entry: Main.gameSigns.entrySet()){
Game game = entry.getValue();
if(game.getGameNaam().equalsIgnoreCase("Heks")) {
System.out.print(game.getGameNaam()+" is changed to FINISHED");
game.setCurrecntState(GameState.Finished);
}else {
game.setCurrecntState(GameState.Maintenance);
}
}
}
}, 10 *20L, 10 *20L);
I have 2 things in the gameSigns HashMap Heks and Snowball.
When I change Heks to GameState.Finished snowball is also being changed.
It is because the variable gamestate is static, so it is shared between all the instances of the Game class. Remove the word static if you want separate values for different instances.
Related
I have the following problem. Five classes are interacting with each other. Two of theme are doing fine. But with the creating of an Object of one class (Ticket) in my main class Event (getting user input from another class (UserInput), an processing this in the costructor) i have now problem to display the results.
Main class Event with main methode
import java.util.ArrayList;
public class Event {
private static String artistName;
private static int artistSalary;
private Language language;
private static ArrayList<String> trackList;
private InputReader inputReader;
private Ticket ticket;
private int amountOfTicketCategories;
private static Object[] ticketList;
private static int index;
public Event() {
}
public Event(Artist artist, Ticket ticket) {
artistName = artist.getArtistName();
artistSalary = artist.getArtistSalary();
trackList = artist.getArrayList();
for (index = 0; index < amountOfTicketCategories; index++) {
ticketList[index] = ticket.getTicket();
ticketList[index].toString();
}
}
public void chooseWhatToDefine() {
language = new Language();
language.whatToSpecify();
}
public void setTicketPrice(String ticketCategory, int ticketPrice) {
}
public void displayArtist(String artistName, int artistSalary) {
language = new Language();
language.displayArtistAndSalary(artistName, artistSalary);
}
public void displayTracklist(ArrayList<String> trackList) {
language = new Language();
language.displayTrackList(trackList);
}
public void displayTickets(Object[] ticketList) {
language = new Language();
language.displayTicket(ticketList);
}
public static void main(String[] args) {
Event event1 = new Event(new Artist(), new Ticket());
event1.displayArtist(artistName, artistSalary);
event1.displayTracklist(trackList);
event1.displayTickets(ticketList);
}
}
Ticket class with constructor that initalize the class with the user input comming from the InputReader class, and creates an object of Strings and Integers.
import java.util.Arrays;
public class Ticket {
private static String ticketCategory;
private static int ticketAmount;
private static int ticketPrice;
private InputReader inputReader;
private int amountOfTicketCategories;
private int index;
private Ticket[] ticketList;
public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {
}
public Ticket() {
inputReader = new InputReader();
inputReader.specifyTicketCategories();
ticketList = new Ticket[amountOfTicketCategories];
for (index = 0; index < amountOfTicketCategories; index++) {
inputReader.specifyTicket(ticketCategory, ticketAmount, ticketPrice);
ticketList[index] = new Ticket(ticketCategory, ticketAmount, ticketPrice);
}
}
public String toString() {
return("TicketCategory: " + ticketCategory + "Amount of Tickets: " + ticketAmount + "Ticket Price: " +ticketPrice);
}
public Object getTicket() {
return ticketList[index];
}
public int getAmountOfTicketCategories() {
amountOfTicketCategories = inputReader.specifyTicketCategories();
return amountOfTicketCategories;
}
}
InptReader class that processes the user input:
import java.util.ArrayList;
import java.util.Scanner;
public class InputReader {
private Scanner sc;
private Language language;
private ArrayList <String> tracks;
public InputReader() {
tracks = new ArrayList<String>();
language = new Language();
sc = new Scanner(System.in);
}
public int specifyTicketCategories() {
language.defineAmountOfTicketCategories();
return sc.nextInt();
}
public void specifyTicket(String ticketCategory, int ticketAmount, int ticketPrice) {
language.specifyTicketCategory();
ticketCategory = sc.next();
language.specifyTicketAmount();
ticketAmount = sc.nextInt();
language.specifyTicketPrice();
ticketPrice = sc.nextInt();
}
public int amountOfTickets() {
return sc.nextInt();
}
public int ticketPrice() {
return sc.nextInt();
}
public String readName() {
language.specifyArtist();
return sc.nextLine();
}
public int readInteger() {
language.specifyArtistSalary();
return sc.nextInt();
}
public void addTitle() {
int anzahlSongs = 3;
int index = 0;
while (index < anzahlSongs) {
language.specifyTrackList();
tracks.add(sc.nextLine());
index++;
}
}
public ArrayList<String> getArray() {
return tracks;
}
}
Language class that consists of the language statements
import java.util.ArrayList;
public class Language {
public Language () {
}
public void whatToSpecify() {
System.out.println("What would you like to specify fist? For Artist press 1, for Ticket press 2");
}
public void specifyArtist() {
System.out.println("Who is the artist? ");
}
public void specifyArtistSalary() {
System.out.println("How much does the artist earn? ");
}
public void displayTicket(Object[] ticketList) {
System.out.println("Ticketlist: " + ticketList);
}
public void displayArtistAndSalary(String artistName, int artistSalary) {
System.out.println("Artist: " + artistName + " " + "Salary: " + artistSalary);
}
public void displayTrackList(ArrayList<String> trackList) {
System.out.println("Tracklist: " + trackList);
}
public void specifyTicketCategory() {
System.out.println("What is the ticket category? ");
}
public void specifyTicketAmount() {
System.out.println("What ist the amount of tickets? ");
}
public void specifyTicketPrice() {
System.out.println("What is the price for your ticket?");
}
public void specifyTrackList() {
System.out.println("Add title: ");
}
public void defineAmountOfTicketCategories() {
System.out.println("How many ticket categories you like to set up?");
}
public void line() {
System.out.println("***********************************");
}
}
Artist class that that has creates an instance of an artist in the main class (same idea as for ticket) but with other variables and parameters.
import java.util.ArrayList;
public class Artist {
private int artistSalary;
private String artistName;
private InputReader inputReader;
ArrayList <String> trackList;
public Artist() {
inputReader = new InputReader();
artistName = inputReader.readName();
artistSalary = inputReader.readInteger();
inputReader.addTitle();
trackList = inputReader.getArray();
trackList.remove(2);
}
public String getArtistName() {
return artistName;
}
public int getArtistSalary() {
return artistSalary;
}
public ArrayList<String> getArrayList(){
return trackList;
}
}
Output in the console:
Add title:
Hello
Add title:
Hello
How many ticket categories you like to set up?
5
Artist: David Salary: 5000
Tracklist: [, Hello]
Ticketlist: null
First of all, in the Ticket class's constructor, you use the other constructor (The one with the 3 arguments), which has an empty body.
ticketList[index] = new Ticket(ticketCategory, ticketAmount, ticketPrice);
public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {
//this is empty
}
That means you're creating an object with.. nothing in it (null variables).
Try this:
public Ticket(String ticketCategory,int ticketAmount,int ticketPrice) {
this.ticketCategory = ticketCategory;
this.ticketAmount = ticketAmount;
this.ticketPrice = ticketPrice;
}
Then, your getTicket method is wrong. You never define the "index" integer in your Ticket class.
public Object getTicket() {
return ticketList[index];
}
Where "index" is undefined.
The ticketList should not be in the Ticket class => each time you create a Ticket instance, it will probably not be the same as the previous one.
I came across FlyWeight Pattern described in the link. In the example provided , I believe only 2 implementations of player objects will be created. Wouldn't the weapons variable be overridden each time a player object is created?
The code as posted on Geeks For Geeks constructs only two mutable objects.
As can be expected, each time PlayerFactory returns a player, it overrides the weapon of one of the two objects.
This can be demonstrated easily:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class CounterStrike
{
private static String[] playerType = {"Terrorist", "CounterTerrorist"};
private static String[] weapons = {"AK-47", "Maverick", "Gut Knife", "Desert Eagle"};
public static void main(String args[])
{
List<Player> players = new ArrayList<>();
System.out.println("------- Construction of Players ----------------");
for (int i = 0; i < 10; i++)
{
Player p = PlayerFactory.getPlayer(getRandPlayerType());
p.assignWeapon(getRandWeapon());
p.mission();
players.add(p);
}
System.out.println("------- Printout all players ----------------");
for(Player p : players) {
p.mission();
}
}
public static String getRandPlayerType()
{
// Will return 0 or 1
int randInt = new Random().nextInt(playerType.length);
return playerType[randInt];
}
public static String getRandWeapon()
{
// Will return an integer between 0 inclusive and 5 exclusive
int randInt = new Random().nextInt(weapons.length);
return weapons[randInt];
}
}
interface Player
{
void assignWeapon(String weapon);
void mission();
}
class Terrorist implements Player
{
private final String TASK;
private String weapon;
public Terrorist()
{
TASK = "PLANT A BOMB";
}
#Override
public void assignWeapon(String weapon)
{
this.weapon = weapon;
}
#Override
public void mission()
{
System.out.println("Terrorist with weapon " + weapon + "|" + " Task is " + TASK);
}
}
class CounterTerrorist implements Player
{
private final String TASK;
private String weapon;
public CounterTerrorist()
{
TASK = "DIFFUSE BOMB";
}
#Override
public void assignWeapon(String weapon)
{
this.weapon = weapon;
}
#Override
public void mission()
{
System.out.println("Counter Terrorist with weapon "+ weapon + "|" + " Task is " + TASK);
}
}
class PlayerFactory
{
private static HashMap <String, Player> hm = new HashMap<>();
public static Player getPlayer(String type)
{
Player p = null;
if (hm.containsKey(type)) {
p = hm.get(type);
} else
{
switch(type)
{
case "Terrorist":
p = new Terrorist();
break;
case "CounterTerrorist":
p = new CounterTerrorist();
break;
default :
System.out.println("Unreachable code!");
}
hm.put(type, p);
}
return p;
}
}
The output shows that all Terrorist have the last applied weapon (Maverick) and all CT an AK-47:
Edit: I did not explore this design pattern, but I must say I am not impressed by the code posted in Geeks For Geeks.
From what I see in other examples the extrinsic attributes need to be managed by the factory.
In this case I guess the factory should have a map for terrorist and a map for CT where the key is the weapon:
public class CounterStrike
{
//better use enums
private static String[] playerType = {"Terrorist", "CounterTerrorist"};
private static String[] weapons = {"AK-47", "Maverick", "Gut Knife", "Desert Eagle"};
public static void main(String args[])
{
List<Player> players = new ArrayList<>();
System.out.println("------- Construction of Players ----------------");
for (int i = 0; i < 10; i++)
{
String type = getRandPlayerType();
Player p = type.equals(playerType[0]) ? PlayerFactory.getTerrorist(getRandWeapon()) :
PlayerFactory.getCoubterTerrorist(getRandWeapon()) ;
p.mission();
players.add(p);
}
System.out.println("------- Printout all players ----------------");
for(Player p : players) {
p.mission();
}
}
public static String getRandPlayerType()
{
// Will return 0 or 1
int randInt = new Random().nextInt(playerType.length);
return playerType[randInt];
}
public static String getRandWeapon()
{
// Will return an integer between 0 inclusive and 5 exclusive
int randInt = new Random().nextInt(weapons.length);
return weapons[randInt];
}
}
class PlayerFactory
{
private static HashMap <String, Player> terrorists = new HashMap<>();
private static HashMap <String, Player> cTerrorists = new HashMap<>();
public static Player getTerrorist(String weapon)
{
Player p = null;
if (terrorists.containsKey(weapon)) {
p = terrorists.get(weapon);
} else{
p = new Terrorist(weapon);
}
terrorists.put(weapon, p);
return p;
}
public static Player getCoubterTerrorist(String weapon)
{
Player p = null;
if (cTerrorists.containsKey(weapon)) {
p = cTerrorists.get(weapon);
} else{
p = new CounterTerrorist(weapon);
}
cTerrorists.put(weapon, p);
return p;
}
}
interface Player
{
void mission();
}
class Terrorist implements Player
{
private final String TASK;
private String weapon;
public Terrorist(String weapon)
{
this.weapon = weapon;
TASK = "PLANT A BOMB";
}
#Override
public void mission()
{
System.out.println("Terrorist with weapon " + weapon + "|" + " Task is " + TASK);
}
}
class CounterTerrorist implements Player
{
private final String TASK;
private String weapon;
public CounterTerrorist(String weapon)
{
this.weapon = weapon;
TASK = "DIFFUSE BOMB";
}
#Override
public void mission()
{
System.out.println("Counter Terrorist with weapon "+ weapon + "|" + " Task is " + TASK);
}
}
We can improve the implementation by using enums, and making the player type an intrinsic attribute, rather than a class:
public class CounterStrike
{
public enum PlayerType{
TERRORIST("PLANT A BOMB"), COUNTER_TERRORIST("DIFFUSE BOMB");
private final String task;
PlayerType(String task){
this.task = task;
}
String getTask(){ return task; }
}
public enum Weapon {
AK47("AK-47"), MAVERICK("Maverick"), GUT_KNIFE("Gut Knife"), DESERT_EAGLE("Desert Eagle");
private final String name;
Weapon(String name) { this.name = name; }
String getName(){ return name; }
#Override
public String toString() { return name; }
}
public static void main(String args[])
{
List<Player> players = new ArrayList<>();
System.out.println("------- Construction of Players ----------------");
for (int i = 0; i < 10; i++)
{
Player p = PlayerFactory.getPlayer(getRandPlayerType(), getRandWeapon()) ;
players.add(p);
System.out.println("Created: "+ p);
}
System.out.println("------- Printout all players ----------------");
for(Player p : players) { System.out.println(p); }
}
public static PlayerType getRandPlayerType()
{
int randInt = new Random().nextInt(PlayerType.values().length);
return PlayerType.values()[randInt];
}
public static Weapon getRandWeapon()
{
int randInt = new Random().nextInt(Weapon.values().length);
return Weapon.values()[randInt];
}
}
class PlayerFactory
{
private static HashMap <Weapon, Player> terrorists = new HashMap<>();
private static HashMap <Weapon, Player> cTerrorists = new HashMap<>();
static Player getPlayer(PlayerType type, Weapon weapon) {
return type == PlayerType.TERRORIST ? getTerrorist(weapon) : getCounterTerrorist(weapon);
}
private static Player getTerrorist(Weapon weapon)
{
Player p = null;
if (terrorists.containsKey(weapon)) {
p = terrorists.get(weapon);
} else{
p = new Player(PlayerType.TERRORIST, weapon);
}
terrorists.put(weapon, p);
return p;
}
private static Player getCounterTerrorist(Weapon weapon)
{
Player p = null;
if (cTerrorists.containsKey(weapon)) {
p = cTerrorists.get(weapon);
} else{
p = new Player(PlayerType.COUNTER_TERRORIST, weapon);
}
cTerrorists.put(weapon, p);
return p;
}
}
class Player
{
private final Weapon weapon;
private final PlayerType type;
Player(PlayerType type, Weapon weapon) {
this.type = type;
this.weapon = weapon;
}
Weapon getWeapon() { return weapon; }
PlayerType getType() {return type; }
String getTask() { return type.getTask(); }
#Override
public String toString() {
StringBuilder sb = new StringBuilder(type == PlayerType.TERRORIST ? "Terrorist" : "Counter Terrorist" );
sb.append(" armed with ").append(weapon).append(". Task: ").append(type.getTask());
return sb.toString();
}
}
In the example class diagram given by the Geeks for Geeks author
If I understand this correctly, the game creates one instance of Terrorist, one instance of CounterTerrorist, and n instances of Player created by the PlayerFactory.
The code reflects the diagram. Terrorist and CounterTerrorist implement the Player interface.
Each Player instance created by the PlayerFactory uses the information from the Terrorist or CounterTerrorist instance, depending on which side the player is on. Since there's a Player instance for each player (Usually 10 in CounterStrike, 5 on each side), there's no confusion as to which player is which.
The CounterStrike class manages the Map created by the PlayerFactory.
This simple real-world example minimizes the duplication that would occur if there were just n Player instances. Each Player instance would have to hold the information for both a terrorist and a counter-terrorist.
By creating one instance of Terrorist, one instance of CounterTerrorist, and sharing those instances with the Player instances, the total amount of storage for the game fields is reduced.
The game code is probably easier to debug and manage as well.
The Java code can be found on Geeks For Geeks.
I have a Java project that requires me to have two classes called: Pokemon.java and Move.java so I can add new Pokemon and their moves. I’ve already written all of the methods that were required for the project but I’m having issues storing and modifying the moves, specifically the forgetMove method in the Pokemon class.
Here’s the code for Pokemon.java:
public class Pokemon
{
// Private constants
private static final int MAX_HEALTH = 100;
private static final int MAX_MOVES = 4;
private String name;
private int health;
private Move move;
// Write your Pokemon class here
public Pokemon(String theName, int theHealth)
{
name = theName;
if(theHealth <= MAX_HEALTH)
{
health = theHealth;
}
}
public String getName()
{
return name;
}
public int getHealth()
{
return health;
}
public boolean hasFainted()
{
if(health <= 0)
{
return true;
}
else
{
return false;
}
}
public boolean canLearnMoreMoves()
{
if(Move.getNumOfMoves() < 4)
{
return true;
}
else
{
return false;
}
}
public boolean learnMove(Move move)
{
if(canLearnMoreMoves())
{
this.move = move;
return true;
}
else
{
return false;
}
}
public void forgetMove(Move other)
{
if(Move.equals(other))
{
move -= other;
}
}
public String toString()
{
return name + " (Health: " + health + " / " + MAX_HEALTH + ")";
}
}
and here is the code for Move.java:
public class Move
{
// Copy over your Move class into here
private static final int MAX_DAMAGE = 25;
private static String name;
private static int damage;
public static int numMoves;
public Move(String theName, int theDamage)
{
name = theName;
if(theDamage <= MAX_DAMAGE)
{
damage = theDamage;
}
numMoves++;
}
public static String getName()
{
return name;
}
public static int getDamage()
{
return damage;
}
public static int getNumOfMoves()
{
return numMoves;
}
public String toString()
{
return name + " (" + damage + " damage)";
}
// Add an equals method so we can compare Moves against each other
public static boolean equals(Move other)
{
if(name.equals(other.getName()))
{
return true;
}
else
{
return false;
}
}
}
Here is the code for PokemonTester.java:
public class PokemonTester extends ConsoleProgram
{
public void run()
{
Pokemon p1 = new Pokemon("Charrizard", 100);
Move m1 = new Move("Flamethrower", 90);
System.out.println(p1);
System.out.println(m1);
}
}
This seems like it might be homework so I won't give you a full implementation.
If you are simply filling out the methods required for the Pokemon and Move class, I would start by reconsidering the way you are storing moves.
The getNumOfMoves provides a hint that your Pokemon class should store more than one move, a common way to do this is with arrays or lists.
If you have stored your moves in a list, the forgetMove function may look like this:
public void forgetMove(Move other){
moves.remove(other);
}
I am making a multiplayer adventure game for my networking class. I have a client and a server, the server is multithreaded, and kicks off a new thread whenever it gets a new client connected. I have an array list that keeps track of the players to make sure that a new player isn't added. For some reason, when a new client connects, it takes the place of the old one as well as filling a new spot. Here is my code for this part
public class ClientHandler implements Runnable{
private AsynchronousSocketChannel clientChannel;
private static String command[];
private static String name;
private static GameCharacter character;
public ClientHandler(AsynchronousSocketChannel clientChannel)
{
this.clientChannel = clientChannel;
}
public void run(){
try{
System.out.println("Client Handler started for " + this.clientChannel);
System.out.println("Messages from Client: ");
while ((clientChannel != null) && clientChannel.isOpen()) {
ByteBuffer buffer = ByteBuffer.allocate(32);
Future result = clientChannel.read(buffer);
//Wait until buffer is ready
result.get();
buffer.flip();
String message = new String(buffer.array()).trim();
if(message == null || message.equals(""))
{
break;
}
System.out.println(message);
clientChannel.write(buffer);
try {
//Add the character to the routing table and the character table
if (message.contains("connect")) {
System.out.println("I'm here too?");
command = message.split(" ");
name = command[1];
AdventureServer.userInfo.put(name, this);
//Check to see if this game character exists
GameCharacter test;
boolean exists = false;
for(int i=0; i < AdventureServer.characters.size(); i++)
{
test = AdventureServer.characters.get(i);
System.out.println(test.getName());
System.out.println(this.name);
if(this.name.equals(test.getName()))
{
System.out.println("already Here");
exists = true;
}
}
if (exists == true)
{
//This person has connected to the server before
}
else {
//Create a game character
System.out.println("didn't exist before");
character = new GameCharacter(this.name, World.getRow(), World.getCol());
AdventureServer.characters.add(AdventureServer.userInfo.size() - 1, character);
System.out.println(AdventureServer.characters.get(0).getName() + " " +AdventureServer.characters.get(1).getName());
}
}
I understand that the print lines at the bottom will throw an error for the first client that connects, but that is not part of the issue.
And here is the declaration of the server
public class AdventureServer {
public static Map<String, ClientHandler> userInfo = new HashMap<>();
public static World world;
public static List<GameCharacter> characters = Collections.synchronizedList(new ArrayList<>());
public static void main(String args[]) {
//Create the games map that all of the users will exist on
world = new World(args[0]);
System.out.println("Asynchronous Chat Server Started");
try {
AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open();
InetSocketAddress hostAddress = new InetSocketAddress("192.168.1.7", 5000);
serverChannel.bind(hostAddress);
while (true)
{
System.out.println("Waiting for client to connect");
Future acceptResult = serverChannel.accept();
AsynchronousSocketChannel clientChannel = (AsynchronousSocketChannel) acceptResult.get();
new Thread (new ClientHandler(clientChannel)).start();
}
} catch (Exception e) {
System.out.println("error interrupted");
e.printStackTrace();
System.exit(0);
}
}
}
Here is my constructor for game characters
public class GameCharacter {
public static int xpos;
public static int ypos;
private static String name;
private static int rowSize;
private static int columnSize;
static List<String> inventory = new ArrayList<>();
//Constructor
GameCharacter(String n, int rSize, int cSize)
{
xpos = 0;
ypos = 0;
name = n;
rowSize = rSize;
columnSize = cSize;
}
GameCharacter()
{
xpos = 0;
ypos = 0;
name = "billybob";
rowSize = 10;
columnSize = 10;
}
You can try:
public static volatile List<GameCharacter> characters = Collections.synchronizedList(new ArrayList<>());
Update:
The problem is that you are using a non synchronized HashMap userInfo.
Change that line from:
AdventureServer.characters.add(AdventureServer.userInfo.size() - 1, character);
To:
AdventureServer.characters.add(character);
Or make your HashMap Synchronized:
public static Map<String, ClientHandler> userInfo = Collections.synchronizedMap(new HashMap<>());
All those static declarations are making problems, you should remove them. In general you should avoid using static.
ClientHandler:
private static String command[];
private static String name;
private static GameCharacter character;
GameCharacter:
public static int xpos;
public static int ypos;
private static String name;
private static int rowSize;
private static int columnSize;
static List<String> inventory = new ArrayList<>();
Just a side note, this way your Class is more like Java code should look like:
import java.util.ArrayList;
import java.util.List;
public class GameCharacter {
private int xpos;
private int ypos;
private String name;
private int rowSize;
private int columnSize;
private List<String> inventory = new ArrayList<>();
// Constructor
GameCharacter(String n, int rSize, int cSize) {
this.xpos = 0;
this.ypos = 0;
this.name = n;
this.rowSize = rSize;
this.columnSize = cSize;
}
GameCharacter() {
this.xpos = 0;
this.ypos = 0;
this.name = "billybob";
this.rowSize = 10;
this.columnSize = 10;
}
public int getXpos() {
return xpos;
}
public void setXpos(int xpos) {
this.xpos = xpos;
}
public int getYpos() {
return ypos;
}
public void setYpos(int ypos) {
this.ypos = ypos;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRowSize() {
return rowSize;
}
public void setRowSize(int rowSize) {
this.rowSize = rowSize;
}
public int getColumnSize() {
return columnSize;
}
public void setColumnSize(int columnSize) {
this.columnSize = columnSize;
}
public List<String> getInventory() {
return inventory;
}
public void setInventory(List<String> inventory) {
this.inventory = inventory;
}
}
As a matter of readability, testability, and style, I would also recommend you not directly access data structures belonging to another class. Instead of
Adventureserver.characters.add(blah blah)
I would recommend making characters a private field of Adventureserver, and then creating a method to add or remove characters from it. In fact, I would be inclined not to make characters static -- there is no real advantage, and you might at some point want more than one Adventureserver running.
Sort of like so:
public class AdventureServer {
<...>
private List<GameCharacter> characters = Collections.synchronizedList(new ArrayList<>);
<...>
public void addCharacter(GameCharacter char) {
<... error checking ...>
characters.add(char);
}
public void removeCharacter(GameCharacter char) {
<... implementation ... >
}
public boolean isCharacterHere(GameCharacter char) {
}
public List<GameCharacter> getCharacters() {
<... you could either return characters here, or a copy of it,
depending upon how paranoid you want to be >
I am working in an android application I want to sort a List of Objects with an Object Property. I have sorted it successfully but when I sort it all the List with that object changes the value to same as the sorted value
Please look into ma code :
SortedSet<Caseload> removeDuplicateClientName = new TreeSet<Caseload>(
new Comparator<Caseload>() {
#Override
public int compare(Caseload caseload0, Caseload caseload1) {
return caseload0.ClientName.compareTo(caseload1.ClientName);
}
});
// Getting the list of values from web service
mLISTCaseloadsHeads = parsedXML.getCaseLoadValues("get_Caseload_ClientServiceGroupID", param);
List<Caseload> newBackUp=mLISTCaseloadsHeads ;
Iterator<Caseload> iterator = mCaseloadsHeads.iterator();
while (iterator.hasNext()) {
removeDuplicateClientName.add(iterator.next());
}
mCaseloadsHeads.clear();
mCaseloadsHeads.addAll(removeDuplicateClientName);
The List newBackUp also changes the value to the same as sorted List
Caseload class:
public class Caseload implements Comparable<Caseload> {
public int BusClientLogID;
public int ClientID;
public int ClientStatus;
public int ClientServiceGroup_ClientSiteTherapyID;
public String ClientName;
public String TimeArrive;
public String TimeDepart;
public String SignOutTime;
public String SignInTime;
public String ServiceCompletedCount;
public Boolean ShowFooter = false;
public int getBusClientLogID() {
return BusClientLogID;
}
public void setBusClientLogID(int busClientLogID) {
BusClientLogID = busClientLogID;
}
public int getClientID() {
return ClientID;
}
public void setClientID(int clientID) {
ClientID = clientID;
}
public int getClientStatus() {
return ClientStatus;
}
public void setClientStatus(int clientStatus) {
ClientStatus = clientStatus;
}
public int getClientServiceGroup_ClientSiteTherapyID() {
return ClientServiceGroup_ClientSiteTherapyID;
}
public void setClientServiceGroup_ClientSiteTherapyID(
int clientServiceGroup_ClientSiteTherapyID) {
ClientServiceGroup_ClientSiteTherapyID = clientServiceGroup_ClientSiteTherapyID;
}
public String getClientName() {
return ClientName;
}
public void setClientName(String clientName) {
ClientName = clientName;
}
public String getTimeArrive() {
return TimeArrive;
}
public void setTimeArrive(String timeArrive) {
TimeArrive = timeArrive;
}
public String getTimeDepart() {
return TimeDepart;
}
public void setTimeDepart(String timeDepart) {
TimeDepart = timeDepart;
}
public String getSignOutTime() {
return SignOutTime;
}
public void setSignOutTime(String signOutTime) {
SignOutTime = signOutTime;
}
public String getSignInTime() {
return SignInTime;
}
public void setSignInTime(String signInTime) {
SignInTime = signInTime;
}
public String getServiceCompletedCount() {
return ServiceCompletedCount;
}
public void setServiceCompletedCount(String serviceCompletedCount) {
ServiceCompletedCount = serviceCompletedCount;
}
#Override
public int compareTo(Caseload compareCaseload) {
int busClientLogID = ((Caseload) compareCaseload).getBusClientLogID();
return busClientLogID - this.BusClientLogID;
}
}
Please give me a solution.
I doubt the return statement associated with your compare function in the comparator.
You should go by this approach to get the right ordering :
#Override
public int compare(YourClass lhs, YourClass rhs) {
YourClass p1 = (YourClass) lhs;
YourClass p2 = (YourClass) rhs;
int first = p1.ClientName; //use your getter if you want
int second = p2.ClientName;
if (second < first) {
return 1;
}
else if (second > first) {
return -1;
}
else {
return 0;
}
}
If you go by this approach I guess you will get the required ordering after sort.
Edit:
Now I have got the issue, you are using a reference of the original list in newBackup and its not a new list that is why this is happening, use this and you are good to go.
List<Caseload> newBackUp=new ArrayList<Caseload>(mLISTCaseloadsHeads);