Ran into the same problem, but this time I cannot figure out what i'm doing wrong. I added text boxes this time to display information instead of the console, and now it cannot find the information. I'm getting "cannot find symbol" for DirectorField.setText(dvd[ArrayIndex].get_director()); and RestockingFeeField.setText(String.valueOf(dvd[ArrayIndex].get_restocking_fee())); It's probably something silly I am overlooking like before.
Code
/** Program: Inventory Program Part 4
* File: Inventory_Program.java
* Summary: Sets, retrieves and displays DVD stock and value.
* Author: Dale R Murray
* Date: October 25 , 2013
**/
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Inventory_Program extends JFrame implements ActionListener
{
private JPanel gridPanel = new JPanel();
private JPanel panel = new JPanel();
//Declare buttons
JButton nextButton;
//Declare Text Fields
JTextField DirectorField;
JTextField ProductNumberField;
JTextField ProductNameField;
JTextField UnitsInStockField;
JTextField PricePerUnitField;
JTextField RestockingFeeField;
JTextField ValueOfInventoryField;
JTextField ValueOfInventoryTotal;
//Declare Labels
JLabel lblDirector;
JLabel lblProductNumber;
JLabel lblProductName;
JLabel lblUnitsInStock;
JLabel lblPricePerUnit;
JLabel lblRestockingFee;
JLabel lblValueOfInventory;
JLabel lblValueOfInventoryTotal;
DVD dvd1;
DVD dvd2;
DVD dvd3;
DVD dvd4;
DVD dvd5;
final int ARRAY_LENGTH = 5;
DVD dvd[] = new DVD[ARRAY_LENGTH];//Create an array of Person objects
private static int ArrayIndex = 0;
public Inventory_Program()
{
DVD dvd1 = new DVD_Director (01, " The Machinist", 25, 12.99, "Brad Anderson");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd2 = new DVD_Director (02, " X-Men:Last Stand", 16, 19.00, "Brett Ratner");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd3 = new DVD_Director (03, " The Dark Knight", 52, 19.99, "Christopher Nolan");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd4 = new DVD_Director (04, " Forrest Gump", 9, 9.99, "Robert Zemeckis");//Creates an instance of the DVD class and initialize class instance variables
DVD dvd5 = new DVD_Director (05, " A Walk To Remember", 12, 5.00, "Adam Shankman");//Creates an instance of the DVD class and initialize class instance variables
dvd[0] = dvd1;//Add The Machinist DVD to the array of DVDs
dvd[1] = dvd2;//Add X-Men:Last Stand DVD to the array of DVDs
dvd[2] = dvd3;//Add The Dark Knight DVD to the array of DVDs
dvd[3] = dvd4;//Add Forest Gump DVD to the array of DVDs
dvd[4] = dvd5;//Add A Walk To Remember DVD to the array of DVDs
DVD.sort_by_name(dvd);//Call the sort_by_name method
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);
}
private JPanel createButtonPanel()
{
nextButton = new JButton("Display Inventory");
nextButton.setActionCommand("Next");
nextButton.addActionListener(this);
// create panel object
JPanel panel = new JPanel();
panel.add(nextButton);
return panel;
}
private JPanel createLabelPanel()
{
lblDirector = new JLabel("Director:");
lblProductNumber = new JLabel("Product Number:");
lblProductName = new JLabel("Product Name:");
lblUnitsInStock = new JLabel("Units in Stock:");
lblPricePerUnit = new JLabel("Price per Unit:");
lblRestockingFee = new JLabel ("Restocking Fee:");
lblValueOfInventory = new JLabel("Value of Inventory:");
lblValueOfInventoryTotal = new JLabel("Total Value:");
panel = new JPanel();
panel.setLayout(new GridLayout(8, 1));
// add labels to the panel
panel.add(lblDirector);
panel.add(lblProductNumber);
panel.add(lblProductName);
panel.add(lblUnitsInStock);
panel.add(lblPricePerUnit);
panel.add(lblRestockingFee);
panel.add(lblValueOfInventory);
panel.add(lblValueOfInventoryTotal);
return panel;
}
private JPanel createTextPanel()
{
DirectorField = new JTextField();//Director text field
DirectorField.setEditable(false);//Set field to read-only
ProductNumberField = new JTextField();//Product Number text field
ProductNumberField.setEditable(false);//Set field to read-only
ProductNameField = new JTextField();//Product Name text field
ProductNameField.setEditable(false);//Set field to read-only
UnitsInStockField = new JTextField();//Units in stock text field
UnitsInStockField.setEditable(false);//Set field to read-only
PricePerUnitField = new JTextField();//Price per unit text field
PricePerUnitField.setEditable(false);//Set field to read-only
RestockingFeeField = new JTextField();//Restocking fee unit text field
RestockingFeeField.setEditable(false);//Set field to read-only
ValueOfInventoryField = new JTextField();//Value of inventory field
ValueOfInventoryField.setEditable(false);//Set field to read-only
ValueOfInventoryTotal = new JTextField();//Total value of entire inventory field
ValueOfInventoryTotal.setEditable(false);//Set field to read-only
panel = new JPanel();
panel.setLayout(new GridLayout(8, 1));
panel.add(DirectorField);
panel.add(ProductNumberField);
panel.add(ProductNameField);
panel.add(UnitsInStockField);
panel.add(PricePerUnitField);
panel.add(RestockingFeeField);
panel.add(ValueOfInventoryField);
panel.add(ValueOfInventoryTotal);
return panel; // return the panel
}
public void actionPerformed(ActionEvent e)
{
// add button functions
if (e.getActionCommand() == "Next")
if (ArrayIndex < dvd.length)
{
DirectorField.setText(dvd[ArrayIndex].get_director());
ProductNumberField.setText(String.valueOf(dvd[ArrayIndex].get_item_number()));
ProductNameField.setText(dvd[ArrayIndex].get_product_name());
UnitsInStockField.setText(String.valueOf(dvd[ArrayIndex].get_units_in_stock()));
PricePerUnitField.setText(String.valueOf(dvd[ArrayIndex].get_price_per_unit()));
RestockingFeeField.setText(String.valueOf(dvd[ArrayIndex].get_restocking_fee()));
ValueOfInventoryField.setText(String.valueOf(dvd[ArrayIndex].get_inventory_value()));
double total = 0.0;
for (int i = 0; i < 5; i++)
{
total += dvd[i].get_inventory_value();//A running total of get_inventory_value
}
ValueOfInventoryTotal.setText(String.valueOf(+total));
ArrayIndex = ArrayIndex + 1;
}
}
public static void main(String[] args)
{
JFrame frame = new Inventory_Program();
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Inventory Application");
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}