Search This Blog

Saturday, August 20, 2011

Need help with my inventory program and coding buttons

Having some issues figuring out how to code my add, modify, search, delete, and save buttons in my program. The buttons need to also adjust the size of the array once I add or delete the an item. Any help would be great. I may be going about this the wrong way entirely but I am very new to Java. These buttons start with line 498


Thank you





package guiinventoryprogram;

/** Program: Inventory Program
* File: InventoryProgram.java
* Summary: Inventory program that gives the name of the movie, the quanity in stock, the price, and the item number
* Author:
* Date: August, 14 2011
*/

import java.awt.GridLayout; // required to create a grid layout

import java.awt.BorderLayout; // required to create a border layout

import java.awt.event.ActionEvent; // required for click events

import java.awt.event.ActionListener; // required for click events

import javax.swing.JPanel; // required to create panels

import javax.swing.JFrame; // required to use the frame

import javax.swing.JButton; // required to create buttons

import javax.swing.JLabel; // required to create text fields

import javax.swing.JTextField; // required to create text fields

import java.util.Arrays; // required to sort array

import java.text.DecimalFormat; // required to display numbers in correct format

import javax.swing.ImageIcon; // required to display image

import javax.swing.JOptionPane; // required to use message dialog box

public class GUIInventoryProgram extends JFrame implements ActionListener
// declare class variables
{

// Declare Class Variables

//Declare two panels. gridPanel is main panel. panel is inserted into gridPanel.

private JPanel gridPanel = new JPanel(); // one panel that contains my gridlayout.

private JPanel panel = new JPanel(); // panel used to contain buttons, labels and text fields

// Declare buttons

JButton firstButton; // first button

JButton nextButton; // next button

JButton previousButton; // previous button

JButton lastButton; // last button

JButton addButton; // add button

JButton deleteButton; // delete button

JButton modifyButton; // modify button

JButton saveButton; // save button

JButton searchButton; // search button

// Declare Text Fields

JTextField TitleField; // Dvd Title

JTextField UnitsField; // Units Field

JTextField ItemNumberField; // Item Number field

JTextField PriceField; // Dvd Price field

JTextField ValueField; // Value field

JTextField DirectorField; // Director field

// Declare Labels

JLabel lblTitle; // Title label

JLabel lblUnits; // Units Label

JLabel lblItemNumber; // Item Number label

JLabel lblPrice; // Price Label

JLabel lblValue; // Value of DVDs Label

JLabel lblDirector; //Director Label

// Declare 5 Dvd Objecs

Dvd dvd1;

Dvd dvd2;

Dvd dvd3;

Dvd dvd4;

Dvd dvd5;

private static final int MAX_dvds = 5; // set maximum size for DVD Array

Dvd[] dvd = new Dvd[MAX_dvds]; // create Dvd Array object

DecimalFormat formatter = new DecimalFormat("$##,###.00"); // to display correct decimal format

private static DecimalFormat currency = new DecimalFormat("$#,##0.00"); // to display currency

private static int ArrayIndex = 0; // array index

public GUIInventoryProgram() // constructor

{

MovieDirector d1 = new MovieDirector("Transformers", 7, 1002, 19.98, "Michael Bay");
// Creates an instance of the DVD class and initialize class instance variables
MovieDirector d2 = new MovieDirector("Green Mile", 3, 542, 14.50, "Frank Darabont");
// Creates an instance of the DVD class and initialize class instance variables
MovieDirector d3 = new MovieDirector("Lord of the Rings", 5, 937, 24.98, "Peter Jackson");
// Creates an instance of the DVD class and initialize class instance variables
MovieDirector d4 = new MovieDirector("Jar Head", 2, 865, 9.75, "Sam Mendes");
// Creates an instance of the DVD class and initialize class instance variables
MovieDirector d5 = new MovieDirector("Inception", 6, 1157, 21.97, "Christopher Nolan");
// Creates an instance of the DVD class and initialize class instance variables

// adding Dvd objects to the array
dvd[0] = d1; // adding Transformers to the array of dvds
dvd[1] = d2; // adding Green Mile to the array of dvds
dvd[2] = d3; // adding Lord of the Rings to the array of dvds
dvd[3] = d4; // adding Jar Head to the array of dvds
dvd[4] = d5; // adding Inception to the array of dvds

gridPanel.setLayout(new BorderLayout()); // create a border layout

gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); // add label panel

gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); // add field panel

gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); // add button panel

add(gridPanel);

}

// helper method creates a panel for the button

private JPanel createButtonPanel()

{

// ActionListener btnListen = new ButtonListener(); // create listener

// create button objects

firstButton = new JButton("First"); // create button object

firstButton.setActionCommand("First"); // add actionCommand

firstButton.addActionListener(this); // add Listener command

nextButton = new JButton("Next"); // create button object

nextButton.setActionCommand("Next"); // add actionCommand

nextButton.addActionListener(this); // add Listener command

previousButton = new JButton("Previous"); // create button object

previousButton.setActionCommand("Previous"); // add actionCommand

previousButton.addActionListener(this); // add Listener command

lastButton = new JButton("Last"); // create button object

lastButton.setActionCommand("Last"); // add actionCommand

lastButton.addActionListener(this); // add Listener command

addButton = new JButton("Add"); // create button object

addButton.setActionCommand("Add"); // add actionCommand

addButton.addActionListener(this); // add Listener command

deleteButton = new JButton("Delete"); // create button object

deleteButton.setActionCommand("Delete"); // add actionCommand

deleteButton.addActionListener(this); // add Listener command

modifyButton = new JButton("Modify"); // create button object

modifyButton.setActionCommand("Modify"); // add actionCommand

modifyButton.addActionListener(this); // add Listener command

saveButton = new JButton("Save"); // create button object

saveButton.setActionCommand("Save"); // add actionCommand

saveButton.addActionListener(this); // add Listener command

searchButton = new JButton("Search"); // create button object

searchButton.setActionCommand("Search"); // add actionCommand

searchButton.addActionListener(this); // add Listener command

// create panel object

JPanel panel = new JPanel();

panel.add(firstButton); // add firt button to panel

panel.add(nextButton); // add next button to panel

panel.add(previousButton); // add previous button to panel

panel.add(lastButton); // add last button to panel

panel.add(addButton); // add the add button to panel

panel.add(deleteButton); // add delete button to panel

panel.add(modifyButton); // add modify button to panel

panel.add(saveButton); // add save button to panel

panel.add(searchButton); // add search button to panel

return panel; // return panel

} // end createButtonPanel method

private JPanel createLabelPanel()

{

// create instance of label objects
ImageIcon icon = new ImageIcon("/Users/garvishopwood/NetBeansProjects/GUIInventoryProgram/src/home.jpg"); // load the image file

JLabel lblLogo = new JLabel(icon); // add the image to the label

lblTitle = new JLabel("Title:"); // label for Title of DVD

lblUnits = new JLabel("Units:"); // label for Units in stock for DVD

lblItemNumber = new JLabel("Item Number:"); // label for DVD Item Number

lblPrice = new JLabel("Price:"); // label for price of DVD

lblValue = new JLabel("Value of Items:"); // label for value of DVD

lblDirector = new JLabel ("Directors Name:"); // label for Director of DVD

// create panel object
panel = new JPanel();

panel.setLayout(new GridLayout(10, 1));

// add labels to the panel
panel.add(lblLogo);

panel.add(lblTitle);

panel.add(lblUnits);

panel.add(lblItemNumber);

panel.add(lblPrice);

panel.add(lblValue);

panel.add(lblDirector);

return panel;

} // end createLabelPanel method

private JPanel createTextPanel()

{

//create instances of text box objects
JLabel lblCompanyName = new JLabel(" DVD Blowout"); // Label for company name

TitleField = new JTextField(); // Title text field

TitleField.setEditable(true); // set field to editable so fields can be added and modified

UnitsField = new JTextField(); // Units text field

UnitsField.setEditable(true); // set field to editable so fields can be added and modified

ItemNumberField = new JTextField(); // Item Number field

ItemNumberField.setEditable(true); // set field to editable so fields can be added and modified

PriceField = new JTextField(); // Price text field

PriceField.setEditable(true); // set field to editable so fields can be added and modified

ValueField = new JTextField(); // Value text field to sum the total of the DVDs value.

ValueField.setEditable(false); // set field to not editable to prevent user from changing the data

DirectorField = new JTextField(); // Director text field

DirectorField.setEditable(true); // set field to editable so fields can be added and modified

panel = new JPanel(); // create panel object

panel.setLayout(new GridLayout(10, 1)); // create grid layout for fields.

panel.add(lblCompanyName); // logo field

panel.add(TitleField); // add the Title field to the grid layout

panel.add(UnitsField); // add the Units field to the grid layout

panel.add(ItemNumberField); // add the Item Number field to the grid layout

panel.add(PriceField); // add the Price field to the grid layout

panel.add(ValueField); // add the total value field to the grid layout

panel.add(DirectorField); // add the director field to the grid layout

return panel; // return the panel

} // end createTextPanel method

@ Override public void actionPerformed(ActionEvent e)

{

Files outputFile = new Files(); // instantiate the Files class
Dvd.sortByTitle(dvd); // sort the array by name of title

// add button functions

if (e.getActionCommand() == "First") // if First button is clicked
ArrayIndex = 0;

{

TitleField.setText(dvd[ArrayIndex].getDvdTitle()); // get the Dvds title

// and assign it the name text field.

UnitsField.setText(String.valueOf(dvd[ArrayIndex].getDvdUnits()));
// get the units in stock and assign it the Units text field.

ItemNumberField.setText(String.valueOf(dvd[ArrayIndex].getDvdItemNumber()));

// get Dvds item number and assign it the item number text field.

PriceField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdPrice()));

// get the Price of the DVD and assign it the price text field.

ValueField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdValue() + dvd[ArrayIndex].getdvdRestockingfee()));
// display the sum of the total of the DVDs in stock

DirectorField.setText(String.valueOf(dvd[ArrayIndex].getdvdDirector()));
// display the Directors name of the DVD

} // end if
if (e.getActionCommand() == "Next") // if Next button is clicked

if (ArrayIndex < dvd.length)

{

TitleField.setText(dvd[ArrayIndex].getDvdTitle()); // get the Dvds title

// and assign it the name text field.

UnitsField.setText(String.valueOf(dvd[ArrayIndex].getDvdUnits()));
// get the units in stock and assign it the Units text field.

ItemNumberField.setText(String.valueOf(dvd[ArrayIndex].getDvdItemNumber()));

// get Dvds item number and assign it the item number text field.

PriceField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdPrice()));

// get the Price of the DVD and assign it the price text field.

ValueField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdValue() + dvd[ArrayIndex].getdvdRestockingfee()));
// display the sum of the total of the DVDs in stock

DirectorField.setText(String.valueOf(dvd[ArrayIndex].getdvdDirector()));
// display the Directors name of the DVD

ArrayIndex = ArrayIndex + 1; // increment the array index to get to the next value

} // end if

if (e.getActionCommand() == "Previous") // if Previous button is clicked

if (ArrayIndex < dvd.length)

{

TitleField.setText(dvd[ArrayIndex].getDvdTitle()); // get the Dvds title

// and assign it the name text field.

UnitsField.setText(String.valueOf(dvd[ArrayIndex].getDvdUnits()));
// get the units in stock and assign it the Units text field.

ItemNumberField.setText(String.valueOf(dvd[ArrayIndex].getDvdItemNumber()));

// get Dvds item number and assign it the item number text field.

PriceField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdPrice()));

// get the Price of the DVD and assign it the price text field.

ValueField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdValue() + dvd[ArrayIndex].getdvdRestockingfee()));
// display the sum of the total of the DVDs in stock

DirectorField.setText(String.valueOf(dvd[ArrayIndex].getdvdDirector()));
// display the Directors name of the DVD

ArrayIndex = ArrayIndex - 1; // increment the array index to get to the previous value

} // end if

if (e.getActionCommand() == "Last") // if Last button is clicked

ArrayIndex = (dvd.length - 1);

{

TitleField.setText(dvd[ArrayIndex].getDvdTitle()); // get the Dvds title

// and assign it the name text field.

UnitsField.setText(String.valueOf(dvd[ArrayIndex].getDvdUnits()));
// get the units in stock and assign it the Units text field.

ItemNumberField.setText(String.valueOf(dvd[ArrayIndex].getDvdItemNumber()));

// get Dvds item number and assign it the item number text field.

PriceField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdPrice()));

// get the Price of the DVD and assign it the price text field.

ValueField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdValue() + dvd[ArrayIndex].getdvdRestockingfee()));
// display the sum of the total of the DVDs in stock

DirectorField.setText(String.valueOf(dvd[ArrayIndex].getdvdDirector()));
// display the Directors name of the DVD
}

if (e.getActionCommand() == "Add") // if Add button is clicked

{
String dvdTitle = JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter the dvd's name");
int dvdUnits = Integer.parseInt(JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter the units in stock"));
Double dvdItemNumber = Double.parseDouble(JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter the Item number for the DVD"));
double dvdPrice = Double.parseDouble(JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter the price of each item"));
String dvdDirector = JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter the director");

// make the object
MovieDirector Dvd = new MovieDirector(Title, Units, Item Number, Price, Director);

// put it in the inv, at the very end
dvd[ArrayIndex].setDvdTitle(TitleField.getText());
dvd[ArrayIndex].setDvdUnits(UnitsField.getText());// get the value from the text field and assign it to the array.

}

if (e.getActionCommand() == "Delete") // if Delete button is clicked

{

// code here

}

if (e.getActionCommand() == "Modify") // if Modify button is clicked

}
MovieDirector Dvd = inv.get(view);

Dvd.setDvdTitle(JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter new item name",dvd.getDvdTitle()));
Dvd.setDvdUnits(Integer.parseInt(JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter new units in stock",dvd.getDvdUnits())));
Dvd.setDvdItemNumber(Integer.parseInt(JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter new item number",dvd.getDvdItemNumber())));
Dvd.setDvdPrice(Double.parseDouble(JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter new price of each item",dvd.getDvdPrice())));

if (dvd instanceof MovieDirector)
}
((MovieDirector)dvd).setdvdDirector(
JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter new director",((MovieDirector)dvd).getdvdDirector()));
}
}

if (e.getActionCommand() == "Search") // if Search button is clicked

{

String str = JOptionPane.showInputDialog(GUIInventoryProgram.this,"Enter the name to search for");
int result = inv.search(str);
if (result == -1) JOptionPane.showMessageDialog(GUIInventoryProgram.this,"Not found"); // do nothing
else { // show the item
view = result;
showDvd();

}

if (e.getActionCommand() == "Save") // if Save button is clicked

{

try

{

outputFile.appendToFile(dvd[ArrayIndex].getDvdTitle());

outputFile.appendToFile(", ");

outputFile.appendToFile(String.valueOf(dvd[ArrayIndex].getDvdUnits()));

outputFile.appendToFile(", ");

outputFile.appendToFile(String.valueOf(dvd[ArrayIndex].getDvdItemNumber()));

outputFile.appendToFile(", ");

outputFile.appendToFile(String.format("$ %8.2f", dvd[ArrayIndex].getDvdPrice()));

outputFile.appendToFile(", ");

outputFile.appendToFile(String.format("$ %8.2f", dvd[ArrayIndex].getDvdValue() + dvd[ArrayIndex].getdvdRestockingfee()));

outputFile.appendToFile(", ");

outputFile.appendToFile(String.valueOf(dvd[ArrayIndex].getdvdDirector()));

outputFile.appendToFile(", ");

JOptionPane.showMessageDialog(null, dvd[ArrayIndex].getDvdTitle() + " has been saved to the file."); // message indicating the file has been saved

}

catch (Exception ex)

{//Catch exception if any

JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage()); // catch errors that can occur

}

} // end if

} // end actionPerformed

public static void main(String[] args)

{

JFrame frame = new GUIInventoryProgram(); // creat a frame object

frame.setSize(800, 400); // set the size of the window - 400 is width, 400 is height

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close the application

frame.setTitle("DVD Inventory"); // set the title of the window

frame.setLocationRelativeTo( null ); // center the form

frame.setVisible(true); // display form

} // end main

} // end GUIInventoryProgram class