So I just wrote this java program to keep track of my art supply closet, and now I have to add a method to the InventoryItem class so that there is a way to change the String variable of any instance of the class post instantiation.
``` public class Inventory { private String item; private int count; public Inventory(String item, int count) { this.item = item; this.count = count; } public String getItem() { return item; } public int getCount() { return count; } public void setCount(int newCount) { count = newCount; } public static void main (String args[]){ InventoryItem item1 = new InventoryItem("Markers", 50); InventoryItem item2 = new InventoryItem("Crayons", 35); InventoryItem item3 = new InventoryItem("Scissors", 5); } } ```
Now the BIG question is, what do I do next? The first part was pretty straight forward but what next? What type of method should it be? As far as I can understand the method has to perform three tasks: 1- Take values for existing `item` and `newItem` 2- Somehow access the `item` with that name 3- Replace `item` with `newItem`
Are you planning on storing more information about those items other than the count?
No, this is just for inventory, keeping a count of all the materials.
Ok, one more thing. The class is called `Inventory` and you're creating objects of `InventoryItem`, are they meant to be the same?
Sorry, they should both be inventory. I created a duplicate of the original, so that when I muck-up atleast I'll have something I know that works to work with later on. I just forgot to change that.
What I attempted was this, but that looks so so so wrong! ``` public String Inventory(String item, String newItem) { this.item = item; this.newItem = newItem; item = newItem; return item; }
Well, the name InventoryItem seems more appropriate for what you did so far. But from what I get you simply want a dictionary. In java you can use Maps for that purpose. Here is a small example that stores counts of vehicles: http://examples.javacodegeeks.com/java-basics/java-map-example/ You can wrap it with your own class if you want and make methods with better names
okay so I still don't quite understand what is meant by key.
have you worked with lists?
as in arrays?
Well the terminology could mean different things, but for our sake it wouldn't matter. So if you have a list, you can access different items using an index, right?
yeah, so does that mean that I can access the different items separately?
The key plays a similar role, it allows you to access different items in the dictionary (Map in java). In lists the index is a number for the position of the item in the list. In dictionaries you can use keys, which are identifying objects (not necessarily integers) for the different items. You can access the items using their keys.
In the example with the vehicles they set up 4 counters for 4 vehicles. The key for each counter is the vehicle brand and it is used to access it. Perhaps this will make it clearer: http://tutorials.jenkov.com/java-collections/map.html#implementations
the way I understood it was that something like this was being created: ``` Item# | ItemName | ItemCount ______________ |___________________ |________________ | | item1 | Markers | 50 item2 | Crayons | 35 item3 | Scissors | 5 ``` Is this wrong?
Well sort of, but dictionaries are not necessarily ordered. From your perspective it is just bunch of pairs of keys and values. ``` Key | Values _____________ "Markers" | 50 "Crayons" | 35 "Scissors" | 5 ```
The dictionary basically attaches for each key object a value object. You can use the dictionary along with a key to get to the value object. You can also attach the key object to a different value object.
but essentially what I need to be doing is taking the key object and replacing it with another one. so I'm just changing the name of the key object.
So when we do: ```java my_map.put("scissors", 5); ``` I attach a key object which is the string `"scissors"` to the value object the integer 5. Now when I want to get the count of scissors I can do: ``` int c = (int)my_map.get("scissors"); // c = 5 ``` And I can attach the key to a different value to 'update' the count: ```java my_map.put("scissors", 15); int c = (int)my_map.get("scissors"); // c = 15 ```
I don't understand, why do you need to switch the key object?
okay so for the first part I followed along with my instructor, and I got the exact same thing. and then after all that he said that, if later on, I want to replace the name of an item, how can I do it. He said that currently there is no way of returning to my InventoryItem object later and changing "markers" to, for example, "felt-tips". He said we should amend the program so that the we can change an InventoryItem's String variable after we have made the object (by adding a method). For example after I have made my changes it should be possible to change item1's String variable from "markers" to "felt-tips"
http://stackoverflow.com/questions/10766906/is-it-possible-to-rename-a-hashmap-key ``` Object obj = map.remove("oldKey"); map.put("newKey", obj); ``` Very simple solution =)
I don't get it. Where does this go? and why are oldKey and newKey in quotation marks?
Because they key here is a string, but in theory you're right, it could be something else. Say you want to rename the item you can do: ``` Object count = map.remove("scissors"); map.put("pens", count); ```
Now instead of "scissors" you have "pens" with the same count.
I was suppose to put it in a method right?
What do you mean?
I suggest making a class Inventory that has a member Map and methods like SetItemCount, GetItemCount, RenameItem, RemoveItem etc. working with the internal map member
ok...don't laugh this is what I did and, no surprises, it was wrong ``` import java.util.HashMap; import java.util.Map; public class Inventory { private String item; private int count; private String newItem; public Inventory(String item, int count) { this.item = item; this.count = count; } public String getItem() { return item; } public int getCount() { return count; } public void setCount(int newCount) { count = newCount; } public String Replacement(String item, String newItem) { Object count = map.remove("scissors"); map.put("pens", count); } public static void main (String args[]){ Inventory item1 = new Inventory("Markers", 50); Inventory item2 = new Inventory("Crayons", 35); Inventory item3 = new Inventory("Scissors", 5); } }
Changed getCount to return int instead of String ```java import java.util.HashMap; import java.util.Map; class Inventory { private Map items = new HashMap(); public Inventory(){ } public int getCount(String item_name) { return (int)items.get(item_name); } public void setCount(String item_name, int count) { items.put(item_name, count); } public void removeItem(String item_name) { items.remove(item_name); } public String renameItem(String item_name, String new_name) { Object count = items.remove(item_name); items.put(new_name, count); } } public class MyProgram { public static void main (String args[]){ Inventory inventory = new Inventory(); inventory.setCount("Markers", 50); inventory.setCount("Crayons", 35); inventory.setCount("Scissors", 5); inventory.renameItem("Scissors", "Pens"); } } ```
I'm getting an error on line 23, the first line below: ``` public String renameItem(String item_name, String new_name) { Object count = items.remove(item_name); items.put(new_name, count); }
oops, why is that String, let me fix it hehe ``` import java.util.HashMap; import java.util.Map; class Inventory { private Map items = new HashMap(); public Inventory(){ } public int getCount(String item_name) { return (int)items.get(item_name); } public void setCount(String item_name, int count) { items.put(item_name, count); } public void removeItem(String item_name) { items.remove(item_name); } public void renameItem(String item_name, String new_name) { Object count = items.remove(item_name); items.put(new_name, count); } } public class MyProgram { public static void main (String args[]){ Inventory inventory = new Inventory(); inventory.setCount("Markers", 50); inventory.setCount("Crayons", 35); inventory.setCount("Scissors", 5); inventory.renameItem("Scissors", "Pens"); } } ``` Ok now?
Yeah I did that too when the word String is there it says, " this method must return a result of type String" and when I take it out, it says, "return type for method is missing"
Replace it with void, don't take it out
I did, the error went away... but how do I test it now?
Ok, you could add print method to the inventory class ``` import java.util.HashMap; import java.util.Map; class Inventory { private Map items = new HashMap(); public Inventory(){ } public int getCount(String item_name) { return (int)items.get(item_name); } public void setCount(String item_name, int count) { items.put(item_name, count); } public void removeItem(String item_name) { items.remove(item_name); } public void renameItem(String item_name, String new_name) { Object count = items.remove(item_name); items.put(new_name, count); } public void print() { for(String key: items.keySet()){ System.out.println(key + ": " + items.get(key)); } } } public class MyProgram { public static void main (String args[]){ Inventory inventory = new Inventory(); inventory.setCount("Markers", 50); inventory.setCount("Crayons", 35); inventory.setCount("Scissors", 5); inventory.print(); iventory.setCount("Scissors", 3); inventory.print(); inventory.renameItem("Scissors", "Pens"); inventory.print(); } } ```
there's an error on `items.keySet()`, it says "Type mismatch: cannot convert from element type Object to String" ``` public void print() { for(String key: items.keySet()){ System.out.println(key + ": " + items.get(key)); } }
Oops it's int type lol, I lost focus =\ ``` public void print() { for(String key: items.keySet()){ System.out.println(key + ": " + (int)items.get(key)); } } ```
nope, both of them are giving an error
Bleh, let me check
Post all your code in pastebin, and let me know the purpose of the program.. shortcomings and how you want it to work. I'll join in on the fun.
I got it to work but I see a: `Note: MyProgram.java uses unchecked or unsafe operations.` So I'll get rid of it first
Deprecated method? lol which one
I don't think it's deprecated method, I think I do unsafe casting hehe
ah.. I ignore that error when I do object Serialization file IO; can't be helped in that instance I think.
I don't like java =S I miss C++ =( Ok so let me get it back to a usable state.. just before it screw it up..
Lol. I guess it's just my 'native language'
Still here @javk ?
Eh... anything I do in java comes along with the thought "in python that wouldn't be so hard.." ``` import java.util.HashMap; import java.util.Map; class Inventory { private Map items = new HashMap(); public Inventory(){ } public int getCount(String item_name) { return (int)items.get(item_name); } public void setCount(String item_name, int count) { items.put(item_name, count); } public void removeItem(String item_name) { items.remove(item_name); } public void renameItem(String item_name, String new_name) { Object count = items.remove(item_name); items.put(new_name, count); } public void print() { System.out.println("~~~Inventory~~~"); for(Object key: items.keySet()){ System.out.println(key.toString() + ": " + items.get(key).toString() ); } } } public class MyProgram { public static void main (String args[]){ Inventory inventory = new Inventory(); inventory.setCount("Markers", 50); inventory.setCount("Crayons", 35); inventory.setCount("Scissors", 5); inventory.print(); inventory.setCount("Scissors", 3); inventory.print(); inventory.renameItem("Scissors", "Pens"); inventory.print(); } } ``` Output: ``` ~~~Inventory~~~ Crayons: 35 Scissors: 5 Markers: 50 ~~~Inventory~~~ Crayons: 35 Scissors: 3 Markers: 50 ~~~Inventory~~~ Crayons: 35 Markers: 50 Pens: 3 ```
yay, it works
In order to get rid of this 'note' we have to specify explicitly the types of the Map. But we can't use primitive types for that so we'll have to switch to `Integer` instead of `int`: ``` import java.util.HashMap; import java.util.Map; class Inventory { private Map<String, Integer> items = new HashMap<String, Integer>(); public Inventory(){ } public int getCount(String item_name) { return items.get(item_name); } public void setCount(String item_name, int count) { items.put(item_name, new Integer(count)); } public void removeItem(String item_name) { items.remove(item_name); } public void renameItem(String item_name, String new_name) { Integer count = items.remove(item_name); items.put(new_name, count); } public void print() { System.out.println("~~~Inventory~~~"); for(String key: items.keySet()){ System.out.println(key + ": " + items.get(key)); } } } public class MyProgram { public static void main (String args[]){ Inventory inventory = new Inventory(); inventory.setCount("Markers", 50); inventory.setCount("Crayons", 35); inventory.setCount("Scissors", 5); inventory.print(); inventory.setCount("Scissors", 3); inventory.print(); inventory.renameItem("Scissors", "Pens"); inventory.print(); } } ```
Join our real-time social learning platform and learn together with your friends!