Modify the Inventory Program to include and Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. Add a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file. Use exception handling to create the directory and file if necessary.
As I said, everything works well except the save button. I'm not getting any errors, it just doesn't do anything when I click it. Here is my code:
// stores a collection of DVD's
import javax.swing.*;
import java.awt.event.*;
public class InventoryPart6 extends JFrame {
private JTextArea txt;
private Inventory i;
private int view = 0;
public InventoryPart6() {
super("DVD");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // quit if the window is closed
// create 3 DVD's
DVD p1 = new DVD(3, "Night at the Museum", 3, 19.99, "Family");
DVD p2 = new DVD(4, "Without a Paddle", 2, 14.99, "Comedy");
DVD p3 = new DVD(1, "A Beautiful Mind", 6, 24.99, "Drama");
// make inventory
i = new Inventory(3);
i.add(p1, 0);
i.add(p2, 1);
i.add(p3, 2);
// sort
i.sort();
// output the products using proper formatting, with a header row
// the columns are spaced using tabs to look nice
System.out.println("Number\tName\tUnits\tPrice\tVal\tFee\tGenre");
for (int k = 0; k < 3; k++) {
System.out.println(i.get(k));
}
System.out.println();
// total val
System.out.printf("Total value = $%.2f", i.totalValue());
//graphical interface
JPanel panel = new JPanel();
txt = new JTextArea(15,20);
txt.setEditable(false);//user shouldn't change it
panel.add(txt);
JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));
JPanel anotherbuttonpanel = new JPanel();
anotherbuttonpanel.setLayout(new BoxLayout(anotherbuttonpanel,BoxLayout.Y_AXIS));
JButton first = new JButton("First");
first.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
view = 0;// go to the beginning
showDVD();
}
});
buttonpanel.add(first);
JButton previous = new JButton("Previous");
previous.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (view > 0) view--;
else view = i.size()-1;
showDVD();
}
});
buttonpanel.add(previous);
JButton next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (view < i.size()-1) view++; //advance to the end
else view = 0;
showDVD();
}
});
buttonpanel.add(next);
JButton last = new JButton("Last");
last.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
view = i.size()-1;
showDVD();
}
});
buttonpanel.add(last);
// new buttons
JButton save = new JButton("Save");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i.save();
}
});
anotherbuttonpanel.add(save);
JButton search = new JButton("Search");
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = JOptionPane.showInputDialog(InventoryPart6.this,"Enter the name to search for");
int result = i.searchForDVD(str);
if (result == -1) JOptionPane.showMessageDialog(InventoryPart6.this,"Not found"); // do nothing
else { // show the item
view = result;
showDVD();
}
}
});
anotherbuttonpanel.add(search);
JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = JOptionPane.showInputDialog(InventoryPart6.this,"Enter the DVD's name");
int units = Integer.parseInt(JOptionPane.showInputDialog(InventoryPart6.this,"Enter the units in stock"));
double price = Double.parseDouble(JOptionPane.showInputDialog(InventoryPart6.this,"Enter the price of each item"));
String manu = JOptionPane.showInputDialog(InventoryPart6.this,"Enter the Genre");
// make the object
DVD soft = new DVD(i.highestNumber()+1, name, units, price, manu);
// put it in the inv, at the very end
i.addNewDVD(soft);
view = i.size()-1;
showDVD();
}
});
anotherbuttonpanel.add(add);
JButton modify = new JButton("Modify");
modify.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// get new values and set them
DVD soft = i.get(view);
soft.setNumber(Integer.parseInt(
JOptionPane.showInputDialog(InventoryPart6.this,"Enter new item number",soft.getNumber())));
soft.setName(
JOptionPane.showInputDialog(InventoryPart6.this,"Enter new item name",soft.getName()));
soft.setUnits(Integer.parseInt(
[code] JOptionPane.showInputDialog(InventoryPart6.this,"Enter new units in stock",soft.getUnits())));
soft.setPrice(Double.parseDouble(
JOptionPane.showInputDialog(InventoryPart6.this,"Enter new price of each item",soft.getPrice())));
soft.setGenre(
JOptionPane.showInputDialog(InventoryPart6.this,"Enter new manufacturer",soft.getGenre()));
// show it
showDVD();
}
});
anotherbuttonpanel.add(modify);
JButton delete = new JButton("Delete");
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (i.size() > 0) { // else there is nothing to delete
i.deleteDVD(i.get(view));
view--;
if (view < 0) view = 0;
showDVD();
}
}
});
anotherbuttonpanel.add(delete);
buttonpanel.add(new Logo());
panel.add(buttonpanel);
panel.add(anotherbuttonpanel);
getContentPane().add(panel);
showDVD();
}
public static void main(String[] args) {
InventoryPart6 gui = new InventoryPart6();
gui.pack();
gui.setVisible(true);
}
// view DVD
public void showDVD() {
txt.setText("");
if (i.size() < 1) return;
txt.setText("DVD Details:\n");
txt.append("Number Name Units Price Val Fee Genre\n");
txt.append(i.get(view).toString()+"\n");
txt.append(String.format
("Value of all the DVD's: $%.2f", i.totalValue()));
}
}
Thanks in advance for any advice!
Shannon
Please use the code tags
It makes us easier to read and cust & paste your code
*Edited to add the [ code] tags
This post has been edited by pbl: 07 October 2008 - 03:31 PM

New Topic/Question
Reply




MultiQuote




|