Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Class, interface, or enum expected error

807591Jun 18 2008 — edited Jun 18 2008
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;



public class InventoryFinal
{
//main method begins execution of java application
public static void main(final String args[])
{

int i; // varialbe for looping
double total = 0; // variable for total inventory
final int dispProd = 0; // variable for actionEvents

// Instantiate a product object
final ProductAdd[] nwProduct = new ProductAdd[5];
// Instantiate objects for the array
for (i=0; i<5; i++)
{
nwProduct[0] = new ProductAdd("CD", 10, 18, 12.00, "Jewel Case");
nwProduct[1] = new ProductAdd("Blue Ray", 9, 20, 25.00, "HD");
nwProduct[2] = new ProductAdd("Game", 8, 30, 40.00, "Game Case");
nwProduct[3] = new ProductAdd("iPod", 7, 40, 50.00, "Box");
nwProduct[4] = new ProductAdd("DVD", 6, 15, 15.00, "DVD Case");
}



for (i=0; i<5; i++)
total += nwProduct.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 JButton AddBtn = new JButton("Add"); // Add button
final JButton DeleteBtn = new JButton("Delete"); // Delete button
final JButton ModifyBtn = new JButton("Modify"); // Modify button
final JButton SaveBtn = new JButton("Save"); // Save button
final JButton SearchBtn = new JButton("Search"); // Search button
final JLabel label; // logo
final JTextArea textArea; // text area for product list
final JPanel buttonJPanel; // panel to hold buttons

//JLabel constructor for logo
Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
label = new JLabel(logo); // create logo label
label.setToolTipText("Company Logo"); // create tooltip


buttonJPanel = new MyJPanel(); // set up panel
buttonJPanel.setLayout( new GridLayout(1, 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);
buttonJPanel.add(SearchBtn);

textArea = new JTextArea(nwProduct[3]+"\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("CD & DVD Inventory"); // set JFrame title
invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // termination command
//invFrame.pack();
invFrame.setSize(600, 600); // set size of JPanel
invFrame.setLocationRelativeTo(null); // set screen location
invFrame.setVisible(true); // display window



// assign actionListener and actionEvent for each button
firstBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.setText(nwProduct[0]+"\n");
} // end firstBtn actionEvent

}); // end firstBtn actionListener
textArea.setText(nwProduct[4]+"n");

prevBtn.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

textArea.setText(nwProduct[4]+"\n");

} // end prevBtn actionEvent



}); // end prevBtn actionListener

textArea.setText(nwProduct[4]+"n");

nextBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.setText(nwProduct[2]+"\n");
} // end nextBtn actionEvent

}); // end nextBtn actionListener
textArea.setText(nwProduct[4]+"n");

lastBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.setText(nwProduct[3]+"\n");
} // end lastBtn actionEvent

}); // end lastBtn actionListener
textArea.setText(nwProduct[4]+"n");// assign actionListener and actionEvent for each button
AddBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.setText(nwProduct[0]+"\n");
} // end AddBtn actionEvent

}); // end AddBtn actionListener
textArea.setText(nwProduct[4]+"n");

// assign actionListener and actionEvent for each button
DeleteBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.setText(nwProduct[0]+"\n");
} // end DeleteBtn actionEvent

}); // end DeleteBtn actionListener
textArea.setText(nwProduct[4]+"n");

// assign actionListener and actionEvent for each button
ModifyBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.setText(nwProduct[0]+"\n");
} // end ModifyBtn actionEvent

}); // end ModifyBtn actionListener
textArea.setText(nwProduct[4]+"n");

// assign actionListener and actionEvent for each button
SaveBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.setText(nwProduct[0]+"\n");
} // end SaveBtn actionEvent

}); // end SaveBtn actionListener
textArea.setText(nwProduct[4]+"n");

// assign actionListener and actionEvent for each button
SearchBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textArea.setText(nwProduct[0]+"\n");
} // end SearchBtn actionEvent

}); // end SearchBtn actionListener
textArea.setText(nwProduct[4]+"n");

// prevBtn.addActionListener(new ActionListener()

// {
// public void actionPerformed(ActionEvent ae)
// {
// dispProd = (nwProduct.length+dispProd-1) % nwProduct.length;
// textArea.setText(nwProduct.display(dispProd)+"\n");
// } // end prevBtn actionEvent
// }); // end prevBtn actionListener





} // end main

} // end class Inventory4

class Product
{
protected String prodName; // name of product
protected int itmNumber; // item number
protected int units; // number of units
protected double price; // price of each unit
protected double value; // value of total units


public Product(String name, int number, int unit, double each) // Constructor for class Product
{
prodName = name;
itmNumber = number;
units = unit;
price = each;

} // end constructor

public void setProdName(String name) // method to set product name
{
prodName = name;
}

public String getProdName() // method to get product name
{
return prodName;
}

public void setItmNumber(int number) // method to set item number
{
itmNumber = number;
}

public int getItmNumber() // method to get item number
{
return itmNumber;
}

public void setUnits(int unit) // method to set number of units
{
units = unit;
}

public int getUnits() // method to get number of units
{
return units;
}

public void setPrice(double each) // method to set price
{
price = each;
}

public double getPrice() // method to get price
{
return price;
}

public double calcValue() // method to set value
{
return units * price;
}



} // end class Product



class ProductAdd extends Product
{
private String feature; // variable for added feature


public ProductAdd(String name, int number, int unit, double each, String addFeat)
{
// call to superclass Product constructor
super(name, number, unit, each);

feature = addFeat;
}// end constructor

public void setFeature(String addFeat) // method to set added feature
{
feature = addFeat;
}

public String getFeature() // method to get added feature
{
return feature;
}

public double calcValueRstk() // method to set value and add restock fee
{
return units * price * 0.10;
}

public String toString()
{
return String.format("Product: %s\nItem Number: %d\nIn Stock: %d\nPrice: $%.2f\nType: %s\nTotal Value of Stock: $%.2f\nRestock Cost: $%.2f\n\n",
getProdName(), getItmNumber(), getUnits(), getPrice(), getFeature(), calcValue(), calcValueRstk());
}


} // end class ProductAdd

class MyJPanel extends JPanel
{
//private static Random generator = new Random();
private ImageIcon picture; //image to be displayed


// load image
public MyJPanel()
{
picture = new ImageIcon("mypicture.png"); // set icon

} // end MyJPanel constructor

// display imageIcon on panel
public void paintComponent( Graphics g )
{
super.paintComponent( g );
picture.paintIcon( this, g, 0, 0 ); // display icon
} // end method paintComponent

// return image dimensions
//public Dimension getPreferredSize()
{
// return new Dimension ( picture.getIconWidth(),
//picture.getIconHeight() );

} // end method getPreferredSize
} // end class MyJPanel

//

import java.io.File;
import java.io.IOException;

public class FileAccessDemo
{
public static void main( String[] args ) throws IOException
{

// declare variables
String formatStr = "%s exists in %s? %b\n\n";

// processing and output
File file1 = new File( "studentScores.txt" ); // create a File object
System.out.printf
(formatStr, file1.getName(), file1.getAbsolutePath(), file1.exists());

// processing and output
File folder1 = new File( "c:/personnel/" ); // create a File object
folder1.mkdir(); // make a directory
File file2 = new File( "/personnel/faculty.txt" );
file2.createNewFile(); // create a new file
System.out.printf
( formatStr, file2.getName(), file2.getAbsolutePath(), file2.exists() );

// processing and output
file2.delete(); // delete a file, but not the directory
System.out.printf
( formatStr, file2.getName(), file2.getAbsolutePath(), file2.exists() );

} // end main

} // end class


I need help in resolving this error.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 16 2008
Added on Jun 18 2008
8 comments
639 views