deleting duplicates from array list - java

I'm creating a simple RPG console game, I'm at the stage of creating an inventory and loot system. Present in the program, both class Player and class Monster have the arrayList Backpack properties, when the program creates an object of the monster class, items in the monster's backpack are also automatically created, after killing the monster, you can take them to your backpack, and this is where my problem begins how to elegantly prevent duplication of items in the backpack, each item is a class too, now this function works by checking in a nested loop each item one by one to see if it is already in the backpack if it is instead of adding it once moreover, it increases its amount property, if I don't have this item in my backpack, it just adds to the list, the solution works, but definitely it is not the right solution, because with many of items this checking mechanism will grow a lot, if anyone has any valuable tips I will be grateful.
I also have a second idea to create a boolean Is_it_in_Backpack variable, and somehow connect it with the loot collecting mechanism
Below some code sample
public class Player {
public static ArrayList<Item> Backpack = new ArrayList<>()
}
and the class Skieleton:
public class Skieleton extends Monsters {
public static ArrayList<Item> Backpack;
public Skieleton() {
Backpack = new ArrayList<>();
Backpack.add(new Weapon("Rusty sword", "Just a rusty sword", 3, 2 ));
Backpack.add(new Armor("Leather Armor", "Old leather armor", 6, 3));
}
class item:
public class Item {
public String ItemName;
public String Description;
public int ItemSize;
public int ItemWeight;
public int Amount;
public Item(String ItemName, String Description, int ItemSize, int ItemWeight)
{
this.ItemName = ItemName;
this.Description = Description;
this.ItemSize = ItemSize;
this.ItemWeight = ItemWeight;
}
public Item() {
}
}

I recommend you use a class that extends java.util.Set:
If order is not important for you, you can use HashSet;
If order of insertion is important, you can use LinkedHashSet;
If natural order is important (alphabetical by name or other property), you can use TreeSet and implement the interface Comparable onto the class inserted in the collection.
However, regardless of your choice, it's recommended you implement hashCode() (for optimization) and equals() (to let collection identify which item is equal to other and avoid duplication).

If you can use third party libraries, I'd recommend using a Bag from Eclipse Collections.
With your Item class implementing equals and hashCode on ItemName field, your example usage could look like:
final MutableBag<Item> backPack = new HashBag<>();
final Item rustySword = new Item("Rusty sword", "Just a rusty sword", 3, 2);
final Item leatherArmour = new Item("Leather Armor", "Old leather armor", 6, 3);
backPack.add(rustySword);
backPack.add(leatherArmour);
backPack.add(rustySword);
System.out.println(backPack.toMapOfItemToCount()); // prints {Item[ItemName='Rusty sword']=2, Item[ItemName='Leather Armor']=1}
System.out.println(backPack.occurrencesOf(rustySword)); // prints 2
The API is rich, and provides a lot more:
https://www.eclipse.org/collections/javadoc/11.0.0/org/eclipse/collections/api/bag/Bag.html

I would use a Map.
Here's my suggestion:
import java.util.*;
class Player {
public Backpack backpack= new Backpack();
}
class Monster { }
class Skieleton extends Monster {
public Backpack backpack= new Backpack();
public Skieleton() {
backpack.add(new Weapon("Rusty sword", "Just a rusty sword", 3, 2 ));
backpack.add(new Armor("Leather Armor", "Old leather armor", 6, 3));
}
}
class Backpack {
private HashMap<Item,Item> items = new HashMap<>();
public Item add(Item item){
if (items.containsKey(item)){
items.get(item).Amount=+ item.Amount;
return items.get(item);
} else {
items.put(item,item);
return item;
}
}
public Item get(Item item){
return items.getOrDefault(item, null);
}
}
class Item {
public String ItemName;
public String Description;
public int ItemSize;
public int ItemWeight;
public int Amount;
public Item(String ItemName, String Description, int ItemSize, int ItemWeight)
{
this.ItemName = ItemName;
this.Description = Description;
this.ItemSize = ItemSize;
this.ItemWeight = ItemWeight;
}
public Item() {
}
public boolean equals(Object o){
if (o instanceof Item){
return ItemName.equals( ((Item)o).ItemName);
}
return false;
}
}

You can use a HashMap for storing the items. First, I would like to change the Item class to not have amount field in it. Let an item denote what an item is (name, description, size and weight).
Here's the updated backpack:
Map<Item, Integer> backpack = new HashMap<>();
To add an item, you can use Map#merge method.
public void addItemToBackpack(Item item, int quantity) {
backpack.merge(item, quantity, (oldQuantity, newQuantity) -> oldQuantity + newQuantity);
}
We insert the item and its quantity. If the item is not already present, it will be inserted with the passed quantity.
If it is already present, then the BiFunction which is a mapping function will be called with the existing value (oldQuantity) and the new value which we tried to insert (newQuantity). We sum them both, return it and the item's value (quantity) will be updated with this value.
Using method references, we can write the above as,
backpack.merge(item, quantity, Integer::sum);

Related

How to print an arrayList using a variable containing the arrayList name

I know this sounds a bit stupid but I am extending a text-based game and I'm trying to figure out a way to print out the items that are in the room. I have made an arrayList for the room and added and "Item" into it by using a separate class called Item.
import java.util.ArrayList;
import java.util.List;
public class Item
{
private String itemName;
private Boolean pickUp;
private Integer itemWeight;
/**
* Constructor for objects of class Items
*/
public Item(String itemName, Boolean pickUp, Integer itemWeight)
{
this.itemName = itemName;
this.pickUp = pickUp;
this.itemWeight = itemWeight;
}
/**
* Returns the name of the item
*/
public String getItemName()
{
return itemName;
}
/**
* Returns the pickUp ability of the item
*/
public Boolean getPickUp()
{
return pickUp;
}
/**
* Returns the weight of the item
*/
public Integer getItemWeight()
{
return itemWeight;
}
}
The part I'm stuck on now is printing those elements out when entering the room. Theres a function in the code that keeps track of the current room and I am trying to use that function to then print out the elements of that corresponding rooms arrayList. Each room in the game is an object from the "Room" class.
public class Room {
private String description;
private String listName;
private HashMap<String, Room> exits; // stores exits of this room.
public Room(String description, String listName)
{
this.description = description;
this.listName = listName;
exits = new HashMap<>();
}
public String getListName()
{
return listName;
}
private void createRooms()
{
Room playground, assemblyHall, groundHallway, lunchHall, ITRoom, groundToilets, headteacherOffice, footballCage, stairs, groundClassroom, firstFloorClassroom, exitGate;
// create the rooms
assemblyHall = new Room("in the assembly hall", "assemblyHallList");
playground = new Room("in the main playground of the school", "playgroundList");
.......
currentRoom = assemblyHall;
ArrayList<Item> assemblyHallList = new ArrayList<Item>();
assemblyHallList.add(new Item("Piece of paper", true, 1));
These last 2 lines are an example of what I will be doing for every room with more items. I feel like I am going about this the wrong way but this was the only idea I had. The problem I have is printing out the elements when I enter a room as I would need to call on the method to get the name of the arrayList then use that name to then access the list and print out the elements in it but I have no idea how to use the string to accesss the array as I would have to use
currentRoom.getListName()
to get the name of the list but when I try to print it out it prints the name itself, not the elements. Any help is appreciated and I can send more parts of the class if needed. Didn't include it all as its very big and a lot of it is irrelevant to my problem
To answer your question, you can't just directly access a variable from a string containing its name. Technically, you could make a static Hashmap<String, ArrayList<Item>> to keep track of all the lists and access them by their name.
However, I don't see why you couldn't just make the list itself part of the constructor and then store the list as an instance variable of the Room.
Something like this would work better:
ArrayList<Item> items = new ArrayList<>();
items.add(new Item("Piece of paper", true, 1));
Room room = new Room("best room", items);
public class Room {
private String description;
private ArrayList<Item> items;
private HashMap<String, Room> exits;
public Room(String description, ArrayList<Item> items)
{
this.description = description;
this.items = items;
exits = new HashMap<>();
}
// Other members not shown
}
You create a ArrayList<Item> assemblyHallList, then you create
Room assemblyHall = new Room("in the assembly hall", "assemblyHallList");
And now you expect that room to have some reference to the List.
That's not how this works. The Room now has the String value "assemblyHallList" in the listName field. But there is no reference to the List. And since assemblyHallList is a local variable in your createRooms method, there's no way of resolving it.
The Solution is this: Don't assign the name of the List variable to the Room (why would that work?), instead assign the List itself:
List<Item> assemblyHallList = ...;
Room assemblyHall = new Room("in the assembly hall", assemblyHallList);
Of course you need to change the type of the constructor's parameter and Room's listName field to List<Item>.

Add array from list into a new set in Java

I want to get the model numbers from the list only
['brand: Samsung, model number: VA2210-MH, size: 21.5', 'brand: Philipe, model number: 244E1SB, size: 21.5']
And I set create attributes and getter and setter of all attributes(only model number will be shown) in Monitor
public class Monitor{
public String brand;
public String modelNumber;
public double size;
public Monitor(String brand, String modelNumber, double size){
this.brand = brand;
this.modelNumber = modelNumber;
this.size = size;
}
public void setModelNumber(String amodelNumber){
modelNumber = amodelNumber;
}
public String getModelNumber(){
return modelNumber;
}
}
so I create a list and add the information into the list
and a method to create a set with model number by the method modelNumberSet()
import java.util.*;
public class ComputerShop{
private List<Monitor> monitorList = new ArrayList<>();
public void addMonitor(String brand, String modelNumber, double size){
Monitor newMonitor = new Monitor(brand, modelNumber, size);
monitorList.add(newMonitor);
}
public Set<Monitor> modelNumberSet(){
Set<Monitor> NewSet = new HashSet<>();
for (Monitor m : monitorList) {
NewSet.add(m.getModelNumber());
}
return NewSet;
}
}
I hope the model number will be added into a new set, the output looks like
[VA2210-MH, 244E1SB]
So I use for loop to incase I will add more information in the future, but the error comes out when I use add(). Why the array cannot be added into the new set? Am I using the wrong function?
Is there a better solution I should use?
Change Set<Monitor> to Set<String>. You are adding model numbers to the set and their types are String. You are trying to put a String where a Monitor is expected. Square peg in a round hole.
Fix the modelNumberSet() method as follows:
public Set<String> modelNumberSet(){
Set<String> newSet = new HashSet<>();
for (Monitor m : monitorList) {
newSet.add(m.getModelNumber());
}
return newSet;
}

How to use hashtable and bucket hashing

So recently I have been trying to practice with hashing and using linkedlists in a table to store values. I understand the concept but I am having trouble putting it into practice and can't seem to find what I'm looking for online.
For example:
Let's say I wanted to use a hashtable to store things like for a computer such as the monitor, mouse, etc. I would want methods such as:
boolean addMonitor(String id, String description, double price, int units, String size)
boolean addMouse(String id, String description, double price, int units, int buttons)
I don't understand how to use these methods to store them in a hashtable. I would obviously like to use other methods to access and change the values within each later too. Any help is appreciated. Thank you.
Even if its name says "table", HashTable is not like a "database table" where you have columns, and each column store values... it seems that you want use hashtable as a database table.
Hashtable store objects! So your methods should look better like this:
public class Example {
public static void main(String[] args) {
ItemStore store;
Monitor monitor;
Mouse mouse;
store = new ItemStore();
monitor = new Monitor();
monitor.id = 2;
monitor.price = 6;
mouse = new Mouse();
mouse.id = 7;
mouse.buttons = 3;
store.addItem(monitor);
store.addItem(mouse);
System.out.println(store.getItem(2).price); // = 6
System.out.println(((Monitor) store.getItem(2)).dpi);
System.out.println(((Mouse) store.getItem(7)).buttons); //Downcasting ... = 3
}
public static class Item {
String id;
String description;
int price;
// common attributes here!
}
public static class Monitor extends Item {
private int dpi;
// monitor particular atributes here!!
}
public static class Mouse extends Item {
private int buttons;
// mouse particular attributes here!!!
}
public static class ItemStore {
private Hashtable<String, Item> table = new HashTable<>();
public boolean addItem(Item item) {
this.table.put(item.getId(), item);
}
public Item getItem(String id) {
return this.table.get(id);
}
}
}

Adding subcategories to a java Enum

Suppose I have a simple Java Enum:
public Enum itemType
{
FRUITS("fru"),
VEGETABLES("veg"),
LIQUOURS("liq"),
SODAS("sod");
private String dbCode;
public ItemType(String dbCode){
this.dbCode = dbCode;
}
public String getDbCode(){
return this.dbCode;
}
}
I would now like to introduce a "category" to this enum, for example to make the distinction between liquid items and solid items. I found two ways of doing this within the enum class, see below. However, both suffer from the same anti-pattern: if the amount of categories or amount of items ever increases/decreases (imagine 100 item types with 10 categories!), I've got a lot of updating to do. What patterns can I use to design this enum as cleanly and re-usable as possible?
First approach: Add additional properties to the enum
public Enum itemType
{
FRUITS("fru",false),
VEGETABLES("veg",false),
LIQUOURS("liq",true),
SODAS("sod",true);
private String dbCode;
private boolean liquid;
public ItemType(String dbCode, boolean liquid){
this.dbCode = dbCode;
this.liquid = liquid;
}
public String getDbCode(){
return this.dbCode;
}
public boolean isLiquid(){
return this.liquid;
}
}
Second approach: Use static methods to ask about subcategories
public Enum itemType
{
FRUITS("fru"),
VEGETABLES("veg"),
LIQUOURS("liq"),
SODAS("sod");
private String dbCode;
public ItemType(String dbCode){
this.dbCode = dbCode;
}
public String getDbCode(){
return this.dbCode;
}
public static boolean isLiquid(ItemType type){
switch(t){
case SODA:
case LIQOURS: return true;
default: return false;
}
}
How about using an EnumSet for that?
public enum ItemType
{
FRUITS("fru"),
VEGETABLES("veg"),
LIQUOURS("liq"),
SODAS("sod");
public static final EnumSet<ItemType> LIQUIDS = EnumSet.of(LIQUOURS, SODAS);
// ...
}
Then you can use ItemType.LIQUIDS.contains(someItemType) to check if someItemType is a "liquid".
I would do something like:
enum Category {
LIQUID, SOLID;
}
enum ItemType {
FRUITS("fru", SOLID),
VEGETABLES("veg", SOLID),
LIQUOURS("liq", LIQUID),
SODAS("sod", LIQUID);
private String dbCode;
private Category category;
public ItemType(String dbCode, Category category){
this.dbCode = dbCode;
this.category = category;
}
/* getters / setters */
}
That would allow, for example, that you can add new products and categories (e.g. BUTANE("but", GAS)) without having to modify the existing code (as would happen in Approach 2).
On the other hand, if the number of categories and items is long and changing, I would consider to use a SQL database.
Since you are modeling something that has no logic that can be encoded in an algorithmic way (i.e. there's no algorithm that would figure out that "sod" is liquid and "veg" is not) there is no way around enumerating all related pairs of (item, category) in one way or the other.
There are three approaches to implementing it:
Enumerate categories on item's side - this is what your code does in both cases, or
Enumerate items on category's side - this would build an enum of categories, and attach a full list of items to each of them, or
Enumerate item+category pairs independently - this approach may be useful when storing item/category mapping in the database or in a configuration file.
I would recommend taking the third approach as it is the most "symmetric" one. Make a table for categories with category codes, and add a "cross-table" (or a cross-file) that has all pairs of categories and their corresponding items. Read the cross table/file at startup, and set up the dependencies on both sides.
public Enum ItemType {
FRUITS("fru")
, VEGETABLES("veg")
, LIQUOURS("liq")
, SODAS("sod");
public void addCategory(ItemCategory category) ...;
public EnumSet<ItemCategory> getItemCategories() ...;
}
public Enum ItemCategory {
LIQUIDS("liq")
, SNACKS("snk")
, FAST("fst");
public void addItem(ItemType type) ...;
public EnumSet<ItemType> getItemTypes() ...;
}
Cross-file or cross-table may look like this:
liq liq
sod liq
fru snk
fru fst
sod fst
You process it by enumerating pairs, and calling addCategory on the pair's item side, and calling addItem on the pair's category side.
These were three excellent answers, but I think I can combine all three in one nice package:
public enum ItemType {
FRUITS("fru",PERISHABLE),
VEGETABLES("veg",PERISHABLE),
LIQUOURS("liq",LIQUIDS),
SODAS("sod",LIQUIDS),
FRESH_SQUEEZED_ORANGE_JUICE("orgj",LIQUIDS,PERISHABLE);
private final String dbCode;
private final EnumSet<ItemCategory> categories;
private static final Map<ItemCategory,Set<ItemType>> INDEX_BY_CATEGORY = new EnumMap<>(ItemCategory.class);
ItemType(String dbcode,ItemCategory... categories) {
this.dbCode = dbcode;
this.categories = EnumSet.copyOf(Arrays.asList(categories));
//for (ItemCategory c:categories) {
// // Illegal Reference to Static Field!
// INDEX_BY_CATEGORY.put(c, this);
//}
}
static {
for (ItemCategory c:ItemCategory.values()) {
INDEX_BY_CATEGORY.put(c, EnumSet.noneOf(ItemType.class));
}
for (ItemType t:values()) {
for (ItemCategory c:t.categories) {
INDEX_BY_CATEGORY.get(c).add(t);
}
}
}
public boolean is(ItemCategory c) {
return INDEX_BY_CATEGORY.get(c).contains(this);
}
public Set<ItemType> getAll(ItemCategory c) {
return EnumSet.copyOf(INDEX_BY_CATEGORY.get(c));
}
public String getDbCode() {
return dbCode;
}
}
Now,
we can easily ask about additional subcategories without writing the code for it: boolean isVegetableLiquid = VEGETABLES.is(LIQUIDS);
we can easily assign not only one, but multiple categories to an item as you can see for FRESH_SQUEEZED_ORANGE_JUICE.
we are using EnumSet and EnumMap for performance, including their methods like contains.
we absolutely are minimizing the amount of code required to add an additional item. This could be further minimized by setting this up by database or configuration. However, in that case we would have to avoid the use of Enum as well.

Adding n number of items to a room class

I'm working through a programming book for class. The current project is a terminal based adventure game.
Currently I have everything compiling and the program does run but it returns 'null' for the item description.
How do I go about actually putting the items specified in the item class into rooms / make it so that rooms are created with items inside?
Is there a way to get this to be done randomly?
below is my item class.
import java.util.ArrayList;
import java.util.HashMap;
public class Item
{
private String idescription;
private int iweight;
private HashMap items;
public Item(String idescription, int iweight)
{
this.idescription = idescription;
this.iweight = iweight;
items=new HashMap();
}
private void addItems()
{
items.put("flier", new Item("there is a flier for a club on the floor",
0));
items.put("baccy", new Item("there is a pouch of tobacco on\n" +
"the table, your friends say you may help yourself if you can find\n" +
"rolling papers, filter tips and a lighter.",2));
//...
}
}
This is my Room class
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
public class Room
{
private String rdescription;
private HashMap<String, Room> exits;
private HashMap items;
private Item itemroom;
private Item item;
public Room(String rdescription)
{
items = new HashMap();
this.rdescription = rdescription;
exits = new HashMap<String, Room>(); //creates the rooms
}
public void addItems(String item_name, String idescription, int iweight)
{
items.put(item_name, new Item(idescription, iweight));
}
public void setExits(String direction, Room neighbour)
{
exits.put(direction, neighbour);
}
public Room getExit(String direction)
{
return exits.get(direction);
}
public String getExitString()
{
String returnString = "Exits: ";
Set<String> keys = exits.keySet();
for(String exit: keys)
{
returnString += " " + exit;
}
return returnString;
}
public String getDescription()
{
return rdescription;
}
public String getLongDescription()
{
return "You are at the" + rdescription + ".\n" +
getExitString()+". \n" +
items.getIdescription();
}
}
I hope someone can help. Still very much a beginner and going through many phases of utter panic and confusion.
I am a little confused by your code, a common mistake for beginners it to over-complicate things. From what I can make out, rooms are supposed to contain items. So, we have 2 classes: Room and Item.
The minimum for the Room class could be something like this:
public class Room {
private List<Item> items = new ArrayList<Item>();
public void addItem(Item item) {
items.add(item);
}
public List<Item> getItems() {
return Collections.unmodifiableList(items);
}
}
That way, the Room contais a list of Items and has methods allowing you to add an item, or list the items.
I took a stab at this problem. It is quite a bit of code to go through. My logic was instead create a ItemList class that houses all of the different items. You can add methods to add a new item to the list of available items or delete an existing item. Then I would just make methods to return a random Item or List<Item> and then just call these methods from your room class. Something like this:
public class ItemList {
private List<Item> items = new ArrayList<>();
private void addItems(){
//I would keep this the same but instead put them in the itemList variable
items.add(new Item());
}
public Item randomItem(){
return items.get(new Random().nextInt(items.size()));
}
public List<Item> randomItemList(int numOfRandomItems){
List<Item> output = new ArrayList<>();
for (int i=0; i<numOfRandomItems; i++){
output.add(randomItem());
}
return output;
}
}
class Item{
//Made these public for example. I would continue using constructor, get/set for the fields.
String idescription;
int iweight;
}
With this, you can even set it up so that rooms have different ItemList so different rooms can choose a random item from different lists. Hope this helps.

Categories

Resources