I have a program i have been working on for my Java class and it is due by the end of this week. My problem is a few things...(1) I do not know how to set the actions on the buttons Save, Delete, Modify and Add. Also I was able to get the previous button to work, but it will only go as far as the first item instead of continuing on through the list. (2) I need to add a logo..have no clue how to do this. (3) I need it to be able to save to a C:/data.file and well do not know how to do that either. Any help on getting these things started would be greatly appreciated.
This is the final requirements i was given....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.
This is the code i have done so far...
import java.util.Arrays; // program uses arrays
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; // Import the java swing package
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class Inventory5 {
// main method begins execution of java application
public static void main(String[] args) {
System.out.println("\nCheckPoint: Inventory Part 5");
System.out.println("My DVD Collection\n");
Movie dvd = null;
Inventory inventory = new Inventory();
dvd = new Movie(1, "Live Free or Die Hard", 18, 19.99f, 2001);
System.out.println(dvd);
inventory.addMovie(dvd);
dvd = new Movie(2, "The Patriot", 14, 20.99f, 1998);
System.out.println(dvd);
inventory.addMovie(dvd);
dvd = new Movie(3, "Full Metal Jacket", 1, 21.99f, 1995);
System.out.println(dvd);
inventory.addMovie(dvd);
dvd = new Movie(4, "Coyote Ugly", 15, 18.99f, 2003);
inventory.addMovie(dvd);
System.out.println(dvd);
dvd = new Movie(5, "Apocalypto", 10, 21.99f, 2006);
System.out.println(dvd);
inventory.addMovie(dvd);
dvd = new Movie(6, "Monster in Law", 1, 7.99f, 2005);
System.out.println(dvd);
System.out.println("\nEnd of DVD collection!\n");
inventory.addMovie(dvd);
inventory.printInventory();
new InventoryGUI(inventory);
} // end main
} // end Inventory5
class DVD {
private int itemNo;
private String title;
private int inStock;
private float unitPrice;
DVD(int itemNo, String title, int inStock, float unitPrice) {
this.itemNo = itemNo;
this.title = title;
this.inStock = inStock;
this.unitPrice = unitPrice;
}
public int getItemNo() { return itemNo; }
public String getTitle() { return title; }
public int getInStock() { return inStock; }
public float getUnitPrice() { return unitPrice; }
public float value() {
return inStock * unitPrice;
}
public String toString() {
return String.format("itemNo=%2d title=%-22s inStock=%3d price=$%7.2f value=$%8.2f",
itemNo, title, inStock, unitPrice, value());
}
} // end DVD
class Inventory {
// Setup an array of Movies (set it to hold 30 items)
private final int INVENTORY_SIZE = 30;
private DVD[] items;
private int numItems;
Inventory() {
items = new Movie[INVENTORY_SIZE];
numItems = 0;
}
public int getNumItems() {
return numItems;
}
public DVD getDVD(int n) {
return items[n];
}
// Adds a Movie to the array of Movies. Adds to first empty slot found.
public void addMovie(DVD item) {
items[numItems] = item;
++numItems;
}
// Loop through our array of Movies and add up the total value.
// Go item by item adding the quantity on hand x its price.
// Add that value to a running total accumulator variable.
public double value() {
double sumOfInventory = 0.0;
for (int i = 0; i < numItems; i++)
sumOfInventory += items[i].value();
return sumOfInventory;
}
// Prints the inventory list including name, quantity, price,
// and total stock value for each item.
public void printInventory() {
System.out.println("\nShelly's DVD Inventory\n");
// If no items were found, print a message saying the inventory is empty.
if (numItems <= 0) {
System.out.println("Inventory is empty at the moment.\n");
} else {
for (int i = 0; i < numItems; i++)
System.out.printf("%3d %s\n", i, items[i]);
System.out.printf("\nTotal value in inventory is $%,.2f\n\n", value());
}
}
} // end Inventory
// Extends DVD class from the base class DVD
class Movie extends DVD {
// Holds movie year and adds restocking fee
private int movieYear;
// Constructor, calls the constructor of Movie first
public Movie(int MovieID, String itemName, int quantityOnHand, float itemPrice, int year) {
super(MovieID, itemName, quantityOnHand, itemPrice);
// Pass on the values needed for creating the Movie class first thing
this.movieYear = movieYear;
}
// To set the year manually
public void setYear(int year) {
movieYear = year;
}
// Get the year of this DVD Movie
public int getMovieYear() {
return movieYear;
}
// Overrides value() in Movie class by calling the base class version and
// adding a 5% restocking fee on top
public float value() {
return super.value() + restockingFee();
}
// Simply gets the base class's value, and figures out the 5% restocking fee only
public float restockingFee() {
return super.value() * 0.05f;
}
} // end Movie
// GUI for the Inventory
// Contains an inventory of DVD's and lets the user step through them one by one
class InventoryGUI extends JFrame
{
// access inventory for DVD Collection
private Inventory theInventory;
// index in the inventory of the currently displayed DVD.
// the index starts at 0, goes to the number of DVDs in the inventory minus 1
private int index = 0;
// GUI elements to display currently selected DVD information
private final JLabel itemNumberLabel = new JLabel(" Item Number:");
private JTextField itemNumberText;
private final JLabel prodnameLabel = new JLabel(" Product Name:");
private JTextField prodnameText;
private final JLabel prodpriceLabel = new JLabel(" Price:");
private JTextField prodpriceText;
private final JLabel numinstockLabel = new JLabel(" Number in Stock:");
private JTextField numinstockText;
private final JLabel valueLabel = new JLabel(" Value:");
private JTextField valueText;
private final JLabel restockingFeeLabel = new JLabel(" Restocking Fee:");
private JTextField restockingFeeText;
private final JLabel totalValueLabel = new JLabel(" Inventory Total Value:");
private JTextField totalValueText;
private JPanel centerPanel;
private JPanel buttonPanel;
// constructor for the GUI, in charge of creating all GUI elements
InventoryGUI(Inventory inventory) {
super("Shelly's Movie Inventory");
final Dimension dim = new Dimension(140, 20);
final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
JPanel jp;
// create the inventory object that will hold the product information
theInventory = inventory;
// setup the GUI
// product information
// setup a panel to collect all the components.
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
buttonPanel = new JPanel();
JButton firstButton = new JButton("First");
firstButton.addActionListener(new FirstButtonHandler());
buttonPanel.add(firstButton);
JButton previousButton = new JButton("Previous");
previousButton.addActionListener(new PreviousButtonHandler());
buttonPanel.add(previousButton);
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new NextButtonHandler());
buttonPanel.add(nextButton);
JButton lastButton = new JButton("Last");
lastButton.addActionListener(new LastButtonHandler());
buttonPanel.add(lastButton);
JButton addButton = new JButton("Add");
addButton.addActionListener(new AddButtonHandler());
buttonPanel.add(addButton);
JButton deleteButton = new JButton("Delete");
deleteButton.addActionListener(new DeleteButtonHandler());
buttonPanel.add(deleteButton);
JButton modifyButton = new JButton("Modify");
modifyButton.addActionListener(new ModifyButtonHandler());
buttonPanel.add(modifyButton);
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new SaveButtonHandler());
buttonPanel.add(saveButton);
JButton searchButton = new JButton("Search");
searchButton.addActionListener(new SearchButtonHandler());
buttonPanel.add(searchButton);
centerPanel.add(buttonPanel);
jp = new JPanel(flo);
itemNumberLabel.setPreferredSize(dim);
jp.add(itemNumberLabel);
itemNumberText = new JTextField(3);
itemNumberText.setEditable(false);
jp.add(itemNumberText);
centerPanel.add(jp);
jp = new JPanel(flo);
prodnameLabel.setPreferredSize(dim);
jp.add(prodnameLabel);
prodnameText = new JTextField(17);
prodnameText.setEditable(false);
jp.add(prodnameText);
centerPanel.add(jp);
jp = new JPanel(flo);
prodpriceLabel.setPreferredSize(dim);
jp.add(prodpriceLabel);
prodpriceText = new JTextField(17);
prodpriceText.setEditable(false);
jp.add(prodpriceText);
centerPanel.add(jp);
jp = new JPanel(flo);
numinstockLabel.setPreferredSize(dim);
jp.add(numinstockLabel);
numinstockText = new JTextField(5);
numinstockText.setEditable(false);
jp.add(numinstockText);
centerPanel.add(jp);
jp = new JPanel(flo);
restockingFeeLabel.setPreferredSize(dim);
jp.add(restockingFeeLabel);
restockingFeeText = new JTextField(17);
restockingFeeText.setEditable(false);
jp.add(restockingFeeText);
centerPanel.add(jp);
jp = new JPanel(flo);
valueLabel.setPreferredSize(dim);
jp.add(valueLabel);
valueText = new JTextField(17);
valueText.setEditable(false);
jp.add(valueText);
centerPanel.add(jp);
// add the overall inventory information to the panel
jp = new JPanel(flo);
totalValueLabel.setPreferredSize(dim);
jp.add(totalValueLabel);
totalValueText = new JTextField(17);
totalValueText.setEditable(false);
jp.add(totalValueText);
centerPanel.add(jp);
// add the panel to the GUI display
setContentPane(centerPanel);
repaintGUI();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(420, 480);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
// (re)display the GUI with current product's information
public void repaintGUI() {
Movie temp = (Movie) theInventory.getDVD(index);
if (temp != null) {
itemNumberText.setText("" + temp.getItemNo());
prodnameText.setText(temp.getTitle());
prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice()));
restockingFeeText.setText(String.format("$%.2f", temp.restockingFee()));
numinstockText.setText("" + temp.getInStock());
valueText.setText(String.format("$%.2f", temp.value()));
}
totalValueText.setText(String.format("$%.2f", theInventory.value()));
}
class FirstButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
index = 0;
repaintGUI();
}
}
class PreviousButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
int numItems = theInventory.getNumItems();
index = (--index) % numItems;
repaintGUI();
}
}
class NextButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
int numItems = theInventory.getNumItems();
index = (++index) % numItems;
repaintGUI();
}
}
class LastButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
int numItems = theInventory.getNumItems();
index = (numItems -1) % numItems;
repaintGUI();
}
}
class AddButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}
class SaveButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}
class ModifyButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}
class DeleteButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}
class SearchButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
repaintGUI();
}
}
} // End InventoryGUI class