/*
* Program: Inventory Program Part 4
* File: InventoryFrame.java
* Summary: Inherits JFrame class to build a window application that
* contains label and text fields.
* Author: Joshua Lewis
* Date: September 10, 2010
*/
package inventoryprogrampart4;
import java.awt.GridLayout; //Import to create grid layout
import java.awt.BorderLayout; //Import to create border layout
import java.awt.event.ActionEvent; //Import for click events
import java.awt.event.ActionListener; //Import for click events
import javax.swing.JPanel; //Import to create panels
import javax.swing.JFrame; //Import to use frame
import javax.swing.JButton; //Import to create buttons
import javax.swing.JLabel; //Import to create text fields
import javax.swing.JTextField; //Import to create text fields
import java.util.Arrays; //Import to use arrays
public class InventoryFrame extends JFrame { //Begin InventoryFrame Class
//Declare two panels. gridPanel is main panel. panel is inserted into gridPanel
private JPanel gridPanel = new JPanel();
private JPanel panel = new JPanel();
//Declare buttons
JButton nextButton; //Button to go to next page
//Declare text fields
JTextField itemNumberField; //Field for itemNumber
JTextField bandNameField; //Field for bandName
JTextField albumNameField; //Field for albumName
JTextField stockUnitsField; //Field for stockUnits
JTextField priceField; //Field for pricePerItem
JTextField totalValueField; //Field for totalInventoryValue
//Declare labels
JLabel lblItemNumber; //Label for itemNumber
JLabel lblBandName; //Label for bandName
JLabel lblAlbumName; //Label for albumName
JLabel lblStock; //Label for stockUnits
JLabel lblPrice; //Label for pricePeritem
JLabel lblValue; //Label for totalInventoryValue
//Declare five RockBand Objects
RockBand band1;
RockBand band2;
RockBand band3;
RockBand band4;
RockBand band5;
public static int MAX_CDS = 5; //Set maximum size for cds array
public RockBand cds[] = new RockBand[MAX_CDS]; //Declare array of RockBand objects
static int ArrayIndex = 0; //Declare array index
public void InventoryFrame() { //Begin Constructor
//Create and initialize five RockBand objects
band1 = new RockBand();
band1.setBandName("Lamb of God"); //Sets bandName for band1
band1.setAlbumName("New American Gospel"); //Sets albumName for band1
band1.setItemNumber("606-001"); //Sets itemNumber for band1
band1.setPrice(9.99); //Sets pricePerItem for band1
band1.setUnits(6); //Sets numberOfUnitsInStock for band1
band2 = new RockBand();
band2.setBandName("Lamb of God"); //Sets bandName for band2
band2.setAlbumName("As the Palaces Burn"); //Sets albumName for band2
band2.setItemNumber("606-002"); //Sets itemNumber for band2
band2.setPrice(10.99); //Sets pricePerItem for band2
band2.setUnits(4); //Sets numberOfUnitsInStock for band2
band3 = new RockBand();
band3.setBandName("Lamb of God"); //Sets bandName for band3
band3.setAlbumName("Ashes of the Wake"); //Sets albumName for band3
band3.setItemNumber("606-003"); //Sets itemNumber for band3
band3.setPrice(11.99); //Sets pricePerItem for band3
band3.setUnits(9); //Sets numberOfUnitsInStock for band3
band4 = new RockBand();
band4.setBandName("Lamb of God"); //Sets bandName for band4
band4.setAlbumName("Sacrament"); //Sets albumName for band4
band4.setItemNumber("606-004"); //Sets itemNumber for band4
band4.setPrice(14.99); //Sets pricePerItem for band4
band4.setUnits(14); //Sets numberOfUnitsInStock for band4
band5 = new RockBand();
band5.setBandName("Lamb of God"); //Sets bandName for band5
band5.setAlbumName("Wrath"); //Sets albumName for band5
band5.setItemNumber("606-005"); //Sets itemNumber for band5
band5.setPrice(14.99); //Sets pricePerItem for band5
band5.setUnits(12); //Sets numberOfUnitsInStock for band5
//Add RockBand objects to cds array
cds[0] = band1; //Assigns band1 to first space in array
cds[1] = band2; //Assigns band2 to second space in array
cds[2] = band3; //Assigns band3 to third space in array
cds[3] = band4; //Assigns band4 to fourth space in array
cds[4] = band5; //Assigns band5 to fifth space in array
gridPanel.setLayout(new BorderLayout()); //Create a border layout
gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); //Add label panel
gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); //Add text panel
gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); //Add button panel
add(gridPanel);
//Parameters for sort are the cds array and AlbumNameComparator
Arrays.sort(cds, new AlbumNameComparator()); //Sort cds array
} //End constructor
//Method to create panel for button
private JPanel createButtonPanel() { //Begin createButtonPanel method
ActionListener btnListen = new ButtonListener(); //Create listener
//Create button objects
nextButton = new JButton("Display CD"); //Create and label button
nextButton.setActionCommand("Next"); //Set action command for click button event
nextButton.addActionListener(btnListen); //Button listener for click button event
//Create panel object
JPanel panel = new JPanel();
panel.add(nextButton); //Add next button to panel
return panel;
} //End createButtonPanel method
private JPanel createLabelPanel() { //Begin createLabelPanel method
//Create instance of label objects
lblItemNumber = new JLabel("Item Number:"); //Label for itemNumber
lblBandName = new JLabel("Band Name:"); //Label for bandName
lblAlbumName = new JLabel("Album Name:"); //Label for albumName
lblPrice = new JLabel("Price Per Item:"); //Label for pricePerItem
lblStock = new JLabel("Number In Stock:"); //Label for stockUnits
lblValue = new JLabel("Total Inventory Value:"); //Label for totalInventoryValue
panel = new JPanel();
panel.setLayout(new GridLayout(5, 1));
//Add labels to panel
panel.add(lblItemNumber);
panel.add(lblBandName);
panel.add(lblAlbumName);
panel.add(lblPrice);
panel.add(lblStock);
panel.add(lblValue);
return panel;
} //End createLabelPanel method
private JPanel createTextPanel() { //Begin createTextPanel method
//Create instances of text box objects
itemNumberField = new JTextField(); //Field for itemNumber
itemNumberField.setEditable(false); //Set field not editable
bandNameField = new JTextField(); //Field for bandName
bandNameField.setEditable(false); //Set field not editable
albumNameField = new JTextField(); //Field for albumName
albumNameField.setEditable(false); //Set field not editable
stockUnitsField = new JTextField(); //Field for stockUnits
stockUnitsField.setEditable(false); //Set field not editable
priceField = new JTextField(); //Field for pricePerItem
priceField.setEditable(false); //Set field not editable
totalValueField = new JTextField(); //Field for totalInventoryValue
totalValueField.setEditable(false); //Set field not editable
panel = new JPanel(); //Create panel object
panel.setLayout(new GridLayout(5, 1)); //Create grid layout for fields
panel.add(itemNumberField); //Add itemNumberField to grid layout
panel.add(bandNameField); //Add bandNameField to grid layout
panel.add(albumNameField); //Add albumNameField to grid layout
panel.add(stockUnitsField); //Add stockUnitsField to grid layout
panel.add(priceField); //Add priceField to grid layout
panel.add(totalValueField); //Add totalValueField to grid layout
return panel;
} //End createTextPanel method
//Nested Button Listener Class
class ButtonListener implements ActionListener { //Begin ButtonListener Class
public void actionPerformed(ActionEvent e) { //Begin actionPerformed Method
//Add button functions
if (e.getActionCommand() == "Next") //If First button
{
if (ArrayIndex < cds.length) //Test to not exceed upper bound of array
{
//get itemNumber and assign to text field
itemNumberField.setText(cds[ArrayIndex].getItemNumber());
//get bandName and assign to text field
bandNameField.setText(cds[ArrayIndex].getBandName());
//get albumName and assign to text field
albumNameField.setText(cds[ArrayIndex].getAlbumName());
//get stockUnits and assign to text field
stockUnitsField.setText(String.valueOf(cds[ArrayIndex].getUnits()));
//get pricePerItem and set to text field
priceField.setText(String.valueOf(cds[ArrayIndex].getPrice()));
//get totalInventoryValue and assign to text field
totalValueField.setText(String.valueOf(cds[ArrayIndex].calcInventoryValue()));
ArrayIndex++;
}//End if
}
} //End actionPerformed Method
} //End ButtonListener Class
} //End InventoryFrame Class
/**
* Program: Inventory Program Part 4
* File: RockBand.java
* Summary: This file holds the subclass RockBand of the superclass CD. This
* subclass includes a new variable, bandName, and the set/get methods
* associated with it.
* Author: Joshua Lewis
* Date: September 10, 2010
*/
package inventoryprogrampart4;
public class RockBand extends CD
{ //Begin RockBand Class
private String bandName; //Variable to hold name of band
private final double RESTOCK_RATE = .05; //Percent of price for restock fee
public void setBandName(String inName) //Access to set bandName variable
{ //Begin setBandName Method
bandName = inName;
} //End setBandName Method
public String getBandName() //Access to bandName variable
{ //Begin getBandName Method
return bandName;
} //End getBandName Method
} //End RockBand Class
/*
* Program: Inventory Program Part 4
* File: Comparator.java
* * Summary: This file is used to sort the array of CD objects used in the Inventory
* Program Part 4.
* Author: Joshua Lewis
* Date: September 10, 2010
*/
package inventoryprogrampart4;
import java.util.Comparator;
public class AlbumNameComparator implements Comparator { //Begin AlbumNameComparator Class
//Begin Compare Method
public int compare(Object cdOne, Object cdTwo){ //Create two cd Objects to compare title
//Use getProductName Method from CD class
String title1 = ((RockBand)cdOne).getAlbumName().toUpperCase();
//Use getProductName Method from CD class
String title2 = ((RockBand)cdTwo).getAlbumName().toUpperCase();
//Compare title of CD object 1 with title of CD object 2
if (!(title1.equals(title2)))
return title1.compareTo(title2);
else
return title2.compareTo(title1);
} //End compare Method
} //End AlbumNameComparator Class
/*
* Program: Inventory Program Part 4
* File: Product.java
* Summary: This contains the product class that holds the variables and methods
* associated with setting/getting information of product name, product
* number, cost, number in stock, and total value of stock for
* objects of class CD used in the Inventory Program Part 4.
* Author: Joshua Lewis
* Date: September 10, 2010
*/
package inventoryprogrampart4;
import javax.swing.JOptionPane; //Import to use JOptionPane Class
public class CD { //Begin CD Class
protected String itemNumber; //Variable to hold item number
protected String albumName; //Variable to hold name of album
protected int stockUnits; //Variable to hold number of units of item in stock
protected double pricePerItem; //Variable to hold price per unit of the item
protected double totalInventoryValue; //Variable to hold the total value of all units of item
protected static double SumInventoryValue; //Variable to hold total value of all stock
public CD() {}; //Empty constructor
public CD(String inItemNumber, String inAlbumName, int inStock, double inPrice)
{ //Constructor to take itemNumber, albumName, stockUnits, and pricePerItem
itemNumber = inItemNumber;
albumName = inAlbumName;
stockUnits = inStock;
pricePerItem = inPrice;
} //End Constructor
//Accesses itemNumber to set variable
public void setItemNumber(String inItemNumber){ //Begin setItemNumber Method
itemNumber = inItemNumber;
} //End setItemNumber Method
//Access to variable itemNumber
public String getItemNumber(){ //Begin getItemNumber Method
return itemNumber;
} //End getItemNumber Method
//Accesses productName to set variable
public void setAlbumName(String inAlbumName){ //Begin setAlbumName Method
albumName = inAlbumName;
} //End setAlbumName Method
//Access to variable albumName
public String getAlbumName(){ //Begin getAlbumName Method
return albumName;
} //End getAlbumName Method
//Access numberOfUnitsInStock to set variable
public void setUnits(int inStock){ //Begin setUnits Method
stockUnits = inStock;
} //End setUnits Method
//Access to variable numberOfUnitsInStock
public int getUnits(){ //Begin getUnits Method
return stockUnits;
} //End getUnits Method
//Access pricePerItem to set variable
public void setPrice(double inItemPrice){ //Begin setPrice Method
this.pricePerItem = inItemPrice;
} //End setPrice Method
//Access to pricePerItem variable
public double getPrice(){ //Begin getPrice Method
return pricePerItem;
} //End getPrice Method
public double calcInventoryValue() //Calculates total value of all units of item
{ //Begin calcInventoryValue Method
totalInventoryValue = pricePerItem * stockUnits;
return totalInventoryValue;
} //End calcInventoryValue Method
} //End CD Class
/*
* Program: InventoryDisplay Program Part 4
* File: InventoryDisplay.java
* Summary: This program establishes an inventory of five items
* of the RockBand class, which is a sub class of CD. These items are
* stored in an array and sorted. This program then displays all five
* items with their respective product name, product number, number
* in stock, cost, and total cost of all in stock in alphabetical order.
* The entire program uses a GUI for interaction.
* Author: Joshua Lewis
* Date: September 10, 2010
*/
package inventoryprogrampart4;
import javax.swing.JFrame; //Import to use JFrame
public class InventoryDisplay { //Begin InventoryDisplay Class
public static void main(String[] args) { //Begin Main Method
JFrame frame = new InventoryFrame(); //Create InventoryFrame object
frame.setSize(400, 200); //Set size of window. 400 is width, 200 is height
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close application
frame.setTitle("Inventory Program Part 4"); //Set title of window
frame.setLocationRelativeTo(null); //Center the form
frame.setVisible(true); //Display form
} //End main Method
} //End InventoryDisplay Class

New Topic/Question
Reply




MultiQuote




|