I have completed most of the code for my final assignment for my Java class but am having some difficulties getting it to compile. The requirements for the assignment are:
• 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.
Here is my current code. Any help getting it to compile would be greatly appreciated!
*****************DVDMovie******************
import java.text.NumberFormat;
/**
*
* @author
*/
// This is class is used to hold the product information for the inventory
// program
public class DVDMovie {
// item number field
private int itemNumber;
// DVD title field
private String dvdTitle;
// number of units in stock
private int unitsInStock;
// price per unit from stock
private double unitPrice;
// DVD genre
private String genre;
// default no argument constructor
public DVDMovie(int itemNumber, String dvdTitle, int unitsInStock, double unitPrice, String genre) {
this.itemNumber = 0;
this.dvdTitle = "unknown";
this.unitsInStock = 0;
this.unitPrice = 0;
this.setGenre("unknown");
}
/**
* Argument constructor to initialize the object with passed values
*
* @param itemNumber
* @param dvdTitle
* @param unitsInStock
* @param unitPrice
*/
public DVDMovie(int itemNumber, String dvdTitle, int unitsInStock,
double unitPrice) {
this.itemNumber = itemNumber;
this.dvdTitle = dvdTitle;
this.unitsInStock = unitsInStock;
this.unitPrice = unitPrice;
}
// calculates inventory value for this product
public double getInventoryValue() {
return unitPrice * unitsInStock;
}
//
// Accessors and Mutator for Class fields
//
public int getItemNumber() {
return itemNumber;
}
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
public String getDVDTitle() {
return dvdTitle;
}
public void setDVDTitle(String DVDTitle) {
this.dvdTitle = dvdTitle;
}
public int getUnitsInStock() {
return unitsInStock;
}
public void setUnitsInStock(int unitsInStock) {
this.unitsInStock = unitsInStock;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public String toString() {
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String str = "Number: " + getItemNumber();
str += ", DVD Title: " + getDVDTitle();
str += ", In Stock: " + getUnitsInStock();
str += ", Unit Price: " + getUnitPrice() + "\n";
str += "Inventory Value: " + formatter.format(getInventoryValue())
+ "\n";
return str;
}
/**
* @return the genre
*/
public String getGenre() {
return genre;
}
/**
* @param genre the genre to set
*/
public void setGenre(String genre) {
this.genre = genre;
}
}
*****************Inventory**************
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.NumberFormat;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author
*/
// This is the Driver program for Product
public class Inventory extends JFrame implements ActionListener {
// Index of current element of array
private int position = 0;
// create Products array
DVDMovie[] movieArray = null;
// Various components that are used in the program
private JLabel titleLabel = new JLabel("DVD Title: ");
private JTextField titleText = new JTextField("");
private JLabel itemNumberLabel = new JLabel("Item Number: ");
private JTextField itemNumberText = new JTextField("");
private JLabel unitsInStockLabel = new JLabel("Units In Stock: ");
private JTextField unitsInStockText = new JTextField("");
private JLabel unitPriceLabel = new JLabel("Unit Price: ");
private JTextField unitPriceText = new JTextField("");
private JLabel invValueLabel = new JLabel("Inventory Value: ");
private JTextField invValueText = new JTextField("");
// Product object related components
private JLabel genreLabel = new JLabel("DVD Genre: ");
private JTextField genreText = new JTextField("");
private JLabel restockingFeeLabel = new JLabel("Restocking Fee: ");
private JTextField restockingFeeText = new JTextField("");
private JLabel totalInvValueLabel = new JLabel("Total Inventory Value: ");
private JTextField totalInvValueText = new JTextField("");
private JButton firstButton;
private JButton previousButton;
private JButton nextButton;
private JButton lastButton;
private JButton addButton;
private JButton modifyButton;
private JButton deleteButton;
private JButton searchButton;
private JButton saveButton;
public Inventory() {
setProductData();
// Sort the products
sortProducts(movieArray);
JPanel buttonPanel = new JPanel();
firstButton = new JButton("First");
previousButton = new JButton("Previous");
nextButton = new JButton("Next");
lastButton = new JButton("Last");
addButton = new JButton("Add");
modifyButton = new JButton("Modify");
deleteButton = new JButton("Delete");
searchButton = new JButton("Search");
saveButton = new JButton("Save");
firstButton.addActionListener(this);
previousButton.addActionListener(this);
nextButton.addActionListener(this);
lastButton.addActionListener(this);
addButton.addActionListener(this);
modifyButton.addActionListener(this);
deleteButton.addActionListener(this);
searchButton.addActionListener(this);
saveButton.addActionListener(this);
buttonPanel.add(firstButton);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);
buttonPanel.add(lastButton);
buttonPanel.add(addButton);
buttonPanel.add(modifyButton);
buttonPanel.add(deleteButton);
buttonPanel.add(searchButton);
buttonPanel.add(saveButton);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
JPanel dataPanel = new JPanel();
dataPanel.setLayout(new GridLayout(8, 2));
dataPanel.add(titleLabel);
titleText.setEditable(false);
dataPanel.add(titleText);
dataPanel.add(itemNumberLabel);
itemNumberText.setEditable(false);
dataPanel.add(itemNumberText);
dataPanel.add(unitsInStockLabel);
unitsInStockText.setEditable(false);
dataPanel.add(unitsInStockText);
dataPanel.add(unitPriceLabel);
unitPriceText.setEditable(false);
dataPanel.add(unitPriceText);
dataPanel.add(invValueLabel);
invValueText.setEditable(false);
dataPanel.add(invValueText);
dataPanel.add(genreLabel);
genreText.setEditable(false);
dataPanel.add(genreText);
dataPanel.add(restockingFeeLabel);
restockingFeeText.setEditable(false);
dataPanel.add(restockingFeeText);
dataPanel.add(totalInvValueLabel);
totalInvValueText.setEditable(false);
dataPanel.add(totalInvValueText);
JPanel dataLogoPanel = new JPanel();
displayProduct();
JPanel iconPanel = new LogoPanel();
dataLogoPanel.add(iconPanel);
dataLogoPanel.add(dataPanel);
getContentPane().add(dataLogoPanel, BorderLayout.NORTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setSize(700, 300);
setResizable(false);
}
private void setProductData() {
DVDMovie DVDProduct1 = new DVDMovie(001, "Puss in Boots", 9, 15.99, "Animated");
DVDMovie DVDProduct2 = new DVDMovie(002, "X-Men", 6, 19.99, "Action");
DVDMovie DVDProduct3 = new DVDMovie(003, "Lincoln", 12, 17.99, "Drama - Historical");
DVDMovie DVDProduct4 = new DVDMovie(004, "World War Z", 8, 18.99, "Action");
DVDMovie DVDProduct5 = new DVDMovie(005, "Olympus Has Fallen", 11, 19.99, "Thriller");
// Create Array to store the objects
movieArray = new DVDMovie[5];
movieArray[0] = DVDProduct1;
movieArray[1] = DVDProduct2;
movieArray[2] = DVDProduct3;
movieArray[3] = DVDProduct4;
movieArray[4] = DVDProduct5;
}
/**
* This method is used to sort the products by DVD Title
*
* @param movieArray
*/
private static void sortProducts(DVDMovie[] movieArray) {
int length = movieArray.length;
DVDMovie obj1 = null;
DVDMovie obj2 = null;
DVDMovie temp = null;
for (int i = 1; i < length; i++) {
for (int j = 0; j < length - i; j++) {
obj1 = movieArray[j];
obj2 = movieArray[j + 1];
if (obj1.getDVDTitle().compareTo(obj2.getDVDTitle()) > 0) {
temp = obj1;
movieArray[j] = movieArray[j + 1];
movieArray[j + 1] = temp;
}
}
}
}
/**
* This method will be used to calculate the Entire Inventory Value
*
* @param movieArray
* @return
*/
private static double calculateEntireInventory(DVDMovie[] movieArray) {
DVDMovie obj = null;
double totalInventory = 0;
for (int i = 0; i < movieArray.length; i++) {
// Sets the current object in obj variable
obj = movieArray[i];
totalInventory += obj.getInventoryValue();
}
return totalInventory;
}
// button pushes...
// // Sets the void for the next-button click
public void actionPerformed(ActionEvent event) {
if (event.getSource() == firstButton) {
position = 0;
} else if (event.getSource() == lastButton) {
position = movieArray.length - 1;
} else if (event.getSource() == previousButton) {
if (position != 0) {
position--;
} else {
position = movieArray.length - 1;
}
} else if (event.getSource() == nextButton) {
if (position != (movieArray.length - 1)) {
position++;
} else {
position = 0;
}
} else if (event.getSource() == modifyButton) {
modifyItem();
} else if (event.getSource() == addButton) {
addItem();
position = movieArray.length - 1;
} else if (event.getSource() == deleteButton) {
deleteItem();
} else if (event.getSource() == searchButton) {
searchProduct();
} else if (event.getSource() == saveButton) {
writeToFile();
}
displayProduct();
}
/**
* This method is used to add the DVD
*/
public void addItem() {
// number should be greater then previous
int lastElementIndex = movieArray.length - 1;
int lastItemNumber = movieArray[lastElementIndex].getItemNumber();
int newItemNumber = lastItemNumber + 1;
String title = getDVDTitle();
int numberOfUnits = getNumberOfItems();
double unitPrice = getUnitPrice();
String genre = getgenre();
DVDMovie product = null;
if (null != genre && genre.trim().length() > 0) {
product = new DVD(newItemNumber, title, numberOfUnits,
unitPrice, genre);
} else {
product = new DVDMovie(newItemNumber, title, numberOfUnits, unitPrice);
}
DVDMovie[] existingArray = new DVDMovie[movieArray.length];
for (int i = 0; i < movieArray.length; i++) {
existingArray[i] = movieArray[i];
}
int newSize = existingArray.length + 1;
movieArray = new DVDMovie[newSize];
for (int i = 0; i < existingArray.length; i++) {
movieArray[i] = existingArray[i];
}
movieArray[newSize - 1] = product;
JOptionPane.showMessageDialog(null, "Movie added successfully!");
position = movieArray.length - 1;
}
private String getgenre() {
String genre = JOptionPane
.showInputDialog("Enter DVD Genre: ");
return genre;
}
private double getUnitPrice() {
String unitPriceStr = "";
double unitPrice = 0;
while (true) {
unitPriceStr = JOptionPane.showInputDialog("Enter Unit Price: ");
if (unitPriceStr != null && !unitPriceStr.trim().equals("")) {
try {
unitPrice = Double.parseDouble(unitPriceStr);
break;
} catch (NumberFormatException e) {
JOptionPane
.showMessageDialog(null,
"Please enter proper value for unit price.");
}
} else {
JOptionPane.showMessageDialog(null,
"Please enter unit price.");
}
}
return unitPrice;
}
private int getNumberOfItems() {
String numberOfUnitsStr = "";
int numberOfUnits = 0;
while (true) {
numberOfUnitsStr = JOptionPane
.showInputDialog("Enter Units in stock: ");
if (numberOfUnitsStr != null && !numberOfUnitsStr.trim().equals("")) {
try {
numberOfUnits = Integer.parseInt(numberOfUnitsStr);
break;
} catch (NumberFormatException e) {
JOptionPane
.showMessageDialog(null,
"Please enter proper value for quantity in stock.");
}
} else {
JOptionPane.showMessageDialog(null,
"Please enter quantity in stock.");
}
}
return numberOfUnits;
}
private String getDVDTitle() {
String title = JOptionPane.showInputDialog("Enter DVD Title: ");
while (title == null || title.trim().equals("")) {
JOptionPane
.showMessageDialog(null, "Please enter DVD title.");
title = JOptionPane.showInputDialog("Enter DVD Title: ");
}
return title;
}
public void displayDVD() {
NumberFormat formatter = NumberFormat.getCurrencyInstance();
DVDMovie DVD = movieArray[position];
double totalInvValue = calculateEntireInventory(movieArray);
titleText.setText(DVD.getDVDTitle());
itemNumberText.setText(DVD.getItemNumber() + "");
unitsInStockText.setText(DVD.getUnitsInStock() + "");
unitPriceText.setText(formatter.format(DVD.getUnitPrice()));
invValueText.setText(formatter.format(DVD.getInventoryValue()));
restockingFeeLabel.setVisible(true);
restockingFeeText.setVisible(true);
genreText.setText(((DVD) DVD).getGenre());
restockingFeeText.setText(formatter.format(((DVD) DVD).getRestockingFee()));
totalInvValueText.setText(formatter.format(totalInvValue));
}
/**
* This method is used to modify the product
*/
public void modifyItem() {
int itemNumStr = JOptionPane.showInputDialog("Enter the item number of the item you want to modify");
if (null == itemNumStr || itemNumStr.equals("")) {
JOptionPane.showMessageDialog(null, "No item number entered.");
return;
}
int searchItemNumber = 0;
try {
searchItemNumber = itemNumStr;
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Please enter proper item number. Try again later.");
return;
}
boolean matchFound = false;
for (int i = 0; i < movieArray.length; i++) {
if (searchItemNumber == movieArray[i].getItemNumber()) {
matchFound = true;
position = i;
break;
}
}
// No match found
if (!matchFound) {
JOptionPane.showMessageDialog(null,
"No product found having Item Number: " + searchItemNumber);
return;
}
DVDMovie product = movieArray[position];
String title = getDVDTitle();
int itemNumber = searchItemNumber;
int numberOfUnits = getNumberOfItems();
double unitPrice = getUnitPrice();
if (product instanceof DVD) {
String genre = getgenre();
((DVD) product).setGenre(genre);
}
// Update the values
product.setDVDTitle(title);
product.setItemNumber(itemNumber);
product.setUnitsInStock(numberOfUnits);
product.setUnitPrice(unitPrice);
JOptionPane.showMessageDialog(null, "Product successfully modified!");
}
/**
* This method is used to delete the product
*/
public void deleteItem() {
String itemNumStr = JOptionPane
.showInputDialog("Enter the item number of the item you want to delete");
if (null == itemNumStr || itemNumStr.equals("")) {
JOptionPane.showMessageDialog(null, "No item number entered.");
return;
}
int searchItemNumber = 0;
try {
searchItemNumber = Integer.parseInt(itemNumStr);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Please enter proper item number.Try again later!");
return;
}
boolean matchFound = false;
for (int i = 0; i < movieArray.length; i++) {
if (searchItemNumber == movieArray[i].getItemNumber()) {
matchFound = true;
position = i;
break;
}
}
// No match found
if (!matchFound) {
JOptionPane.showMessageDialog(null,
"No matches found.");
return;
}
DVDMovie productToBeDeleted = movieArray[position];
// Decrease the array by 1 and add the new elements as last element
DVDMovie[] existingArray = new DVDMovie[movieArray.length];
for (int i = 0; i < movieArray.length; i++) {
existingArray[i] = movieArray[i];
}
int newSize = existingArray.length - 1;
// Decrease the size by 1
movieArray = new DVDMovie[newSize];
int counter = 0;
for (int i = 0; i < existingArray.length; i++) {
if (existingArray[i].getItemNumber() != productToBeDeleted
.getItemNumber()) {
movieArray[counter] = existingArray[i];
counter++;
}
}
JOptionPane.showMessageDialog(null, "Product deleted successfully!");
// Set position to newly added product
position = movieArray.length - 1;
}
/**
* This method is used to search the product
*/
public void searchProduct() {
String itemNumStr = JOptionPane
.showInputDialog("Enter the item number of the item you want to search");
if (null == itemNumStr || itemNumStr.equals("")) {
JOptionPane.showMessageDialog(null, "No item number entered.");
return;
}
int searchItemNumber = 0;
try {
searchItemNumber = Integer.parseInt(itemNumStr);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Please enter proper item number.Try again later!");
return;
}
boolean matchFound = false;
for (int i = 0; i < movieArray.length; i++) {
if (searchItemNumber == movieArray[i].getItemNumber()) {
matchFound = true;
position = i;
break;
}
}
// No match found
if (!matchFound) {
JOptionPane.showMessageDialog(null,
"No product found having Item Number: " + searchItemNumber);
} else {
JOptionPane.showMessageDialog(null, "Product found.");
}
}
/**
* This method is used to write to file
*/
public void writeToFile() {
try {
File file = new File("c:\\data\\inventory.dat");
if (!file.exists()) {
new File("c:\\data\\").mkdir();
}
PrintWriter outFile = new PrintWriter(new FileWriter(
"c:\\data\\inventory.dat"));
DVDMovie product = null;
NumberFormat nf = NumberFormat.getCurrencyInstance();
for (int i = 0; i < movieArray.length; i++) {
product = movieArray[i];
outFile.println("DVD Title: " + product.getDVDTitle());
outFile.println("Item Number: " + product.getItemNumber());
outFile.println("Units in stock: " + product.getUnitsInStock());
outFile.println("Unit Price: " + product.getUnitPrice());
outFile.println("Inventory Value: "+ nf.format(product.getInventoryValue()));
outFile.println("DVD Genre: "+ ((DVD) product).getGenre());
outFile.println("Restocking Fee: "+ (((DVD) product).getRestockingFee()));
outFile.println();
outFile.println();
}
JOptionPane.showMessageDialog(null, "inventory.dat saved.");
outFile.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// main method
public static void main(String[] args) {
Inventory gui = new Inventory();
gui.setVisible(true);
}
}
class LogoPanel extends JPanel {
ImageIcon image = null;
private static String COMPANY_LOGO = "/companylogo.jpg";
public LogoPanel() {
image = new ImageIcon(COMPANY_LOGO);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
image.paintIcon(this, g, 0, 0);
}
public Dimension getPreferredSize() {
return new Dimension(image.getIconWidth(), image.getIconHeight());
}
}
**********************DVD*********************
/**
*
* @author
*/
// This is class is used to hold the product information for the DVD
public class DVD extends DVDMovie {
private String genre;
/**
* Argument constructor to initialize the object with passed values
*
* @param itemNumber
* @param DVDTitle
* @param unitsInStock
* @param unitPrice
* @param genre
*/
public DVD(int itemNumber, String DVDTitle, int unitsInStock, double unitPrice, String genre) {
super(itemNumber, DVDTitle, unitsInStock, unitPrice);
this.genre = genre;
}
// calculates inventory value after adding restocking fee
@Override
public double getInventoryValue() {
double value = super.getInventoryValue();
// Add 5% restocking fee
value = value + getRestockingFee();
return value;
}
// Restocking fee is 5% of value
public double getRestockingFee() {
double value = super.getInventoryValue();
return value * 0.05;
}
@Override
public String toString() {
String str = super.toString();
str += "DVD Genre: " + getGenre() + "\n";
return str;
}
//
// Accessors and Mutator for Class fields
//
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
}
********************ERRORS********************
The errors I'm getting are in the Inventory.java file and I cannot figure out how to correct them.
1) The serializable class LogoPanel does not declare a static final serialVersionUID field of type long /DVDInventory line 539
2) Cannot invoke equals(String) on the primitive type int /DVDInventory line 359
3) The assignment to variable dvdTitle has no effect /DVDInventory line 72
4) The serializable class Inventory does not declare a static final serialVersionUID field of type long /DVDInventory line 25
5) The operator == is undefined for the argument type(s) null, int /DVDInventory line 359
6) Type mismatch: cannot convert from String to int /DVDInventory line 358
Again, any help resolvile these issues in a timely manner would be greatly appreciated as this is due by Sunday 2/2.