Starting with your last submission (Inventory Program Part 5), - 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.
- 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 the product’s information in the GUI.
- Keep the previous functionality intact from the previous Inventory Assignments.
This is what I have so far from Inventory program part 5:
Inventory file:
package comparable.Inventory; // packages Inventory class into comparable folder
import java.awt.Color; // imports java.awt's Colors
import java.awt.Font; // imports java.awt's Fonts
import java.awt.Graphics;
import java.awt.GridLayout; // imports java.awt's GridLayout layout
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import javax.swing.JTextField; // imports javax.swing's JTextField
import java.util.Arrays; // imports java Array utility
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame; // imports javax.swing's JFrame
import javax.swing.JPanel; // imports javax.swing's JPanel
import javax.swing.JLabel; // imports javax.swing's JLabel
public class Inventory // declares class Inventory
{
// declares new array of ProductProducer class called product1 with 6 elements
private static ProductProducer[] product1 = new ProductProducer[6];
private static int current = 0;
private static JTextField itemNumber, dvd, producer, units, price, value;
// starts execution of Main function
public static void main(String args[])
{
// sets values for element 0
product1[0] = new ProductProducer("The Darkest Hour", "Chris Gorak",
25, 10, 12.50);
// sets values for element 1
product1[1] = new ProductProducer("The Smurfs", "Raja Gosnell",
184, 5, 10.50);
// sets values for element 2
product1[2] = new ProductProducer("Free Runner", "Lawrence Silverstein",
212, 25, 17.50);
// sets values for element 3
product1[3] = new ProductProducer("Happy Feet 2", "George Miller",
51, 17, 14.50);
// sets values for element 4
product1[4] = new ProductProducer("Immortals", "Tarsem Singh",
112, 12, 10.80);
// sets values for element 5
product1[5] = new ProductProducer("In Time", "Andrew Niccol",
118, 9, 15.50);
// Creates Font to be used for JText below
Font f = new Font("Times New Roman", Font.BOLD, 24);
// Creates JTextFields
itemNumber = new JTextField(10);
dvd = new JTextField(10);
producer = new JTextField(10);
units = new JTextField(10);
price = new JTextField(10);
value = new JTextField(10);
// sorts array by dvd name
Arrays.sort(product1);
// creates JFrame with title
JFrame frame = new JFrame( "DVD Inventory Collection" );
// sets close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// creates new JPanel fieldPanel
// Declares a new JPanel and calls it fieldPanel
JPanel fieldPanel = new JPanel();
// creates GridLayout for fieldPanel JPanel
fieldPanel.setLayout( new GridLayout(7, 6));
// creates ImageIcon for image.png in source folder
ImageIcon image = new ImageIcon("image.png");
// Creates new JLabel and fills it with image.png
JLabel picture = new JLabel(image);
// adds picture JLabel to fieldPanel
fieldPanel.add(picture);
// Creates JLabel to display title
JLabel logoMessage = new JLabel("My Movie Inventory");
logoMessage.setFont(f); // sets font of logoMessage to f font defined above
fieldPanel.add(logoMessage); // adds logMessage to fieldPanel
JLabel blank2 = new JLabel(); // Creates blank JLabel to skip grid
fieldPanel.add(blank2); // adds blank label to grid
JLabel blank3 = new JLabel(); // Creates another blank JLabel to skip another grid
fieldPanel.add(blank3); // adds blank label to grid
// creates JLabel to be used as column header for item numbers
JLabel itemNumberLabel = new JLabel( "Item Number: " );
fieldPanel.add(itemNumberLabel); // adds new JLabel to fieldPanel
fieldPanel.add(itemNumber); // adds JTextField to fieldPanel
// creates JLabel to be used as column header for dvd titles
JLabel dvdLabel = new JLabel( " Dvd title: " );
fieldPanel.add( dvdLabel ); // adds new JLabel to fieldPanel
fieldPanel.add( dvd ); // adds JTextField to fieldPanel
// creates JLabel to be used as column header for producers
JLabel producerLabel = new JLabel( " Producer: " );
fieldPanel.add( producerLabel ); // adds new JLabel to fieldPanel
fieldPanel.add( producer ); // adds JTextField to fieldPanel
// creates JLabel to be used as column header for Units
JLabel unitsLabel = new JLabel( " Units: " );
fieldPanel.add( unitsLabel ); // adds new JLabel to fieldPanel
fieldPanel.add( units ); // adds JTextField to fieldPanel
// creates JLabel to be used as column header for Prices
JLabel priceLabel = new JLabel( " Price: " );
fieldPanel.add( priceLabel ); // adds new JLabel to fieldPanel
fieldPanel.add( price ); // adds JTextField to fieldPanel
// creates JLabel to be used as column header for Values
JLabel valueLabel = new JLabel( " Value: " );
fieldPanel.add( valueLabel ); // adds new JLabel to fieldPanel
fieldPanel.add( value ); // adds JTextField to fieldPanel
// Declares 4 JButtons and gives them names
JButton first = new JButton( "First Item" );
JButton last = new JButton( "Last Item" );
JButton previous = new JButton( "Previous Item" );
JButton next = new JButton( "Next Item" );
JButton add = new JButton( "Add" );
JButton delete = new JButton( "Delete" );
JButton modify = new JButton( "Modify" );
JButton search = new JButton( "Search" );
JButton save = new JButton( "Save" );
// Creates action listener for first JButton
first.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
current = 0;
refresh(); // calls refresh method
}
}
);
// adds first JButton to fieldPanel
fieldPanel.add( first );
// Creates action listener for last JButton
last.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
current = (product1.length - 1 );
refresh(); // calls refresh method
}
}
);
// adds last JButton to fieldPanel
fieldPanel.add( last );
// Creates action listener for next JButton
next.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// increments current each time next JButton is pressed
++current;
// if statement sets current to 0 every time the last array element is reached
if (current == (product1.length))
{
current = 0;
}
refresh(); // calls refresh method
}
}
);
// adds next JButton to fieldPanel
fieldPanel.add( next );
// adds action listener for previous JButton
previous.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// sets current to last array element everytime current reaches 0
if (current == 0)
{
current = product1.length;
}
// deincrements current every time previous JButton is pressed
--current;
refresh(); // calls refresh() method
}
}
);
add.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
}
);
delete.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
}
);
modify.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
}
);
save.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
}
);
search.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
}
);
// adds previous JButton to fieldPanel
fieldPanel.add( previous );
fieldPanel.add( add );
fieldPanel.add( delete );
fieldPanel.add( modify );
fieldPanel.add( search );
fieldPanel.add( save );
frame.add(fieldPanel); // adds fieldPanel to the JFrame
// Creates Font to be used for JLabel totalValue that is created below
Font g = new Font("Times New Roman", Font.BOLD + Font.ITALIC, 14 );
// creates JLabel displaying message that goes with total Value of stock
JLabel totalValueMessage = new JLabel( " Total Value of Stock is: " );
totalValueMessage.setFont(g); // sets font for totalValueMessage JLabel
fieldPanel.add( totalValueMessage ); // adds totalValueMessage to fieldPanel
//adds JLabel totalValue to display a hard print version of the total value of stock that isn't in a text field
JLabel totalValue = new JLabel(String.format( "$%.02f", + getTotalValue(product1)));
totalValue.setFont(g); // sets font for totalValue JLabel
fieldPanel.add( totalValue ); // adds JLabel to field Panel at the bottom of 2nd column
frame.setSize(900, 600); // sets size of JFrame
frame.setVisible(true); // sets JFrame to be visible
} // end main
// Declares method refresh()
public static void refresh()
{
// sets Strings to fill JTextFields each time different JButtons are pressed
itemNumber.setText("" + product1[current].getItemNumber());
dvd.setText("" + product1[current].getDvd());
producer.setText("" + product1[current].getProducer());
units.setText(String.format("%.02f", product1[current].getUnitsAvailable()));
price.setText(String.format("$%.02f", product1[current].getUnitPrice()));
value.setText(String.format("$%.02f", product1[current].getValue()));
} // end method refresh
// method to get the total value of the product1 array
public static double getTotalValue( Product[] product1)
{
int i = 0;
double value = 0;
for(i = 0; i < product1.length; i++)
{
value += product1[i].getValue();
}
return (value);
} // end method getTotalValue
} // end class Inventory
Product file:
package comparable.Inventory; // packages Product into comparable folder with Inventory.java
import javax.swing.Icon;
import javax.swing.JPanel;
public class Product extends JPanel implements Comparable // allows class to be compared
{
// declares variables
private double unitsAvailable;
private double unitPrice;
private String dvd;
private int itemNumber;
private double value;
// creates object Product for Product class
public Product()
{
unitsAvailable = 0;
unitPrice = 0;
} // end constructor
// creates 4-argument constructor
public Product( String dvd, int itemNumber, double unitsAvailable, double unitPrice )
{
this.dvd = dvd;
this.itemNumber = itemNumber;
this.unitsAvailable = unitsAvailable;
this.unitPrice = unitPrice;
} // end Product constructor
// method to get dvd name
public String getDvd()
{
return dvd;
} // end method getDvd
// method to get Unit price
public double getUnitPrice()
{
return unitPrice;
} // end method getUnitPrice
// method to get Units in stock
public double getUnitsAvailable()
{
return unitsAvailable;
} // end method getUnitsAvailable
// method to getItemNumber
public int getItemNumber()
{
return itemNumber;
} // end method getItemNumber
// method to get the value in stock
public double getValue()
{
return unitsAvailable * unitPrice;
} // end method getValue
// method to set the name of the dvd
public void setDvd(String dvd)
{
this.dvd = dvd;
} // end method setDvd
// method to set the price per unit
public void setUnitPrice(double unitPrice)
{
this.unitPrice = unitPrice;
} // end method setUnitPrice
// method to set the number of units in stock
public void setUnitsInStock(double unitsInStock)
{
this.unitsAvailable = unitsInStock;
} // end method setUnitsInStock
// method to set the item number
public void setItemNumber(int itemNumber)
{
this.itemNumber = itemNumber;
} // end method setItemNumber
// method to compare dvd title's in the inventory class
public int compareTo( Object anotherProduct)
{
Product tmp = (Product) anotherProduct;
return this.getDvd().compareTo(tmp.getDvd());
} // end method compareTo
} // end class Product
ProductProducer file:
package comparable.Inventory;
public class ProductProducer extends Product
{
private String producer; // Producer of film
// five-argument constructor
public ProductProducer( String dvd, String producer, int itemNumber, double unitsAvailable, double unitPrice)
{
super( dvd, itemNumber, unitsAvailable, unitPrice );
setProducer(producer); // validates and stores producer
} // end five-argument constructor
// set Producer
public void setProducer( String producer )
{
this.producer = producer;
} // end method setProducer
// method to get Producer
public String getProducer()
{
return producer;
} // end method getProducer
@Override // This method Overrides the getValue() method in its super class Product
public double getValue()
{
// this statement returns the value of the Product class getValue() function * 1.05 for re-stocking fee
return super.getValue() * 1.05;
} // end the Override getValue()
} // end class ProductProducer

New Topic/Question
Reply



MultiQuote








|