Many of you may have seen this before. I am really struggling with this class. Here is my current assignment:
Modify the Inventory Program to include an 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. An
item added to the inventory should have an item number one more than the previous last
item.
· 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.
· Add a search button to the GUI that allows the user to search for an item in the inventory
by the product name. If the product is not found, the GUI should display an appropriate
message. If the product is found, the GUI should display that product’s information in the
GUI.
I pretty much understand the adding new buttons, but I am lost on how to make them work. Particularly the modify and the save buttons. So lets just start with those and I can figure out how to do the add and delete. I dont want anyone to do it for me but rather help me get started. Please will someone point me in the right direction.
CODE
package Inventory2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Inventory2 {
static int displayDVD = 0; // variable for actionEvents
static JTextArea textArea;
public static void main(String args []) {
System.out.print( "Welcome to the DVD Program!!");
int i; // varialbe for looping
double total = 0; // variable for total inventory
final DVD[] DVD = new DVD[4];
// Instantiate objects for the array
for (i=0; i<4; i++)
{
DVD[0] = new DVD(1, "Transformers", 53, 15.99);
System.out.println(DVD[0]);
DVD[1] = new DVD(2, "Iron Man", 32, 15.99);
System.out.println(DVD[1]);
DVD[2] = new DVD(3, "The Love Guru", 26, 15.99);
System.out.println(DVD[2]);
DVD[3] = new DVD(4, "Street Kings", 30, 15.99);
System.out.println(DVD[3]);
}
for (i=0; i<4; i++)
total += DVD.length; // calculate total inventory cost
final JButton firstBtn = new JButton("First"); // first button
final JButton prevBtn = new JButton("Previous"); // previous button
final JButton nextBtn = new JButton("Next"); // next button
final JButton lastBtn = new JButton("Last"); // last button
final JLabel label; // logo
final JTextArea textArea; // text area for product list
final JPanel buttonJPanel; // panel to hold buttons
final JButton addBtn = new JButton("Add");
final JButton deleteBtn = new JButton("Delete");
final JButton modifyBtn = new JButton("Modify");
final JButton saveBtn = new JButton ("Save");
//JLabel constructor for logo
Icon logo = new ImageIcon("C://logo.jpg"); // load logo
label = new JLabel(logo); // create logo label
label.setToolTipText("The Logo"); // create tooltip
buttonJPanel = new JPanel(); // set up panel
buttonJPanel.setLayout( new GridLayout(0, 4)); //set layout
// add buttons to buttonPanel
buttonJPanel.add(firstBtn);
buttonJPanel.add(prevBtn);
buttonJPanel.add(nextBtn);
buttonJPanel.add(lastBtn);
buttonJPanel.add(addBtn);
buttonJPanel.add(deleteBtn);
buttonJPanel.add(modifyBtn);
buttonJPanel.add(saveBtn);
textArea = new JTextArea(DVD[0]+"\n"); // create textArea for product display
// add total inventory value to GUI
textArea.append("\nTotal value of Inventory "+new java.text.DecimalFormat("$0.00").format(total)+"\n\n");
textArea.setEditable(false); // make text uneditable in main display
JFrame invFrame = new JFrame(); // create JFrame container
invFrame.setLayout(new BorderLayout()); // set layout
invFrame.getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER); // add textArea to JFrame
invFrame.getContentPane().add(buttonJPanel, BorderLayout.SOUTH); // add buttons to JFrame
invFrame.getContentPane().add(label, BorderLayout.NORTH); // add logo to JFrame
invFrame.setTitle("DVD Inventory"); // set JFrame title
invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // termination command
//invFrame.pack();
invFrame.setSize(400, 400); // set size of JPanel
invFrame.setLocationRelativeTo(null); // set screem location
invFrame.setVisible(true); // display window
// assign actionListener and actionEvent for each button
firstBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
displayDVD = 0;
textArea.setText(DVD[displayDVD]+"\n");
} // end firstBtn actionEvent
}); // end firstBtn actionListener
prevBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
--displayDVD;
if(displayDVD < 0)
displayDVD = DVD.length-1;
textArea.setText(DVD[displayDVD]+"\n");
} // end prevBtn actionEvent
}); // end prevBtn actionListener
lastBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
displayDVD = 3;
textArea.setText(DVD[displayDVD]+"\n");
} // end lastBtn actionEvent
}); // end lastBtn actionListener
nextBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int length = DVD.length;
displayDVD = (++displayDVD) % length;
textArea.setText(DVD[displayDVD]+"\n");
} // end nextBtn actionEvent
}); // end nextBtn actionListener
addBtn.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent ae){
DVD[DVD.length].addToStock();
}
});
deleteBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
}
});
} // end main
}//end class inventory2
class DVD {
private int ItemNumber;
private String Title;
private int Stock;
private double Price;
public DVD(int itemNumber, String title, int stock, double price)
{
//begin 4-argument constructor
ItemNumber = itemNumber;
Title = title;
Stock = stock;
Price = price;
} //end 4-argument constructor
// set ItemNumber
public void setItemNumber(int itemNumber)
{
ItemNumber = itemNumber;
} //end method
//return ItemNumber
public int getItemNumber()
{
return ItemNumber;
} //end
//set Title
public void setTitle(String title)
{
Title = title;
} //end
//return Title
public String getTitle()
{
return Title;
} //end
//set stock
public void setStock(int stock)
{
Stock = stock;
} //end
//return Stock
public int getStock()
{
return Stock;
} //end
//set price
public void setPrice(double price)
{
Price = price;
} //end
//return Price
public double getPrice()
{
return Price;
} //end
//calculate inventory value
public double Value()
{
return Price * Stock;
} //end
public void addToStock(){
}
@Override
public String toString() {
return String.format( " Item Number = %3d\n Title = %-20s\n In Stock = %d\n " +
"Price = $%.2f\n Value = $%.2f\n\n\n ",
ItemNumber, Title, Stock, Price, Value() );
}
} //end class DVD