Hello I very new to Java, and I am having problems with this program I can not get it to compile or run. Can someone please help me figure this out it for a class I am taking
Here is the code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Chris
*/
// Program uses class Scanner to accept user input
// Program Uses Class NumberFormat to format number output
// Program Needs to Use U.S. Currency
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InventoryProgram4
{
private int MaxNumberOfCameras = 100;
// array of cameras form the inventory
private Camera myCamera[] = new Camera[MaxNumberOfCameras]; // Create a camera array
// number of cameras that the user has stored
private int numberOfCameras = 0;
// location of the camera in the array that is on the screen
private int index = 0;
// Set Currency to U.S. Dollars
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
// GUI elements to display currently selected camera's information
private JLabel cameraIDLabel;
private JTextField cameraIDText;
private JLabel cameraNameLabel;
private JTextField cameraNameText;
private JLabel cameraTypeLabel;
private JTextField cameraTypeText;
private JLabel cameraWeightLabel;
private JTextField cameraWeightText;
private JLabel numberInStockLabel;
private JTextField numberInStockText;
private JLabel cameraPriceLabel;
private JTextField cameraPriceText;
private JLabel cameraRestockingFeeLabel;
private JTextField cameraRestockingFeeText;
private JLabel cameraInventoryValueLabel;
private JTextField cameraInventoryValueText;
private JLabel entireInventoryValueLabel;
private JTextField entireInventoryValueText;
private JFrame cameraGUIFrame;
// setup buttons
// button to display next inventory item
JButton nextButton;
public InventoryProgram4() {
// setup the element of the GUI
cameraNameLabel = new JLabel("Name:");
cameraIDLabel = new JLabel("ID:");
cameraTypeLabel = new JLabel("Type:");
cameraWeightLabel = new JLabel("Weight:");
numberInStockLabel = new JLabel("Unit in Stock:");
cameraPriceLabel = new JLabel("Unit Price:");
cameraRestockingFeeLabel = new JLabel("Restocking fee:");
cameraInventoryValueLabel = new JLabel("Camera Inventory Value");
entireInventoryValueLabel = new JLabel("Overall Camera Inventory Value");
entireInventoryValueText = new JTextField("");
entireInventoryValueText.setColumns(30);
entireInventoryValueText.setEditable(false);
cameraInventoryValueText = new JTextField("");
cameraInventoryValueText.setEditable(false);
cameraRestockingFeeText = new JTextField("");
cameraRestockingFeeText.setEditable(false);
cameraPriceText = new JTextField("");
cameraPriceText.setEditable(false);
numberInStockText = new JTextField("");
numberInStockText.setEditable(false);
cameraWeightText = new JTextField("");
cameraWeightText.setEditable(false);
cameraTypeText = new JTextField("");
cameraTypeText.setEditable(false);
cameraNameText = new JTextField("");
cameraNameText.setEditable(false);
cameraIDText = new JTextField("");
cameraIDText.setColumns(30);
cameraIDText.setEditable(false);
// setup the code for the buttons
nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() // register event handler
{
public void actionPerformed(ActionEvent event) // process button event
{
// update the current item to be displayed by the inventory
index++;
// if we've gone forward back too far, then wrap around
if ( index == numberOfCameras ) {
index = 0;
}
repaint();
}
});
cameraGUIFrame = new JFrame(); // JFrame container
cameraGUIFrame.setLayout(new BorderLayout()); // set layout
// create and setup panel to hold buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4, 0, 4));
buttonPanel.add(nextButton);
JPanel entireInventoryValuePanel = new JPanel();
entireInventoryValuePanel.add(entireInventoryValue Label);
entireInventoryValuePanel.add(entireInventoryValue Text);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(2, 1));
infoPanel.add(buttonPanel);
infoPanel.add(entireInventoryValuePanel);
// setup a panel to collect all the components.
// this will have 8 rows and 2 columns
JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));
centerPanel.add(cameraNameLabel);
centerPanel.add(cameraNameText);
centerPanel.add(cameraIDLabel);
centerPanel.add(cameraIDText);
// row 2
centerPanel.add(cameraTypeLabel);
centerPanel.add(cameraTypeText);
// row 3
centerPanel.add(cameraWeightLabel);
centerPanel.add(cameraWeightText);
// row 4
centerPanel.add(numberInStockLabel);
centerPanel.add(numberInStockText);
// row 5
centerPanel.add(cameraPriceLabel);
centerPanel.add(cameraPriceText);
// row 6
centerPanel.add(cameraRestockingFeeLabel);
centerPanel.add(cameraRestockingFeeText);
// row 7
centerPanel.add(cameraInventoryValueLabel);
centerPanel.add(cameraInventoryValueText);
// add the panel to the center of the GUI's window
cameraGUIFrame.getContentPane().add(centerPanel, BorderLayout.CENTER);
// adds buttons to frame
cameraGUIFrame.getContentPane().add(infoPanel, BorderLayout.SOUTH);
// title of window
cameraGUIFrame.setTitle("Camera Inventory");
// quit the program when the window is closed
cameraGUIFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
// Show Program Welcome Message
System.out.println( ); // Show an Empty Line
System.out.println( "Welcome to the Inventory Program!!!"); // Show Welcome Message
System.out.println( ); // Show an Empty Line
// Prompt for the number of cameras
System.out.print( "Please enter the number of digital cameras: "); // prompt message
numberOfCameras = input.nextInt(); // read the number of cameras
input.nextLine(); // Need this to get leftover carriage return --> input queue cleanup
// Loop for Camera Data Entry
for (int i = 0; i < numberOfCameras; i++)
{
myCamera[i] = new Camera(); // Instantiate the object --> Create object memory space
// Show an Empty Line
System.out.println( );
// Prompt for Camera Type
System.out.print( "Enter a Camera #" + (i+1) + " Name: " ); // prompt message
myCamera[i].setName(input.nextLine()); // read the camera name
// Prompt for Camera ID
System.out.print( "Enter an ID for Camera #" + (i+1) + ": " ); // prompt message
myCamera[i].setID(input.nextLine()); // read the camera ID
// Prompt for Camera Type
System.out.print( "Enter the type for Camera #" + (i+1) + ": " ); // prompt message
myCamera[i].setType(input.nextLine()); // read the camera type
// Prompt for Camera Weight
System.out.print( "Enter the weight for Camera #" + (i+1) + ": " ); // prompt message
myCamera[i].setWeight(input.nextFloat()); // read the camera weight
// Prompt for Camera's Unit Price
System.out.print( "Enter the Unit Price for Camera #" + (i+1) + " ($): " ); // prompt message
myCamera[i].setUnitPrice(input.nextFloat()); // read the camera's unit price
// Prompt for Camera's Stock Inventory
System.out.print( "Enter the Number of Camera#" + (i+1) + " Units in Stock: " ); // prompt message
myCamera[i].setUnitsInStock(input.nextLong()); // read the number Camera's in stock
input.nextLine(); // Need this to get leftover carriage return --> input queue cleanup
}
sortCameraInventory(myCamera, numberOfCameras);
// display the GUI on screen
cameraGUIFrame.pack();
cameraGUIFrame.setVisible(true);
repaint();
}
// each time the program data is modified, called repaint() to get the most recent information on the screen
public void repaint() {
// display the current item on the screen's text area
// get the Camrea at the current index
Camera theCamera = myCamera[index];
// print out its information to the screen
if (theCamera != null) {
cameraInventoryValueText.setText(currency.format(t heCamera.getTotalInventoryValue()));
cameraRestockingFeeText.setText("" + currency.format(theCamera.getUnitPrice() * theCamera.getRestockingFee()/100));
cameraPriceText.setText(currency.format(theCamera. getUnitPrice()));
numberInStockText.setText("" + theCamera.getUnitsInStock());
cameraWeightText.setText("" + theCamera.getWeight());
cameraTypeText.setText("" + theCamera.getType());
cameraNameText.setText(theCamera.getName());
cameraIDText.setText("" + theCamera.getID());
}
entireInventoryValueText.setText(currency.format( getTotalInventoryValue(myCamera, numberOfCameras) ) );
}
// Method that Computes the Overall Value of a Camera Inventory Array
public static float getTotalInventoryValue(Camera cameraInventory[], int numberOfCameras)
{
float totalInventoryValue = 0; // Placeholder for the overall inventory value
// Loop through all the cameras in the supplied inventory array
for (int i = 0; i < numberOfCameras; i++)
{
totalInventoryValue = totalInventoryValue + cameraInventory[i].getTotalInventoryValue(); // Add the total value of a camera to the overall camera inventory value
}
return totalInventoryValue; // Return the overall camera inventory value for the supplied camera array
} // end getTotalInventoryValue method
// Method that Sorts the Camera Inventory Based on the Camera Name
public static void sortCameraInventory(Camera cameraInventory[], int numberOfCameras)
{
// Temporary Camera Placeholder
Camera tempCamera = new Camera();
// Loop that specifies the last element in the sorting comparisons
for (int j = numberOfCameras; j > 0; j--)
{
// Loop throught the camera elements
for (int i = 0; i < (j-1); i++)
{
// Compare the names of the camera elements and swap them, if necessary, to get them in the correct order
if ( cameraInventory[i].getName().compareToIgnoreCase( cameraInventory[i+1].getName() ) > 0 )
{
// Swap the two camera positions in the camera inventory array
tempCamera = cameraInventory[i+1]; // Temporarily store camera i+1 in tempCamera
cameraInventory[i+1] = cameraInventory[i]; // Replace the "i+1" camera element with the "i" camera element
cameraInventory[i] = tempCamera; // Set the "i" camera element to the previous "i+1" camera element value
} // end if statement
} // end for loop
} // end for loop
} // end sortCameraInventory method
// main method begins execution of Java application
public static void main( String args[] )
{
new InventoryProgram4();
} // end method main
} //end class InventoryProgram4
