HashMap help

I'm stumped on how HashMap needs to work in this situation. It worked fine with an object array, but this is stumping me good. To be a little more specific, I need several functions to happen with this HashMap, each correlating with a button for the GUI.

It needs to be able to add Item objects to the HashMap (the key is the inventory number, and the value is the Item object), it needs to be able to delete an Item object (by using the inventory number), it needs to be able to find Items by inventory number, show all Items currently in the HashMap, save the info in the HashMap to a file on disk by using ObjectStreams, and a restore button just loads the data from that file to the HashMap.

It's a lot right? :D

I'm struggling to get anything to work with a HashMap however. If I could get some assistance with any of these functions, I would greatly appreciate it.

[code]
import java.util.HashMap;
import java.util.Iterator;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;


public class InventoryGUI {

Map = new HashMap();
private static int count = 0;

public static void main(String[] args) {
JFrame gui = new JFrame("Project 2 GUI");
gui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(5,2));

inputPanel.add(new Label("Inventory Number"));
final JTextField numberField = new JTextField(15);
inputPanel.add(numberField);

inputPanel.add(new Label("Name"));
final JTextField nameField = new JTextField(15);
inputPanel.add(nameField);

inputPanel.add(new Label("Manufacturer"));
final JTextField manField = new JTextField(15);
inputPanel.add(manField);

inputPanel.add(new Label("Description"));
final JTextField descField = new JTextField(15);
inputPanel.add(descField);

inputPanel.add(new Label("Price"));
final JTextField priceField = new JTextField(15);
inputPanel.add(priceField);

gui.add(inputPanel, BorderLayout.NORTH);


final DefaultListModel dfl = new DefaultListModel();
JList jl = new JList(dfl);
JScrollPane jsp = new JScrollPane(jl);
gui.add(jsp);

JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(2,1));
final JTextField errorMsg = new JTextField();
errorMsg.setEditable(false);
southPanel.add(errorMsg);

JPanel buttonPanel = new JPanel();
southPanel.add(buttonPanel);

JButton addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

String number = numberField.getText();
numberField.setText("");
String name = nameField.getText();
nameField.setText("");
String manufacturer = manField.getText();
manField.setText("");
String description = descField.getText();
descField.setText("");

float price = Integer.parseInt(priceField.getText());
priceField.setText("");

Item myItem=new Item(number, name, manufacturer, description, price);

map.put(number, myItem);
count++;
}
});
buttonPanel.add(addButton);

JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dfl.clear();

String number = numberField.getText();
numberField.setText("");

map.remove(number);



}
});
buttonPanel.add(deleteButton);

JButton findButton = new JButton("Find");
findButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dfl.clear();


String number = numberField.getText();
numberField.setText("");

Item item = map.get(number);

dfl.addElement(temp);

}
});
buttonPanel.add(findButton);

JButton showButton = new JButton("Show");
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dfl.clear();
for (Item p : item) {
dfl.addElement(p);
}

}
});
buttonPanel.add(showButton);

JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

Collection items=map.values();

}
});
buttonPanel.add(saveButton);

JButton restoreButton = new JButton("Restore");
restoreButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

objectOutputStream.writeObject(map);
}
});
buttonPanel.add(restoreButton);

JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(1);
}
});
buttonPanel.add(exitButton);

gui.add(southPanel, BorderLayout.SOUTH);

gui.pack();
gui.setVisible(true);
}

}

[/code]
[hr]

Can anyone see what I'm doing wrong? Any help is extremely welcome.
Oh in case someone wants to see it, this is my Item class

[code]
public class Item{
private String number;
private String name;
private String manufacturer;
private String description;
private float price;

public Item(String number, String name, String manufacturer,
String description, float price) {
this.number = number;
this.name = name;
this.manufacturer = manufacturer;
this.description = description;
this.price = price;
}

@Override
public String toString() {
return "#"+number+" N:"+name+" M:"+manufacturer+" D:"+description+" $"+price;
}
}[/code]

Comments

  • : I'm stumped on how HashMap needs to work in this situation. It
    : worked fine with an object array, but this is stumping me good. To
    : be a little more specific, I need several functions to happen with
    : this HashMap, each correlating with a button for the GUI.
    :
    : It needs to be able to add Item objects to the HashMap (the key is
    : the inventory number, and the value is the Item object), it needs to
    : be able to delete an Item object (by using the inventory number), it
    : needs to be able to find Items by inventory number, show all Items
    : currently in the HashMap, save the info in the HashMap to a file on
    : disk by using ObjectStreams, and a restore button just loads the
    : data from that file to the HashMap.
    :
    : It's a lot right? :D
    :
    : I'm struggling to get anything to work with a HashMap however. If I
    : could get some assistance with any of these functions, I would
    : greatly appreciate it.
    :
    : [code]:
    : import java.util.HashMap;
    : import java.util.Iterator;
    :
    : import java.awt.BorderLayout;
    : import java.awt.GridLayout;
    : import java.awt.Label;
    : import java.awt.event.ActionEvent;
    : import java.awt.event.ActionListener;
    :
    : import javax.swing.DefaultListModel;
    : import javax.swing.JButton;
    : import javax.swing.JFrame;
    : import javax.swing.JList;
    : import javax.swing.JPanel;
    : import javax.swing.JTextField;
    : import javax.swing.JScrollPane;
    :
    :
    : public class InventoryGUI {
    :
    : [red]Map = new HashMap(); // Missing variable name[/red]
    : private static int count = 0;
    :
    : public static void main(String[] args) {
    : JFrame gui = new JFrame("Project 2 GUI");
    : gui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    :
    : JPanel inputPanel = new JPanel();
    : inputPanel.setLayout(new GridLayout(5,2));
    :
    : inputPanel.add(new Label("Inventory Number"));
    : final JTextField numberField = new JTextField(15);
    : inputPanel.add(numberField);
    :
    : inputPanel.add(new Label("Name"));
    : final JTextField nameField = new JTextField(15);
    : inputPanel.add(nameField);
    :
    : inputPanel.add(new Label("Manufacturer"));
    : final JTextField manField = new JTextField(15);
    : inputPanel.add(manField);
    :
    : inputPanel.add(new Label("Description"));
    : final JTextField descField = new JTextField(15);
    : inputPanel.add(descField);
    :
    : inputPanel.add(new Label("Price"));
    : final JTextField priceField = new JTextField(15);
    : inputPanel.add(priceField);
    :
    : gui.add(inputPanel, BorderLayout.NORTH);
    :
    :
    : final DefaultListModel dfl = new DefaultListModel();
    : JList jl = new JList(dfl);
    : JScrollPane jsp = new JScrollPane(jl);
    : gui.add(jsp);
    :
    : JPanel southPanel = new JPanel();
    : southPanel.setLayout(new GridLayout(2,1));
    : final JTextField errorMsg = new JTextField();
    : errorMsg.setEditable(false);
    : southPanel.add(errorMsg);
    :
    : JPanel buttonPanel = new JPanel();
    : southPanel.add(buttonPanel);
    :
    : JButton addButton = new JButton("Add");
    : addButton.addActionListener(new ActionListener() {
    : public void actionPerformed(ActionEvent ae) {
    :
    : String number = numberField.getText();
    : numberField.setText("");
    : String name = nameField.getText();
    : nameField.setText("");
    : String manufacturer = manField.getText();
    : manField.setText("");
    : String description = descField.getText();
    : descField.setText("");
    :
    : float price = Integer.parseInt(priceField.getText());
    : priceField.setText("");
    :
    : Item myItem=new Item(number, name, manufacturer, description, price);
    :
    : map.put(number, myItem);
    : count++;
    : }
    : });
    : buttonPanel.add(addButton);
    :
    : JButton deleteButton = new JButton("Delete");
    : deleteButton.addActionListener(new ActionListener() {
    : public void actionPerformed(ActionEvent ae) {
    : dfl.clear();
    :
    : String number = numberField.getText();
    : numberField.setText("");
    :
    : map.remove(number);
    :
    :
    :
    : }
    : });
    : buttonPanel.add(deleteButton);
    :
    : JButton findButton = new JButton("Find");
    : findButton.addActionListener(new ActionListener() {
    : public void actionPerformed(ActionEvent ae) {
    : dfl.clear();
    :
    :
    : String number = numberField.getText();
    : numberField.setText("");
    :
    : Item item = map.get(number);
    :
    : dfl.addElement(temp);
    :
    : }
    : });
    : buttonPanel.add(findButton);
    :
    : JButton showButton = new JButton("Show");
    : showButton.addActionListener(new ActionListener() {
    : public void actionPerformed(ActionEvent ae) {
    : dfl.clear();
    : for (Item p : item) {
    : dfl.addElement(p);
    : }
    :
    : }
    : });
    : buttonPanel.add(showButton);
    :
    : JButton saveButton = new JButton("Save");
    : saveButton.addActionListener(new ActionListener() {
    : public void actionPerformed(ActionEvent ae) {
    :
    : Collection items=map.values();
    :
    : }
    : });
    : buttonPanel.add(saveButton);
    :
    : JButton restoreButton = new JButton("Restore");
    : restoreButton.addActionListener(new ActionListener() {
    : public void actionPerformed(ActionEvent ae) {
    :
    : objectOutputStream.writeObject(map);
    : }
    : });
    : buttonPanel.add(restoreButton);
    :
    : JButton exitButton = new JButton("Exit");
    : exitButton.addActionListener(new ActionListener() {
    : public void actionPerformed(ActionEvent ae) {
    : System.exit(1);
    : }
    : });
    : buttonPanel.add(exitButton);
    :
    : gui.add(southPanel, BorderLayout.SOUTH);
    :
    : gui.pack();
    : gui.setVisible(true);
    : }
    :
    : }
    :
    : [/code]:
    : [hr]
    :
    : Can anyone see what I'm doing wrong? Any help is extremely welcome.
    : Oh in case someone wants to see it, this is my Item class
    :
    : [code]:
    : public class Item{
    : private String number;
    : private String name;
    : private String manufacturer;
    : private String description;
    : private float price;
    :
    : public Item(String number, String name, String manufacturer,
    : String description, float price) {
    : this.number = number;
    : this.name = name;
    : this.manufacturer = manufacturer;
    : this.description = description;
    : this.price = price;
    : }
    :
    : @Override
    : public String toString() {
    : return "#"+number+" N:"+name+" M:"+manufacturer+" D:"+description+" $"+price;
    : }
    : }[/code]:
    :
    The only error I could find is marked in red above.
  • Yeah, I got that error too, but I'm slightly confused. I think it's saying that I don't have an Item object yet. I make Item objects in the add() function, so how do I handle this? I don't think I should create an Item object at the beginning because it won't use it. Hah, I'm confusing myself, can anyone clear this up for me?

  • : Yeah, I got that error too, but I'm slightly confused. I think it's
    : saying that I don't have an Item object yet. I make Item objects in
    : the add() function, so how do I handle this? I don't think I should
    : create an Item object at the beginning because it won't use it.
    : Hah, I'm confusing myself, can anyone clear this up for me?
    :
    :
    What is the precise error, and where does it occur? It might be that there's a subtle error in your Item class definition, which doesn't allow the compiler to compile it and give an error in your InventoryGUI class.
  • It the exact same error you pointed out. On line 20 (darn, they aren't numbered here) where this is:
    [code]Map = new HashMap(); // Missing variable name[/code]
    The error " expected" pops up. My Item class compiles just fine...not sure what to do now lol. Thanks for taking a look.



  • : It the exact same error you pointed out. On line 20 (darn, they
    : aren't numbered here) where this is:
    : [code]: Map = new HashMap(); // Missing variable name[/code]:
    : The error " expected" pops up. My Item class compiles
    : just fine...not sure what to do now lol. Thanks for taking a look.
    :
    :
    :
    :
    As you should know a declaration of a variable as the following format:
    [code]
    visibility flags Type name = value;
    [/code]
    For example (Taken from line 21):
    [code]
    private static int count = 0;
    [/code]
    In your map variable declaration, there is no variable name:
    [code]
    Map [red][b]????[/b][/red] = new HashMap();
    [/code]
    The visibility is specified: default
    There are no flags, which is correct since they are optional.
    The type is specified: Map
    The name is missing.
    The value is specified: new HashMap()

    For your code to work you [b]must[/b] add a variable name to the declaration.
  • Haha, wow I feel like an idiot. I was completely overlooking such a simple thing. I knew what it was looking for, I just thought it was referring somehow to my Item class, and not my variable name lol. Thanks for the heads up.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories