I keep getting these errors when I try to run the program
Exception in thread "main" java.lang.NumberFormatException: For input string: "GTR"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at Inventory6.addProductToInventory2(Inventory6.java:549)
at Inventory6.main(Inventory6.java:734)
I've spent hours trying to figure out what I'm doing wrong and it's due in a few hours. I also cannot get an image added to my gui either.
//Inventory6.java
//include an add, delete and modify button, a save and search button
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.net.URL;
import java.io.*;
import java.util.*;
class Inventory implements Serializable
{
String name;
String stocknumber;
int units;
double price;
public Inventory(String Name, String stockNumber, int Units, double Price)
{
name = Name;
stocknumber = stockNumber;
units = Units;
price = Price;
}
public void setName(String Name)
{
name = Name;
}
public String getName()
{
return name;
}
public void setstockNumber(String stockNumber)
{
stocknumber = stockNumber;
}
public String getstockNumber()
{
return stocknumber;
}
public void setUnits(int Units)
{
units = Units;
}
public int getUnits()
{
return units;
}
public void setPrice(double Price)
{
price = Price;
}
public double getPrice()
{
return price;
}
public double getInventoryValue()
{
return price * units;
}
@Override
public String toString()
{
return "Product Name: "+name + "\nProduct stockNumber : "+stocknumber+"\nProduct Price: $"+price+"\nUnits in Stock: "+units + "\nInventory Value: $"+getInventoryValue();
}
} // end Inventory Class
class Product extends Inventory
{
String Model;
double restockFee;
public Product(String Model, double restockFee, String Name, String stockNumber, int Units, double Price)
{
super(Name, stockNumber, Units, Price);
this.Model = Model;
this.restockFee = restockFee;
}
public Product(String Model, String Name, String stockNumber, int Units, double Price)
{
super(Name, stockNumber, Units, Price);
this.Model = Model;
//restocking fee is 5%
this.restockFee = 0.05;
}
public void setModel(String model)
{
model = Model;
}
public String getModel()
{
return Model;
}
public double getRestockFee()
{
return super.getInventoryValue() * restockFee;
}
@Override
public double getInventoryValue()
{
return super.getInventoryValue() + (super.getInventoryValue() * restockFee);
}
@Override
public String toString()
{
StringBuffer sb = new StringBuffer("\nModel: ").append(Model).append("\n");
sb.append(super.toString());
return sb.toString();
}
} // End Product Class
class Inventory2 implements Serializable
{
public static int MAXIMUM_SIZE = 1000;
// store Nissan models
Product[] inventory;
int number_of_models_in_inventory = 0;
public Inventory2(int maximum_number_of_models)
{
inventory = new Product[maximum_number_of_models];
}
public void addProduct(Product models_to_add_to_inventory)
{
inventory[number_of_models_in_inventory] = models_to_add_to_inventory;
number_of_models_in_inventory++;
}
public Product getProduct(int index)
{
return inventory[index];
}
public void setProduct(Product p, int index)
{
inventory[index] = p;
}
public int getSize()
{
return number_of_models_in_inventory;
}
// return the maximum number of Nissan models
public int getMaxSize()
{
return inventory.length;
}
// add up Nissan models with restocking fee
public double getTotalValueOfAllInventory()
{
double tot = 0.00;
for(int i = 0; i < number_of_models_in_inventory; i++)
{
tot += inventory[i].getInventoryValue();
}
return tot;
}
public void sortInventory()
{
for(int j = 0; j < number_of_models_in_inventory - 1; j++)
{
for(int k = 0; k < number_of_models_in_inventory - 1; k++)
{
if(inventory[k].getName().compareToIgnoreCase(inventory[k+1].getName()) > 0)
{
Product temp = inventory[k];
inventory[k] = inventory[k+1];
inventory[k+1] = temp;
}
}
}
}
public Inventory2 deleteProduct(int index) {
Inventory2 newInventory2 = new Inventory2(inventory.length);
for (int i = 0; i < this.getSize(); i++) {
if (i != index)
newInventory2.addProduct(this.getProduct(i));
}
return newInventory2;
}
public int searchForProductByName(String searchString) {
// go through each Nissan model of inventory
int i = 0;
for ( i = 0; i < this.getSize(); i++ ) {
Product p = this.getProduct(i);
// does the name match
if ( p.getName().matches("(?i).*"+searchString+".*") ) {
// return index if model found
return i;
}
}
return -1;
}
}
public class Inventory6 extends JFrame
{
Inventory2 ProductInventory2;
int index = 0;
int lastNumberAdded = 0;
// GUI
private final JLabel nameLabel = new JLabel(" Product Name: ");
private JTextField nameText;
private final JLabel stocknumberLabel = new JLabel(" Product stockNumber: ");
private JTextField stocknumberText;
private final JLabel modelLabel = new JLabel(" Model: ");
private JTextField modelText;
private final JLabel priceLabel = new JLabel(" Price: ");
private JTextField priceText;
private final JLabel unitsLabel = new JLabel(" Units: ");
private JTextField unitsText;
private final JLabel valueLabel = new JLabel(" Value: ");
private JTextField valueText;
private final JLabel restockFeeLabel = new JLabel(" Restocking Fee: ");
private JTextField restockFeeText;
private final JLabel totalValueLabel = new JLabel(" Inventory Total: ");
private JLabel totalValueText;
private Action nextAction = new AbstractAction("Next") {
public void actionPerformed(ActionEvent evt) {
if (index == ProductInventory2.getSize() - 1) {
index = 0;
} else {
index++;
}
repaint();
}
};
private JButton nextButton = new JButton(nextAction);
// go to the previous model
private Action previousAction = new AbstractAction("Previous") {
public void actionPerformed(ActionEvent evt) {
if (index == 0) {
index = ProductInventory2.getSize() - 1;
} else {
index--;
}
repaint();
}
};
private JButton previousButton = new JButton(previousAction);
// go to the first model
private Action firstAction = new AbstractAction("First") {
public void actionPerformed(ActionEvent evt) {
index = 0;
repaint();
}
};
private JButton firstButton = new JButton(firstAction);
// go to last model
private Action lastAction = new AbstractAction("Last") {
public void actionPerformed(ActionEvent evt) {
index = ProductInventory2.getSize() - 1;
repaint();
}
};
private JButton lastButton = new JButton(lastAction);
Action searchAction = new AbstractAction("Search") {
public void actionPerformed(ActionEvent evt) {
if (searchButton.getText().equals("Search")) {
// change text
searchButton.setText("GO!");
// model may be changed
nameText.setEditable(true);
} else if (searchButton.getText().equals("GO!")) {
nameText.setEditable(false);
String inputName = nameText.getText();
int i = ProductInventory2.searchForProductByName(inputName);
if ( i >= 0 ) {
index = i;
repaint();
} else {
JOptionPane.showMessageDialog(null, "No Matches Found!");
}
searchButton.setText("Search");
}
repaint();
}
};
JButton searchButton = new JButton(searchAction);
Action deleteAction = new AbstractAction("Delete") {
public void actionPerformed(ActionEvent evt) {
ProductInventory2 = ProductInventory2.deleteProduct(index);
index = index - 1;
if (index < 0) {
index = 0;
}
repaint();
}
};
JButton deleteButton = new JButton(deleteAction);
Action saveAction = new AbstractAction("Save") {
public void actionPerformed(ActionEvent evt) {
saveInventory2ToFile();
}
};
JButton saveButton = new JButton(saveAction);
Action loadAction = new AbstractAction("Load File") {
public void actionPerformed(ActionEvent evt) {
loadInventory2FromFile();
}
};
JButton loadButton = new JButton(loadAction);
Action addAction = new AbstractAction("Add") {
public void actionPerformed(ActionEvent evt) {
if (addButton.getText().equals("Add")) {
addButton.setText("Click to Add!");
nameText.setEditable(true);
stocknumberText.setEditable(true);
modelText.setEditable(true);
priceText.setEditable(true);
unitsText.setEditable(true);
nameText.setText("");
stocknumberText.setText("" + (lastNumberAdded + 1));
modelText.setText("");
priceText.setText("$0.00");
unitsText.setText("0");
valueText.setText("$0.00");
restockFeeText.setText("$0.00");
} else if (addButton.getText().equals("Click to Add!")) {
try {
String Name = nameText.getText().trim();
String stockNumber = stocknumberText.getText().trim();
String Model = modelText.getText().trim();
String Units = unitsText.getText().trim();
String strPrice = priceText.getText().trim();
int in_stock = Integer.parseInt ( Units );
strPrice = strPrice.replaceAll("\\$", "");
Double price = Double.parseDouble( strPrice );
Product newProduct = new Product(Model, stockNumber, Name, in_stock, price );
addProductToInventory2(newProduct);
index=ProductInventory2.getSize()- 1;
} catch (Exception addException) {
JOptionPane.showMessageDialog(null, "Error adding Nissan Model information:\n" + addException);
index = 0;
}
addButton.setText("Add");
nameText.setEditable(false);
stocknumberText.setEditable(false);
modelText.setEditable(false);
priceText.setEditable(false);
unitsText.setEditable(false);
repaint();
}
}
};
JButton addButton = new JButton(addAction);
Action modifyAction = new AbstractAction("Modify") {
public void actionPerformed(ActionEvent evt) {
if (modifyButton.getText().equals("Modify")) {
modifyButton.setText("Click to Modify!");
nameText.setEditable(true);
stocknumberText.setEditable(true);
modelText.setEditable(true);
priceText.setEditable(true);
unitsText.setEditable(true);
} else if (modifyButton.getText().equals("Click to Modify!")) {
try {
String Name = nameText.getText().trim();
String stockNumber = stocknumberText.getText().trim();
String model = modelText.getText().trim();
String Units = unitsText.getText().trim();
String strPrice = priceText.getText().trim();
int in_stock = Integer.parseInt ( Units );
strPrice = strPrice.replaceAll("\\$", "");
Double price = Double.parseDouble( strPrice );
Product newProduct = new Product(model, stockNumber, Name, in_stock, price );
ProductInventory2.setProduct(newProduct, index);
} catch (Exception modifyException) {
JOptionPane.showMessageDialog(null, "Error modifying Nissan model information:\n" + modifyException);
}
modifyButton.setText("Modify");
nameText.setEditable(false);
stocknumberText.setEditable(false);
modelText.setEditable(false);
priceText.setEditable(false);
unitsText.setEditable(false);
repaint();
}
}
};
JButton modifyButton = new JButton(modifyAction);
public void addProductToInventory2(Product temp)
{
ProductInventory2.addProduct(temp);
lastNumberAdded = Integer.parseInt(temp.getModel());
repaint();
}
public void sortProductInventory2()
{
ProductInventory2.sortInventory();
index = 0;
repaint();
}
public void saveInventory2ToFile() {
File theDir = new File("C:\\data");
theDir.mkdir();
File theFile = new File("C:\\data\\inventory.dat");
try {
FileOutputStream fileOut = new FileOutputStream( theFile );
ObjectOutputStream objOut = new ObjectOutputStream( fileOut );
objOut.writeObject(ProductInventory2);
objOut.flush();
objOut.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error occured while writing to file:" + e);
}
}
public int loadInventory2FromFile() {
Inventory2 oldProductInventory2 = ProductInventory2;
File theFile = new File("C:\\data\\inventory.dat");
try {
FileInputStream fileIn = new FileInputStream( theFile );
ObjectInputStream objIn = new ObjectInputStream( fileIn );
ProductInventory2 = (Inventory2)objIn.readObject();
objIn.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Could not load file:" + e);
ProductInventory2 = oldProductInventory2;
oldProductInventory2 = null;
return -1;
}
oldProductInventory2 = null;
// update GUI
repaint();
return 0;
}
public Inventory6(int maximum_number_of_models)
{
ProductInventory2 = new Inventory2(maximum_number_of_models);
// setup the GUI
// add the next Nissan model button to the top of the GUI
JPanel buttonPanel = new JPanel();
buttonPanel.add(firstButton);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);
buttonPanel.add(lastButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
JPanel dataButtonPanel = new JPanel();
dataButtonPanel.add(addButton);
dataButtonPanel.add(deleteButton);
dataButtonPanel.add(modifyButton);
dataButtonPanel.add(searchButton);
dataButtonPanel.add(saveButton);
dataButtonPanel.add(loadButton);
getContentPane().add(dataButtonPanel, BorderLayout.NORTH);
// logo
URL url = this.getClass().getResource("");
Image img = Toolkit.getDefaultToolkit().getImage(url);
Image scaledImage = img.getScaledInstance(205, 300, Image.SCALE_AREA_AVERAGING);
Icon logoIcon = new ImageIcon(scaledImage);
JLabel companyLogoLabel = new JLabel(logoIcon);
// add logo
getContentPane().add(companyLogoLabel, BorderLayout.WEST);
JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));
centerPanel.add(nameLabel);
nameText = new JTextField("");
nameText.setEditable(false);
centerPanel.add(nameText);
centerPanel.add(stocknumberLabel);
stocknumberText = new JTextField("");
stocknumberText.setEditable(false);
centerPanel.add(stocknumberText);
centerPanel.add(modelLabel);
modelText = new JTextField("");
modelText.setEditable(false);
centerPanel.add(modelText);
centerPanel.add(priceLabel);
priceText = new JTextField("");
priceText.setEditable(false);
centerPanel.add(priceText);
centerPanel.add(unitsLabel);
unitsText = new JTextField("");
unitsText.setEditable(false);
centerPanel.add(unitsText);
centerPanel.add(valueLabel);
valueText = new JTextField("");
valueText.setEditable(false);
centerPanel.add(valueText);
centerPanel.add(restockFeeLabel);
restockFeeText = new JTextField("");
restockFeeText.setEditable(false);
centerPanel.add(restockFeeText);
centerPanel.add(totalValueLabel);
totalValueText = new JLabel("");
centerPanel.add(totalValueText);
getContentPane().add(centerPanel, BorderLayout.CENTER);
repaint();
}
@Override
public void repaint() {
Product temp = ProductInventory2.getProduct(index);
if (temp != null) {
nameText.setText( temp.getName() );
stocknumberText.setText( ""+temp.getstockNumber() );
modelText.setText( temp.getModel() );
priceText.setText( String.format("$%.2f", temp.getPrice()) );
unitsText.setText( ""+temp.getUnits() );
valueText.setText( String.format("$%.2f", temp.getPrice() * temp.getUnits() ) );
restockFeeText.setText( String.format("$%.2f", temp.getRestockFee() ) );
}
totalValueText.setText( String.format("$%.2f", ProductInventory2.getTotalValueOfAllInventory() ) );
}
public static void main(String args[])
{
Inventory6 Product_GUI = new Inventory6 (40);
// Add the Nissan models
Product_GUI.addProductToInventory2(new Product("Frontier","3", "wheels" ,14, 000.00));
Product_GUI.addProductToInventory2(new Product("Cube", "5", "floormats" ,12, 000.00));
Product_GUI.addProductToInventory2(new Product("Quest", "1","radio",12,000.00));
Product_GUI.addProductToInventory2(new Product("Rogue","3", "tires",30, 000.00));
Product_GUI.addProductToInventory2(new Product("Armada","2", "dvdplayer",50, 000.00));
// sort models
Product_GUI.sortProductInventory2();
Product_GUI.loadInventory2FromFile();
Product_GUI.setDefaultCloseOperation( EXIT_ON_CLOSE );
Product_GUI.pack();
Product_GUI.setSize(700, 350);
Product_GUI.setResizable(false);
Product_GUI.setLocationRelativeTo( null );
Product_GUI.setVisible(true);
return;
}
} // End Inventory6 class