Hello,
I have done the code for the final part of the Java Inventory Program in my class. This is the requirements for the final code.
• 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 code and following it are the errors I am getting. Any help would be greatly appreciated!
tory Program
* This program keeps track of products and calculates and displays;
* price, name, number, quantity, and total inventory value.
*/
package stock;
/**IT 215
*June, 2008
* @author Tammy
*/
import java.text.NumberFormat;
public class Stock {
private String itemName;
private int itemNumber;
private int itemQuantity;
private double itemPrice;
public Stock () {
this.itemName = "";
this.itemNumber = 0;
this.itemQuantity = 0;
this.itemPrice = 0;
}
public Stock(String name, int number, int quantity, double price) {
this.itemName = name;
this.itemNumber = number;
this.itemQuantity = quantity;
this.itemPrice = price;
}
public void setItemName(String name) {
this.itemName = name;
}
public void setItemPrice(double itemPrice) {
this.itemPrice = itemPrice;
}
public void setItemQuantity(int quantity) {
this.itemQuantity = quantity;
}
public void setItemNumber(int number) {
this.itemNumber = number;
}
public String getItemName() {
return this.itemName;
}
public int getItemNumber() {
return this.itemNumber;
}
public double getItemPrice() {
return this.itemPrice;
}
public int getItemQuantity() {
return this.itemQuantity;
}
public double calculateTotalItemValue() {
return getItemQuantity()* getItemPrice();
}
@Override
public String toString() {
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
return"\nItemName: "+getItemName() +"\nItem Number: "+getItemNumber()+"\nItemPrice:"
+currencyFormat.format(getItemPrice())
+"\nItem Quantity: " +getItemQuantity()
+"\nValue of Inventory: " +currencyFormat.format(this.calculateTotalItemValue());
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stock;
import java.text.NumberFormat;
/**
*
* @author Tammy
*/
public class StockSubClass extends Stock {
private String ItemGenre;
public StockSubClass() {
super();
ItemGenre = "";
}
public void setItemGenre (String genre) {
this.ItemGenre = genre;
}
public String getItemGenre() {
return this.ItemGenre;
}
//Total item inventory value
public double calculateRestockFee() {
return (super.getItemPrice() * .05);
}
//Total item inventory value
public double calculateInventoryValue() {
return (super.getItemPrice()*super.getItemQuantity() + calculateRestockFee());
}
//Total value of all inventory
public static double getTotalInventoryValue(StockSubClass [] items) {
double total = 0.0;
for(int j = 0; j < items.length; j++) {
total += items[j].calculateInventoryValue();
}
return total;
}
//Override toString function to display a single item
@Override
public String toString() {
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
return"\nItem Name: "+super.getItemName()+ "\nItem Number: "+super.getItemNumber()+"\nItem Price: "
+currencyFormat.format(super.getItemPrice())+"\nRestock Fee: "
+currencyFormat.format(calculateRestockFee())+"\nItem Quantity: "+super.getItemQuantity()+"\nGenre: "
+getItemGenre()+"\nInventory Value: "+currencyFormat.format(this.calculateInventoryValue())+"\n\n";
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package stock;
import java.util.*;
import java.text.*;
/**
*
* @author Tammy
*/
public class StockInput {
private StockSubClass [] items;
//Constructor
public StockInput (int numberOfitems)
{
items = new StockSubClass[numberOfitems];
for (int i = 0; i < numberOfitems; i++) {
items[i] = new StockSubClass();
}
}
public StockSubClass [] inputItems() {
items[0].setItemName("Hurricane Charley");
items[0].setItemQuantity(3);
items[0].setItemPrice(16.95);
items[0].setItemNumber(152);
items[0].setItemGenre("Documentary");
items[1].setItemName("Eight Legged Freaks");
items[1].setItemQuantity(7);
items[1].setItemPrice(10.00);
items[1].setItemNumber(342);
items[1].setItemGenre("Horror");
items[2].setItemName("Major Payne");
items[2].setItemQuantity(3);
items[2].setItemPrice(13.95);
items[2].setItemNumber(294);
items[2].setItemGenre("Comedy");
items[3].setItemName("Phil the Alien");
items[3].setItemQuantity(6);
items[3].setItemPrice(7.95);
items[3].setItemNumber(187);
items[3].setItemGenre("Comedy");
return (items);
}
//Sort array by name
public StockSubClass [] sortByItemName() {
StockSubClass[] sorted = new StockSubClass[items.length];
String [] names = new String[items.length];
for (int i =0; i < items.length; i++) {
names[i] = items[i].getItemName();
}
Arrays.sort(names);
int index = -1;
for (int i = 0; i < names.length; i++) {
index = this.searchByName(names[i]);
if (index != -1) {
sorted[i] = items[index];
}
}
this.items = sorted;
return (sorted);
}
private int searchByName(String name) {
for(int i = 0; i < items.length; i++) {
if (name.equals(items[i].getItemName())) {
return i;
}
}
return -1;
}
}
* and open the template in the editor.
*/
package stock;
/**
*
* @author Tammy
*/
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import javax.swing.JTextArea;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class StockGui extends JFrame {
private StockSubClass [] itemList;
private StockInput myStock;
public static final int MAXIMUM_ITEMS = 5;
private JTextArea invTotal;
private JLabel iconLabel;
private JButton last;
public StockGui () {
myStock = new StockInput(MAXIMUM_ITEMS);
itemList = new StockSubClass[MAXIMUM_ITEMS];
myStock.inputItems();
itemList = myStock.sortByItemName();
}
private int position = 0;
private int number = 0;
public static final int width = 520;
public static final int height = 460;
public static final String TITLE = "Current Inventory";
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JLabel jLabel = null;
private JButton next = null;
private JButton previous = null;
private JButton first = null;
//JLabels to display information
private JLabel[] labels = new JLabel[10];
private void displayOneItem(StockSubClass stockSubClass) {
}
//Initialize JFrame
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setContentPane(getJContentPane());
jFrame.setSize(new Dimension(width, HEIGHT));
jFrame.setTitle(TITLE);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
return jFrame;
}
//Initialize JContentPane
private JPanel getJContentPane() {
Object JContentPane = null;
if (JContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.CENTER);
jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
}
return jContentPane;
}
//Method initializes jPanel
private JPanel getJPanel() {
//Set up logo
Icon logo = new ImageIcon(getClass().getResource("logo.jpg"));
iconLabel = new JLabel(logo, SwingConstants.CENTER);
iconLabel.setToolTipText("Company Logo");
add(iconLabel);
if (jPanel == null) {
jLabel = new JLabel();
jLabel.setText("Current Inventory");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setPreferredSize(new Dimension(width, 30));
jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jPanel.add(jLabel);
jPanel.add(getPanel("Item Number"));
jPanel.add(getPanel("Item Name"));
jPanel.add(getPanel("Item Quantity"));
jPanel.add(getPanel("Item Price"));
jPanel.add(getPanel("Inventory Value"));
jPanel.add(getPanel("Restocking Fee"));
jPanel.add(getPanel("Inventory Value w/ Restocking Fee"));
jPanel.add(getPanel("Genre"));
jPanel.add(getPanel("Total Value of All Inventory"));
displayOneItem(itemList[0]);
}
return jPanel;
}
public JPanel getPanel (String title) {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(width, 30));
panel.add(getLabel(title, false));
JLabel valueLabel = getLabel("", true);
labels[number] = valueLabel;
number++;
panel.add(valueLabel);
return panel;
}
public JLabel getLabel(String value, boolean end) {
JLabel label = new JLabel();
if (!end) {
label.setText(value +": ");
label.setHorizontalAlignment(SwingConstants.TRAILING);
label.setPreferredSize(new Dimension(width / 2, 20));
}
else {
label.setText(value);
label.setPreferredSize(new Dimension(width / 3, 20));
}
return label;
}
private Component getJPanel1() {
throw new UnsupportedOperationException("Not yet implemented");
}
//Initialize "Next" button
private JButton getNext() {
if(next == null) {
next = new JButton("Next");
next.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
next();
}
});
}
return next;
}
//Initialize "Previous" button
private JButton getPrevious() {
if (previous == null) {
previous = new JButton("Previous");
previous.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
previous();
}
});
}
return previous;
}
//Initialize "First" button
private JButton getFirst() {
if(first == null) {
first = new JButton("First");
first.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first();
}
private void first() {
StockSubClass itemCurrent = new StockSubClass();
itemCurrent = itemList[0];
position = 0;
this.displayOneItem(itemCurrent);
}
});
return first;
}
}
//Initialize "Last" button
private JButton getLast() {
if (last == null) {
last = new JButton("Last");
last.addActionListener(new java.awt.event.ActionListener(){
private Object item;
public void actionPerformed(java.awt.event.ActionEvent e) {
last();
}
private void displayOneItem(StockSubClass itemCurrent) {
String tempValue;
labels[0].setText(item.getItemNumber() + "");
labels[1].setText(item.getItemName());
labels[2].setText(Long.toString(item.getItemQuantity()));
tempValue = String.format("$%.2f", item.getItemPrice());
labels[3].setText(tempValue);
tempValue = String.format("$%.2f", item.calculateTotalItemValue());
labels[4].setText(tempValue);
tempValue = String.format("$%.2f", item.calculateRestockingFee());
labels[5].setText(tempValue);
tempValue = String.format("$%.2f", item.calculateInventoryValue());
labels[6].setText(tempValue);
labels[7].setText(item.getItemGenre());
tempValue = String.format("$%.2f", StockSubClass.getTotalInventoryValue(itemList));
labels[3].setText(tempValue);
}
private void last() {
StockSubClass itemCurrent = new StockSubClass();
itemCurrent = itemList[MAXIMUM_ITEMS - 1];
position = MAXIMUM_ITEMS - 1;
this.displayOneItem(itemCurrent);
}
});
return (JButton) last;
}
}
private JButton Add() {
JButton Add = new JButton("Add");
Add.addActionListener(new java.awt.event.ActionListener() {
private Object stockInput;
private Object NameText;
public void actionPerformed(ActionEvent e) {
int index;
FeeQtyProduct temp = (FeeQtyProduct)
stockInput.getFeeQtyProduct(index);
int numberOfItems = stockInput.getNumberOfItems() +1;
index = (numberOfItems - 2 % numberOfItems);
repaint GUI;
if(NameText.getText().equals("Add artist name")) {
JOptionPane.showMessageDialog(null, "Please fill blank entry before adding more" +
"\nRemember to push Modify when you finish with changes","Negative, the pattern is full",JOptionPane.ERROR_MESSAGE);
}
if(NameText.getText().equals("Add Artist Name") !=true) {
FeeQtyProduct product = new FeeQtyProduct("Add artist name",
Integer.parseInt(itemNumberText.getText())+1, 0, 0.0, "Add movie title");
index = (numItems - 1) % numItems;
stockinput.addFeeQtyProduct(product);
repaintGUI();
}
if (numItems == 4)
{
stockinput.removeFeeQtyProduct(temp);
JOptionPane.showMessageDialog(null, "No more entries", JOptionPane.ERROR_MESSAGE);
}
}
});
}
private JButton Save(){
JButton Save = new JButton("Save");
Save.addActionListener(new java.awt.event.ActionListener() {
private Object stockInput;
public void actionPerformed(ActionEvent e) {
int numItems = stockInput.getNumItems();
Inventory Storage = new InventoryStorage();
title.openFile();
int currentTitle = 0;
do {
title.addTitle();
currentTitle = currentTitle + 1;
index = (++index) % numItems;
}
while (currentTitle < numItems);
title.closeFile();
}
});
}
private JButton Modify() {
JButton Modify = new JButton("Modify");
Modify.addActionListener(new java.awt.event.ActionListener() {
private Object artistText;
private Object numberText;
public void actionPerformed(ActionEvent e) {
if (artistText.getText(.equals("")) {
JOptionPane.showMessageDialog(null, "Please complete entry", JOptionPane.ERROR_MESSAGE);
repaint GUI();
}
int MESSAGE();
if (numberText.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Please complete entry", JOptionPane.ERROR-MESSAGE);
repaint GUI();
}
try {
Double.parseDouble(qtyText.getText());
Double.parseDouble(priceText.getText());
}
catch (Exception d) {
JOptionPane.showMessageDialog(null, "Recheck numbers entered for price and quantity", JOptionPane.ERROR-MESSAGE);
repaintGUI();
}
String name; int number; double amount, price; String title;
name = NameText.getText();
number = Integer.parseInt(temNumberText.getText());
amount = Double.parseDouble(qtyText.getText());
price = Double.parseDouble(priceText.getText());
title = NameText.getText();
FeeQtyProduct modify = (FeeQtyProduct)
stockinput.getFeeQtyProduct(index);
modify.setProductNumber(number);
modify.setProductName(name);
modify.setBaseAmount(amount);
mosidy.setBasePrice(price);
modify.setMovieTitle(title);
repaintGUI();
}
});
}
private JButton Delete() {
JButton Delete = new JButton("Delete");
Delete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
int numItems = stockInput.getNumItems();
FeeQtyProduct temp = (FeeWtyProduct)
stockInput.getFeeQtyProduct(index);
if (numItems != 0) {
if(Integer.parseInt(itemNumberText.getText())!= numItems) {
stockInput.remove(index);
repaintGUI();
int i = Integer.parseInt(itemNumberText.getText());
index = (++index) % numItems;
repaintGUI();
int j = Integer.parseInt(itemNumberText.getText());
if (i > j) {
index = (--index) % numItems;
}
repaintGUI();
temp == (FeeQtyProduct)stockInput.getFeeQtyProduct(index);
temp.setProductNumber(j-1);
index = (++index) % numItems;
repaintGUI();
}
if(Integer.parseInt(itemNumberText.getText())== numItems) {
stockInput.removeFeeQtyProduct(temp);
index = (++index) % numItems;
repaint GUI();
}
}
if (numItems == 1) {
FeeQtyProduct = new FeeQtyProduct("Add artist name", numItems, 0, 0.0, "Add movie title");
stockInput.addFeeQtyProduct(product);
JOptionPane.showMessageDialog(null, "Delete process complete", JOptionPane.ERROR_MESSAGE);
repaintGUI();
}
}
});
}
}
//Display first item
private void first() {
StockSubClass itemCurrent = new StockSubClass();
itemCurrent = itemList[0];
position = 0;
this.displayOneItem(itemCurrent);
}
//Display last item
private void last() {
StockSubClass itemCurrent = new StockSubClass();
itemCurrent = itemList[MAXIMUM_ITEMS - 1];
position = MAXIMUM_ITEMS - 1;
this.displayOneItem(itemCurrent);
}
//Display previous item
private void previous() {
StockSubClass itemCurrent = new StockSubClass();
if (position != 0) {
position--;
}
else {
position = MAXIMUM_ITEMS - 1;
}
itemCurrent = itemList[position];
this.displayOneItem(itemCurrent);
}
//Display next item
private void next() {
StockSubClass itemCurrent = new StockSubClass();
if (position != (MAXIMUM_ITEMS - 1)) {
position++;
}
else {
position = 0;
}
itemCurrent = itemList[position];
this.displayOneItem(itemCurrent);
}
//Display one item
private void displayOneItem(StockSubClass item) {
String tempValue;
labels[0].setText(item.getItemNumber() + "");
labels[1].setText(item.getItemName());
labels[2].setText(Long.toString(item.getItemQuantity()));
tempValue = String.format("$%.2f", item.getItemPrice());
labels[3].setText(tempValue);
tempValue = String.format("$%.2f", item.calculateTotalItemValue());
labels[4].setText(tempValue);
tempValue = String.format("$%.2f", item.calculateRestockingFee());
labels[5].setText(tempValue);
tempValue = String.format("$%.2f", item.calculateInventoryValue());
labels[6].setText(tempValue);
labels[7].setText(item.getItemGenre());
tempValue = String.format("$%.2f", StockSubClass.getTotalInventoryValue(itemList));
labels[3].setText(tempValue);
}
public void main(String[] args) {
StockGui displayItem = new StockGui();
displayItem.getJFrame();
}
class InventoryStorage {
private Formatter output;
public void openFile() {
try {
String strDirectory ="C://data/";
boolean success = (new File(strDirectory));
if (success) {
JOptionPane.showMessageDialog(null, "We created a directory named data in your C drive", JOptionPane.PLAIN_MESSAGE);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "You do not have write access to the C: drive", JOptionPane.ERROR_MESSAGE);
}
try {
output = new Formatter("C:/data/inventory.dat");
}
catch (SecurityException securityException) {
JOptionPane.showMessageDialog(null, "You do not have write access to this file",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
catch(FileNotFoundException fileNotFoundException) {
JOptionPane.showMessageDialog(null, "Please create a file named data in your c drive", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
JOptionPane.showMessageDialog(null, "File saved successfully", JOptionPane.PLAIN_MESSAGE);
}
public void addItems() {
FeeQtyProduct Item = (FeeQtyProduct)stockInput.getFeeQtyProduct(index);
BigDecimal roundPrice = new BigDecimal (Double.toStr5ing(item.restock()));
roundPrice = roundPrice.setScale(2, RoundingMode.HALF_UP);
BigDecimal roundValue = new BigDecimal(Double.toString(item.total()));
roundValue = roundValue.setScale(2,RoundingMode.HALF_UP);
BigDecimal roundTotal = new BigDecimal(Double.toString(stockInput.value()));
roundTotal = roundTotal.setScale(2,RoundingMode.HALF_UP);
if (item != null){
output.format("Artist: %s Product#: %s WTY#: %.OfPrice: $%.2f%s: %s",
record.getItemName(), item.getItemNumber(), item.getItemAmount(), item.getItemPrice(),
"Movie", item.getItemTitle() + "Inventory Total $" + (roundTotal) + "\t\n END OF LINE\t\t");
}
}
public void closeFile() {
if (output != null) {
output.close();
}
}
public void repaintGUI() {
FeeQtyProduct temp = (FeeQtyProduct)stockInput.getFeeQtyProduct(index);
if (temp != null){
itemNumberText.setText("" + temp.getProductNumber());
artistText.setText(temp.getProductName());
movieText.setText (String.format("%s", temp.getMovieTitle()));
priceText.setText(String.format("%.2f", temp.getBasePrice()));
restockFeeText.setText(String.format("$%.2f", temp.restock()));
qtyText.setText(String.format("%.0f", temp.getBaseAmount()));
valueText.setText(String.format("$%.2f", temp.total()));
}
totalValueText.setText(String.format("$%.2f", stock.Input.value()));
}
}
These are the errors I am getting:
:\Documents and Settings\Tammy\My Documents\NetBeansProjects\Stock\src\stock\StockGui.java:368: illegal start of expression
if (artistText.getText(.equals("")) {
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\Stock\src\stock\StockGui.java:374: ')' expected
int MESSAGE();
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\Stock\src\stock\StockGui.java:379: ';' expected
repaint GUI();
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\Stock\src\stock\StockGui.java:446: not a statement
temp == (FeeQtyProduct)stockInput.getFeeQtyProduct(index);
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\Stock\src\stock\StockGui.java:459: ';' expected
repaint GUI();
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\Stock\src\stock\StockGui.java:486: 'class' or 'interface' expected
private void first() {
6 errors
BUILD FAILED (total time: 0 seconds)