Hello Again,
I am in need of some help getting my GUI to display the inventory values. The code runs and compiles successfully, but it does not display anything. I am new to this and could use some help. Thanks!
package inventory;
import java.text.DecimalFormat;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
/**
*
* @author Mel
*/
class Pizza {
// Private variables
String name;
private int quantity;
private double price;
private int productNumber;
String textArea;
// Default constructor uses other constructor to set the
// default state of the object.
public Pizza() {
this(0,"Unknown",0,0.00);
}
// Constructor that user can specify a name, quantity, and price for items.
public Pizza(int number, String itemname, int quantityOnHand, double itemprice) {
setName(itemname);
setQuantityOnHand(quantityOnHand);
setPrice(itemprice);
}
public void setNumber(int number){
number = productNumber;
}
// Public mutators (changes state of the object)
public void setName(String itemname) {
name = itemname;
}
// Sets quantity on hand and if negative, defaults to zero.
public void setQuantityOnHand(int quantityOnHand) {
if (quantityOnHand > 0) {
quantity = quantityOnHand;
}
else { quantity = 0; }
}
// Set price of a product and defaults it to zero if negative.
public void setPrice(double itemPrice) {
if (itemPrice > 0.00) {
price = itemPrice;
}
else { price = 0.00; }
}
// Get products number
public int getProductNumber(){
return productNumber;
}
// Get the product's name
public String getName() {
return name;
}
public int getQuantityOnHand() {
return quantity;
}
public double getPrice() {
return price;
}
// Calculate the value of stock on this particular item.
public double getItemValue() {
return (price * (double)quantity);
}
public double getRestockFee(){
return (getItemValue() * 0.05);
}
// String representation of the product
public String toString() {
return name + " - " + price;
}
}
public class Inventory {
// Setup an array of Products (set it to hold 30 items)
int inventorySize = 30;
private Pizza items[] = new Pizza[inventorySize];
// Set the formatter to format values into currency form.
DecimalFormat formatter = new DecimalFormat("$##,###.00");
// Adds a product to the array of pizza. Adds to first empty slot found.
public void addPizza(Pizza item) {
for (int i = 0; i < inventorySize; i++) {
if (items[i] == null) {
items[i] = item;
return;
}
}
}
// Loop through our array of pizza and add up the total value.
// Go item by item adding the quantity on hand x its price.
// Add that value to a running total variable.
public double getTotalInvValue() {
double sumOfInventory = 0.0;
// Uses a for loop which iterates the array of items.
for (Pizza item : items) {
if (item != null) {
sumOfInventory += item.getItemValue();
}
}
return sumOfInventory;
}
// Prints the inventory list including name, quantity, price, and total stock value for each item.
public void printInventory() {
System.out.println("Printing items in inventory...\n");
boolean hasItems = false;
for (Pizza item : items) {
if (item != null) {
hasItems = true;
System.out.println(item.toString() + " Quantity: " + item.getQuantityOnHand() + " Value of Stock: " + formatter.format(item.getItemValue()));
}
}
// If no items were found, print a message saying the inventory is empty.
if (!hasItems) { System.out.println("Inventory is empty at the moment.\n"); }
}
}
// Inherited class PizzaRestock from the base class Pizza
class PizzaRestock extends Pizza {
// Holds the year the move was made
private String supplier;
public PizzaRestock(int productNumber, String itemname, int quantityOnHand, double itemprice, String supplier) {
// Pass on the values needed for creating the Product class first thing
super(productNumber, itemname, quantityOnHand, itemprice);
supplier = "";
}
// Get the supplier of this Pizza product
public String getSupplier() {
return supplier;
}
// Overrides getItemValue() in Pizza class by calling the base class version and
// adding a 5% restocking fee on top
@Override
public double getItemValue() {
return super.getItemValue() * 1.05;
}
// Simply gets the base class's value, and figures out the 5% restocking fee only
public double getRestockFee() {
return super.getItemValue() * .05;
}
}
class InventoryTest {
static DecimalFormat formatter = new DecimalFormat("$##,###.00");
public static void main(String args[]) {
Pizza item1 = new Pizza(1234, "Dough", 22, 1.99);
Pizza item2 = new Pizza(1235, "Sauce", 35, 5.99);
Pizza item3 = new Pizza(1236, "Cheese", 4, 15.99);
Pizza item4 = new Pizza(1237, "Pepperoni", 3, 14.99);
Pizza item5 = new Pizza(1238, "Ham", 2, 16.99);
// Create an inventory item to store our products
Inventory myPizza = new Inventory();
// Let's show there is nothing in the inventory to start.
myPizza.printInventory();
// Now lets add the items we declared above.
myPizza.addPizza(item1);
myPizza.addPizza(item2);
myPizza.addPizza(item3);
myPizza.addPizza(item4);
myPizza.addPizza(item5);
// Print out the inventory
myPizza.printInventory();
// (after formatted using DecimalFormat class into dollars and cents)
// Print the total value of the inventory
System.out.println("\nTotal value of inventory is: " + formatter.format(myPizza.getTotalInvValue()));String[] pizza = {"Dough","Sauce","Cheese","Pepperoni","Ham"};
PizzaGUI gui = new PizzaGUI();
gui.setVisible(true);
Arrays.sort(pizza);
System.out.println("The sorted String array is:");
for (String name : pizza) {
System.out.println("Name = " + name);
}
}
}
class PizzaGUI extends JFrame {
static int max = 5;
static final Pizza[]Pizza = new Pizza[max];
int counter;
/**
* Creates new form PizzaGUI
*/
public PizzaGUI() {
super();
this.counter = - 1;
JFrame gui = new JFrame();
initComponents();
Pizza[0] = new Pizza(1234,"Dough",22,1.99);
Pizza[1] = new Pizza(1235, "Sauce", 35, 5.99);
Pizza[2] = new Pizza(1236, "Cheese", 4, 15.99);
Pizza[3] = new Pizza(1237, "Pepperoni", 3, 14.99);
Pizza[4] = new Pizza(1238, "Ham", 2, 16.99);
gui.add(textArea);
}
static void textArea() {
JTextArea textArea = new JTextArea(Pizza[max].textArea);
textArea.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
nextButton = new javax.swing.JButton();
closeButton = new javax.swing.JButton();
backButton = new javax.swing.JButton();
titleLabel = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
textArea = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
nextButton.setText("Next");
nextButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nextButtonActionPerformed(evt);
}
});
closeButton.setText("Close Program");
closeButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeButtonActionPerformed(evt);
}
});
backButton.setText("Back");
backButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
backButtonActionPerformed(evt);
}
private void backButtonActionPerformed(ActionEvent evt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
titleLabel.setText("Inventory Program");
textArea.setEditable(false);
textArea.setColumns(20);
textArea.setRows(5);
jScrollPane1.setViewportView(textArea);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(backButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
.addComponent(closeButton))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(titleLabel)))
.addGap(85, 85, 85)
.addComponent(nextButton)))
.addContainerGap()));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(titleLabel)
.addGap(9, 9, 9)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nextButton)
.addComponent(closeButton)
.addComponent(backButton))
.addContainerGap()) );
pack();
}// </editor-fold>
private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (counter < 4) {
counter = counter + 1;
}else {
counter = 0;
}
textArea.setText(Pizza[counter].getProductNumber() + "");
textArea.setText(Pizza[counter].getName() + "");
textArea.setText(Pizza[counter].getPrice() + "");
textArea.setText(Pizza[counter].getQuantityOnHand() + "");
textArea.setText(Pizza[counter].getRestockFee() + "");
textArea.setText(Pizza[counter].getItemValue() + "");
}
/**
* @param args the command line arguments
*/
public void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PizzaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PizzaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PizzaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PizzaGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PizzaGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton backButton;
private javax.swing.JButton closeButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton nextButton;
private javax.swing.JTextArea textArea;
private javax.swing.JLabel titleLabel;
// End of variables declaration
}