Java Monopoly-ish Game - java

I'm working on a Monopoly based game with properties, money, cards, etc. I recently ran into a problem when working on the chance card aspect of the game...
I have an array of Strings that say things, ex,"Tax refund; collect $25" or "You lose money due to stocks; -$100". Each card does something different, not all of them just deal with money. My question: how can i store these cards to each hold a string with its description but also any action that is involved. For example, card1 has a String and an int value (-25), but on the other hand another card, card2 has a String, an int value(+10) and a property. Sorry if the question is really vague but I don't know how else to describe it.
Just to clarify:
A card could contain simply the description and a money value. While another card might contain a description, money value and move certain spaces.
Thanks to everyone who gave out awesome ideas so quickly!

If your range of actions is very limited (say, 2 or 3 actions involving money, move squares, etc) then I might use the following class:
class Card {
// It would be a good practice to not make the following fields
// public and use getters/setters instead but I've made them this
// way just for illustration purposes
public String text;
public String action;
public int value;
Card(String text, String action, int value) {
this.text = text;
this.action = action;
this.value = value;
}
}
This way (as already pointed out by some other answers), you can use an array of Cards instead of array of Strings. You can then have text in one field, the action in a separate field, and the value associated with that action in a third field. For example, you could have the following cards:
Card lose25 = new Card("You lose $25", "money", -25);
Card move5squares = new Card("Move 5 squares ahead!", "move", 5);
When you're 'processing' the cards, you can do so in the following manner:
...
if (card.action.equals("money") {
// Update user's money with card.value
} else if (card.action.equals("move") {
// Update user's position with card.value
} else if (card.action.equals("...") {
// and so on...
}
...
EDIT:
If the cards can hold more than one action, you can use a HashMap to store actions for that card:
class Card {
public String text;
public HashMap<String, Integer> actions;
Card(String text) {
this.text = text;
actions = new HashMap<String, Integer>();
}
addAction(String action, int value) {
actions.put(action, value);
}
}
A HashMap is a collection that can store key-value pairs. So for a card that has 2 actions, you can use the above code as:
Card aCard = new Card("Lose $25 and move back 3 spaces!");
aCard.addAction("money", -25);
aCard.addAction("move", -3);
Now, when you're actually processing the cards, you need to check the HashMap for all actions stored in this card. One way to iterate through the HashMap is to do the following:
Card processCard = ...;
for (Map.Entry<String, Integer> entry : processCard.actions.entrySet()) {
// This loop will get each 'action' and 'value' that you added to
// the HashSet for this card and process it.
String action = entry.getKey();
int value = entry.getValue();
// Add the earlier 'card processing' code here...
if (action.equals("money") {
// Update user's money with value
} else if (action.equals("move") {
// Update user's position with value
} else if (action.equals("...") {
// and so on...
}
}

Create a class for Card, use the Command Pattern design pattern so each card can have a different type of action if need be. So it will hold a String field, text, an int field, value, and another field that is for an interface that represents your action.
The key field will be the "action" field that holds a reference to an interface type, say called CardAction:
interface CardAction {
execute();
}
Or you could use use a Runnable or a Future if you want to use a ready made interface. This way you can inject objects of this class with different actions for different types of cards. This will prevent you from having to create multiple different Card classes.

This is a solution that allows each card to have several actions.
The ActionType is an interface, as it only has several correct values.
All the fields are made final as once the cards have been created, they shouldn't be allowed to change.
The getActions() method returns a copy of the array as we don't want the game to be able to modify the actions of the card after creation.
enum ActionType {
MONEY, MOVE, ...
}
class Action {
private final String description;
private final ActionType type;
private final int value;
public Action(String desc, ActionType type, int value) {
this.description = desc;
this.type = type;
this.value = value;
}
public String getDescription() {
return description;
}
public ActionType getType() {
return type;
}
public int getValue() {
return value;
}
}
class Card {
private final Action[] actions; // If you want to be able to modify the list of actions after creation, use a non-final List<>, otherwise an array
public Card(Action... actions) {
this.actions = actions;
}
public Action[] getActions() {
Action[] copy = new Action[actions.length];
System.arraycopy(actions, 0, copy, 0, actions.length);
}
}

Make a Card Class, it should contain the description (String) and a value (int) (+10,-25, 0 ect...). You then would have a array of Cards instead of array of strings.

Have a Card class with description and value.
Card
{
String description;
int value;
Card(String desc, int value)
{
this.description = desc;
this.value = value;
}
getters..
setters...
}
Create objects like
Card c = new Card("You may loose 25$...", -25);
Note that, these cards with int value can accept values between -2,147,483,648 to 2,147,483,647. If you want some higher values, you can change int to long or something else.

Make a Card class, with a string called text or something, and a method called onAction() or something. Then, make lots of other classes - one for each card - extending the Card class. Give the string the text you want and in the method you can then put exactly what you want to happen. You can create a class called Deck containing instances of all of these Cards and an array/list, and a draw() method or whatever you've done already. On drawing a Card from the Deck, get it to print that Card's text and call its onAction() method.
Hope this helps.

Related

What's the best way to change attributes of objects stored in an ArrayList or HashMap?

I have to do a little exercise (homework, like a friendlist) in Java, and i'm a little stuck on one of the tasks that i have to implement in my program.
The exercise is about storing some friend-objects with a variety of attributes in a container-class and implementing some methods in the container-class for various tasks on the friend-objects.
The overall exercise is not a problem at all, but i'm quite unconvinced that my solution is the way to go. I hope you can give me some tips here.
The method that is left over, should be something like a "updateFriend" method, with which you can set the value of a given attribute to a new value, straight from the container-class.
I've already set up my friend-class with a handfull of attributes (e.g. prename, lastname, date of birth, adress, and so on) an getters/setters for all of them. I've also implemented the container-class (as an ArrayList), but i can't seem to find an elegant way to implement this specific method. My updateFriend()-method right now takes three parameters.
1.The specific id of the friend-object
2.The name of the attribute that i want to change
3.The new value of the attribute
It uses an enum to check if the entered attribute is an existing attribute and if yes, the method searches the ArrayList for the object that contains that attribute and should overwrite the existing value. It gets a little bulky, as i have implemented a switch on the enum, that calls the fitting setter-method for each attribute of the friend, if the type in attribute exists at all.
So basically the friend-class looks like this:
public class Friend {
private static int friendCount = 1;
private String firstname;
private String lastname;
private LocalDate dateOfBirth;
private String phonenumber;
private String mobilenumber;
private String eMail;
private Adress home;
private int friendID;
//Getters & Setters
...
}
The method that gives me problems in the container-class looks something like this at the moment:
public void updateFriend(int id, String toChange, String newValue)
{
for(Attribute a : attribute.values())
{
if(String.valueOf(a).equalsIgnoreCase(toChange))
{
for(Friend f : friends)
{
int counter = 1;
if(f.getID() == id)
{
switch(a)
{
case FIRSTNAME:
{
f.setPreName(neuerWert);
break;
}
//a case for each attribute
}
I'm quite certain that my take on the given method is messy, slow, and cumbersome. What would be an elegant way of solving this?
Excuse my wording and thanks in advance, greets.
I would suggest 3 performance improvements.
Use HashMap instead of List with key as id. Since, id will be unique, it will take O(1) time to get the relevant object for modification instead of spending O(n) time on List iteration.
You can change the type of toChange parameter from String to enum. This will avoid enum to String conversion and then comparing it.
Since, you are already doing validation of the attribute to be modified and you must be following standard java convention while naming your getters and setters, you can use reflection to call the method on the Friend object by creating the method name from attribute name like set{Attributename}.
Okay, lets start using the enum Attribute to handle all the changes (Since you already holding the attribute values)
Attribute Enum
public enum Attribute {
FIRSTNAME("fname", (friend, name) -> friend.setFirstname(String.valueOf(name))),
LASTNAME("lname", (friend, lname) -> friend.setLastname(String.valueOf(lname))),
DATEOFBIRTH("dob", (friend, dob) -> friend.setDateOfBirth((LocalDate) dob)),
PHONENUMBER("pno", (friend, pno) -> friend.setFirstname(String.valueOf(pno))),
MOBILENUMBER("mno", (friend, mno) -> friend.setFirstname(String.valueOf(mno)));
private String attributeName;
private BiConsumer<Friend, Object> attributeSetter;
public static Attribute getAttributeSetterByName(String attributeName) {
return Arrays.stream(Attribute.values())
.filter(attribute -> attribute.getAttributeName().equalsIgnoreCase(attributeName))
.findFirst()
.orElseThrow(() -> new RuntimeException(String.format("Invalid Attribute name - %s", attributeName)));
//.orElse(null);
}
//Getter, Setter & Args Constructor (Use Lombok to reduce Boiler Plate code)
}
Update Logic
public void updateFriend(int id, String toChange, String newValue) {
Attribute attribute = Attribute.getAttributeSetterByName(toChange);
for (Friend friend : friends) {
if (friend.getId() == id) {
attribute.getAttributeSetter().accept(friend, newValue);
break;
}
}
}
You can use a java.util.function.Consumer<T> object to change an object inside your container where you have all the type safety you get. Instead of having magic strings and string arguments for values, which might not be even for string fields, you can work directly on the objects type:
public void updateFriend(int id, Consumer<Friend> c) {
// find the friend object
Friend found = null;
for (Friend f: this.friends) {
if (f.getId() == id) {
found = f;
break;
}
}
if (found == null) {
throw new IllegalArgumentException("There is no friend object with the given id");
}
// use the friend object.
c.accept(found);
}
You can use this method like this:
container.updateFriend(42, f -> f.setVorName("abc"));
container.updateFriend(9, f -> f.setAddress(some_address_object));

Serialization of my GameState to JSON in libGdx is not working, how can I fix it?

I am having trouble saving my GameState to JSON.
Originally I was saving the GameState to JSON like this:
Json json = new JSON();
String save = json.prettyPrint(state);
And I was loading it as follows:
json.fromJson(GameState.class, save);
The State was not loading correctly, however, so I ran a little test. I did the following:
class Car
{
public int id;
public Color color;
public Speed speed;
public Car(int id, Color color, Speed speed)
{
this.color = color;
this.id = id;
this.speed = speed;
}
}
class Speed
{
enum ASpeed
{
FAST, MEDIUM, SLOW;
}
ASpeed speed;
public Speed()
{
speed = ASpeed.FAST;
}
}
And in create
#Override
public void create()
{
Json json = new Json();
Car car = new Car(1, Color.RED, new Speed());
String carSave = json.prettyPrint(car);
System.out.println(carSave);
}
The result I get is this:
{
id:1
color:{
r:1
a:1
}
speed:{}
}
I see why my game was not properly loading; it was not properly serializing. My GameState is pretty much a class containing 3 Arrays of data that needs to progress from game to game. I have the regular getter and setter methods as well as a couple dozen utility methods to access relevant data as follows:
//TODO: Possibly add an Array of Event instances to fire for each player when certain circumstances are met as well as a string-based flagging system that can be used in the events.
//TODO: Research threading API in libGdx and see about moving the game logic over to a seperate thread - since this class is used for game logic, consider moving this with it.
public class GameState
{
private Array<Player> players;
private Array<Planet> planets;
private Array<Fleet> fleets;
public GameState(Array<Player> players, Array<Planet> planets, Array<Fleet> fleets)
{
this.players = players;
this.planets = planets;
this.fleets = fleets;
}
public Fleet getFleet(int id)
{
if(id <= fleets.size)
{
return fleets.get(id);
}
return null;
}
public Planet getPlanet(int id)
{
if(id <= planets.size)
{
return planets.get(id);
}
return null;
}
public Player getPlayer(int id)
{
if(id <= players.size)
{
return players.get(id);
}
return null;
}
Of course, this isn't the best approach, but I only have about 100 of each field and do not plan to add much more, so this method serves my purpose fine. Each player contains an Array of Integer values used to fetch the relevant data. An example;
public class Player
{
private final int id;
//libGdx Color used by ShapeRenderer to display planets in the right political color
private Color color;
//Things that the game logic demands be accessible from each Player instance
//TODO: Move these over to the libGdx IntegerArray when you have the time - this will avoid the boxing/unboxing penalty when accessing members.
Array<Integer> allies;
Array<Integer>desiredAllies;
Array<Integer> enemies:
Array<Integer> neutrals;
Array<Integer> planets;
Array<Integer> fleets;
public Player(int id, Color color)
{
this.id = id;
this.color = color;
allies = new Array<Integer>();
planets = new Array<Integer>();
fleets = new Array<Integer>();
neutrals = new Array<Integer>();
enemies = new Array<Integer>();
}
//Getters, Setters, and many, many utility methods.
}
The Planet, Player, and Fleet classes all have similar structure, even going so far as to implement the same Interface:
public interface IndexedGameObject extends GameObject
{
int getID();
}
My game design and structure works well until it comes to serialization. I learned Json specifically to save my game, and I might have missed something. Why does this design not work when serializing? Is there a way I can make it to work? If not, what possible solutions might work aside from writing my own save format? I have little experienceing in XML, would that work?
Notes:
I am willing and able to write my own save format, but doing so in a consistent, readable-to-the-human-eye way will add a lot of overhead and maintenance and would waste time (as it is akin to writing a very small, not completely functional language and I would need to do a lot of general testing to ensure it is working properly) and energy that would be better spent developing and maintaining a more intuitive user interface. If there is no way JSON can serve my purposes, is there anything that can?
I would essentially have to rework my entire schedule (and yes, I keep a list of things I need do and when I should do them such as; Tuesday - Fix that annoying, nigh impossible to recreate button disappearing bug after school and homework).
My JSON should, in theory have a structure like this;
GameState ->
fleets ->
fleet ->
owner: integer id of the player that owns this fleet.
id: the index of this fleet in the Array
locationID: integer id of the province this fleet is located in.
originalTransports: final integer used for reinforcing.
originalFighters: final integer used for reinforcing.
originalFrigates: final integer used for reinforcing.
originalDreadnaughts: final integer used for reinforcing.
transports: number of troop transports currently in the fleet.
fighters: the number of fighters currently in fleet.
frigates: the number of frigates in this fleet.
dreadnaughts: number of dreadnaughts in the fleet.
players ->
player ->
id: int representing the index of this player in the GameState
provinces: Integer Array representing the provinces owned by this player.
allies: Integer Array representing the index of the other Players allied to this one.
enemies: Integer Array representing the indices of the other players at war with this one.
neutrals: Integer Array representing the players whom this player is neutral to
desiredAllies: Integer Array representing the players who this player desires for allies - used mainly by the ai to determine who to send alliance offers to and who to accept them from.
fleets: Integer Array representing the indices of the fleets of this player
provinces ->
province ->
id: int representing the index of this province
owner: int id of the owning player in the GameState
name: String value. Text displayed on the screen when province selected.
isUnderSiege: boolean. Read the variable name.
colonies ->
colony ->
name: String value. Displayed in the menu when the city is selected from the province info screen that pops up when the province is selected.
controller: The index of the controlling Player in the GameState.
population: int number of people who reside here. Used to calculate things such as how many ships this planet provides, or how much tax it gives to the player.
fortLevel: double representing the amount of organized military resistance this colony will give an attacker before surrendering.
growthRate: the number of births occurring in this colony every 60 ticks.
EDIT:
The players Array and the fleets work properly. When I reach the provinces Array, things just get weird;
{
planets:[
//There should be 300 provinces. Instead 300 of these
colonies: [ {},{},{},{},{},{},{} ]
]
}
And my class with relevant fields looks something like this;
public class Planet implements IndexedGameObject, Modifier
{
private int id;
private Array<Colony> colonies;
public Planet(int id, Array<Colony> colonies)
{
this.id = id;
this.colonies = colonies;
}
//Utility methods + Getters/Setters
}
class Colony
{
private int parentProvinceID;
private ColonyType type;
public Colony(int parentProvinceID, ColonyType type)
{
this.parentProvinceID = parentProvinceID;
this.type = type;
}
//Some more irrelevant methods
}
enum ColonyType
{
ADMINISTRATIVE, MILITARY, POPULATION;
}
Your problem is being caused by generic Arrays. I suppose that it is not possible to JSON to serialize for example Array<Player>.
The solution is to wrap generic arrays - take a look at this thread or to use not generic Array where it is possible (for exmaple FloatArray instead of Array).
On the other hand the structure of your logic seems to be like relational - meybe it would be more convenient to use some SQL techniques instead of keeping this in objects and serialize/deserialize? Here you have some nutshell info
I solved this by making the enum fields public static and moving them into their parent classes - I had overlooked this, as pointed out by #Barodapride and #TenFour04.
public class Planet
{
private Array<Colony> colonies;
public static class Colony
{
private ColonyType type;
private int parentProvinceID;
public static enum ColonyType
{
ADMINISTRATIVE, MILITARY, POPULATION;
}
}
}
Serializes correctly.

Implement Attribute and Skill Systems [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a problem with two solutions, which one to pick. I wonder what a right way of implementing attributes in a RPG are. Let's for the sake of it say we have three attributes. Offensive, Defensive and Speed. Every character will have those. Somewhere I need to store the name, the description and the values of the attributes. I figure it should be done in Attributes.java. Giving me the following Attribute.java
public class Attribute
{
private String name;
private String desc;
private int value;
public Attribute (String name, String desc, int value) {
this.name = name;
this.desc = desc;
this.value = value;
}
public String getName () { // for desc and value as well
return name;
}
}
Now onto the problem, were do I create and store these attributes?
Option #1, I create them here, filling the ArrayList with them
Character.java
import java.util.ArrayList;
import java.util.List;
public class Character
{
private List<Attribute> attributes;
public Character() {
attributes = new ArrayList<Attribute>();
}
public List<Attribute> getAttributes () {
return attributes;
}
}
Option #2, I create an AttributeSystem
AttributeSystem.java
public class AttributeSystem
{
private List<Attribute> attributes;
public AttributeSystem () {
attributes = new ArrayList<Attribute>();
attributes.add (new Attribute ("Offensive", "Your damage.", 5);
attributes.add (new Attribute ("Defensive", "Your defense.", 5);
attributes.add (new Attribute ("Speed", "Your speed.", 5);
}
public Attribute getAttribute(int i) {
return attributes[i];
}
}
Character.java
public class Character
{
private AttributeSystem attributes;
public Character() {
attributes = new AttributeSystem();
}
public AttributeSystem getAttributes () {
return attributes;
}
}
Conclusion
Option #2 makes logically more sense to me.
In both cases I can use a HashMap instead of an ArrayList.
Which one and why would I use?
Final Note
This does not yet effects on your character. Nor does it have the ability to add attribute points. Ignore those two factors for now.
Both options seems legit to me.
But I would declare the list of Attribute as static, as the list of available attribute for player will not change no matter what.
Then for each player, you would map each available attribute to a given value.
I agree with Pablo, but I'd like to expand it a bit. So I think this design is a bit too meta for your requirements. In that Attribute is so generic it's not that helpful, and its flexibility is getting in your way.
In design #2 a Character could never vary their attributes. They would all be 5. Maybe that's just an example, but it becomes more cumbersome to to work with when you go beyond the hard coded values.
If every character has to have those three values then why not a more straightforward design? This is also much faster and less code to access than the attribute scheme.
class Character {
int defensive, offensive, speed;
}
Now you can encapsulate modifiers like weapons, armor, etc by using getter methods like so:
class Character {
int defensive, offensive, speed;
Item weapon;
Item armor;
Item shoes;
public int getTotalDefensive() {
return armor.getDefensive() + defensive;
}
public int getTotalOffensive() {
return weapon.getOffensive() + offensive;
}
public int getTotalSpeed() {
return shoes.getSpeed() + speed;
}
public List<Attribute> getAttributes() {
// if you really need to work with a character like this then you can do that too.
List<Attribute> attributes = new ArrayList<Attribute>();
attributes.add( new Attribute( "offensive", "How much damage you can do", offensive );
attributes.add( new Attribute( "defensive", "How much damage you can sustain", defensive );
attributes.add( new Attribute( "speed", "How fast you can move", offensive );
return attributes;
}
}
Maybe you are over engineering: Assuming this is the classic RPG the chosen name for those values (attributes) should give you a hint: If you have a Character object, and it will have attributes... then I'd add those attributes to the character itself:
class Character {
int defensive,offensive,speed;
... //other attributes here, like name, race or whatever you need
}
The equipment, skills and other things that can be more variable (you can have an item or 20, you can learn one skill or 20... and their nature can be totally different between them) have more sense to be in a list or in another related object.
Besides, don't mix model and view: the description of each attribute should not be part of the Character, that kind of information is not needed by that object and it would be duplicated each time a new character is created.

how to extract an array from a list of objects in java

Hope you don't mind I'm asking a newbie question about the use of iterator for a arrayList / List.
I have a Building Class with a few data members:
public Building() {
private String buildingName;
private String buildingType;
private double width;
private double length;
private String managerName;
....
}
I've already set toString() as follow to access the name String and I'm NOT allowed to change toString to return a different data member.
public String toString() {
return buildingName;
}
I've created a bunch of building objects in the Building class by loading a building.xml file, and I've used iterator to create an array/list of buildingNames to be viewed in a swing ComboBox (but let's ignore those swing component below and focus on the just the logics):
// Create building combo box model.
buildingComboBoxModel = new DefaultComboBoxModel<Building>();
buildingsCache = new ArrayList<Building>(buildings);
Iterator<Building> i = buildingsCache.iterator();
while (i.hasNext()) buildingComboBoxModel.addElement(i.next());
// Create building list.
buildingComboBox = new JComboBoxMW<Building>(buildingComboBoxModel);
buildingComboBox.addActionListener(this);
buildingComboBox.setMaximumRowCount(10);
buildingSelectPanel.add(buildingComboBox);
How many ways do can you think of and how in putting together an list of buildingType using iterator without significant code changes?
If I need to modify some of the buildingType Strings in some situations by adding a String to it,
say in one building,
String buildingName = "Adam's Place";
String buildingType = "Greenhouse";
[post edit]
e.g. I need to change buildingType to "Greenhouse Type 1" for "Adam's Place" and change another building's buildingType to "Greenhouse Type 2"
Can it be done using iterator and how?
If not, other ways are welcome
Thanks in advance!
There is no special way for handling objects read from an iterator. If the only changes needed are just setting different values to the members of the Building class, this can be done the standard way, i.e. by adding member setter functions.
For example, to modify the buildingType at any time, a setBuildingType() method is needed:
public class Building {
private String buildingName;
private String buildingType;
private double width;
private double length;
private String managerName;
....
public void setBuildingType(String buildingType) {
this.buildingType = buildingType;
}
}
Given the above, the iterator-based code can be modified as follows:
Iterator<Building> i = buildingsCache.iterator();
while (i.hasNext()) {
Building b = i.next();
b.setBuildingType("Whatever type");
buildingComboBoxModel.addElement(b);
}

How do I add or subtract String values to an object, and set the value to "none" when it's empty?

I have a class Passengers which has member properties String name, int health, and String disease with setter and getter methods. The disease variable will initially hold null. Here's that class
public class Passengers
{
private String name;
private int health;
private String disease;
public Passengers(String _name, int _health, String _disease)
{
name = _name;
health = _health;
disease = _disease;
}
public void setHealth(int _health)
{
health = _health;
}
public void setDisease(String _disease)
{
disease = _disease;
}
public String getName()
{
return name;
}
public int getHealth()
{
return health;
}
public String getDisease()
{
return disease;
}
}
What I want to know is how I could add new strings onto this variable, and then how to take away. For example, a passenger Bill starts at null for his diseases, and then contracts malaria and the cold. Bill's disease variable should now hold malaria, cold. Now say the user chooses to treat Bill's malaria. How would I
1) add malaria and cold
2) subtract just malaria from disease?
Whenever I attempt to change the disease with
passengers[index].setDisease() = null;
it says "error: method setDisease in class Passengers cannot be applied to given types:
required: String
found: no arguments"
I would reccomend making disease a Set of Strings.
Set<String> diseases = new HashSet<String>();
void addDisease(String disease) {
diseases.add(disease);
}
void removeDisease(String deisease) {
diseases.remove(disease);
}
Sets are "better", in this case, than other Collections because they cannot hold duplicates.
You should give the class a List<String> such as an ArrayList<String> and put the diseases in this List.
Better still, create a class or enum of Disease and have your Passenger class use a List<Disease> and avoid over-use of String. You could then give the class public addDisease(Disease disease) and removeDisease(Disease disease) methods.
Incidentally, your class above should be named Passenger, the singular, not Passengers, the plural, since it represents the concept of a single Passenger.
For your requirement if you are using List like ArrayList you can access your elements(disease names) by index, but it will allow duplicate data to be inserted(same disease may be added multiple times, it will unnecessary increase in number of diseases and may arise some problems).
If you use Set like HashSet it will allow unique element only, so no issues related to duplicated entries but at the same time you can't access a particular disease by index (if you need so, as of now I am not aware of your further requirement).
So as best of my knowledge I suggest you to use LinkedHashSet(HashSet with Linked approach) it will provide you FIFO order without duplicate insertion problem.

Categories

Resources