Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hash Table

Status
Not open for further replies.

zcruz

Programmer
Joined
Oct 25, 2002
Messages
2
Location
US
I don't know how to get started on the code for the hash table which will be using chaining. All I know is that I'm creating an inventory system that will store an item's:
1) SKU
2) description
3) cost
4) retail
5) qty on hand

and the SKU number is what is going to be used for storing the items in a hash table. So when a certain SKU is entered, that item associated with that SKU will show the description, cost, retail, and quantity of that item. I've never written code for a hash table so please help.

 
Suppose you have the class :

Code:
class Item
{
  //Constructor
  public Item (String sku, String desc, int cost, ??? retail, int quantity) {...}

  //Accessors
  public String sku () {...}
  public String description () {...}
  public int cost () {...}
  public ??? retail () {...}//I don't know the type
  public int quantity () {...}
};

Then your inventory system could work like this :

Code:
//Create the inventory
import java.util.Hashtable;
...
Hashtable inventory = new Hashtable ();

//Insert items in inventory
inventory.put ("item1", new Item ("item1", "desc1", 0, ?, 1);
inventory.put ("item2", new Item ("item2", "desc2", 0, ?, 1);
...

//Get item1 from the inventory
if (inventory.containsKey ("item1"))
  Item item1 = (Item) inventory.get ("item1")
else
  //error code

See for further documentation.

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top