Hello, I am back and in need of direction again. I am on the part of the Inventory assignment which requires: Modify the InventoryProgarm to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition the GUI should display the value of the entire inventory, the additional attribrute(movie year), and the restocking fee.
Sorry everyone, I forgot some of the code on my other post. Here is what I have and need help with.
CODE
*
*/
package InventoryProgram;
/** Inventory Program
* IT 215
* @author Tammy
* June 21, 2008
*/
public class Product {
// Private variables
private String name;
private int quantity;
private double price;
private int productid = 0;
//Default constructor uses other constructor to set the default state of the object.
public Product() {
this (0, "Unknown", 0, 0.00);
}
public static void main(String[] args) {
}
//Constructor for user to specify a name, quantity, and price, for the items.
public Product (int productId, String itemname, int quantityOnHand, double itemprice) {
productid = productId;
setName (itemname);
setQuantityOnHand (quantityOnHand);
setPrice (itemprice);
}
//Changes state of the object
public void setName(String itemname) {
name = itemname;
}
//Sets price of product 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 to zero if negative
public void setPrice (double itemPrice) {
if (itemPrice > 0.00) {
price = itemPrice;
}
else {price = 0.00;}
}
//Get product name
public String getName() {
return name;
}
public int getQuantityOnHand() {
return quantity;
}
public double getPrice() {
return price;
}
//Calculate the value of stock on particular item
public double getItemValue() {
return (price * (double)quantity);
}
//String representing product
@Override
public String toString () {
return name + " - " + price;
}
}
and:
CODE
* @author Tammy
*/
public class DVD extends Product {
//Holds year movie was made
private int movieyear;
//Constructor calls constructor of Product first, passing its values to the base class to initialize before setting the instance (year) variable
public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year) {
//Pass on values needed for creating Product class first
super(productId, itemname, quantityOnHand, itemprice);
movieyear = year;
}
DVD(String item, String string0, String string1, int i, int i0, double d) {
throw new UnsupportedOperationException("Not yet implemented");
}
//Set year manually
public void setYear (int year) {
movieyear = year;
}
//Get the year of the DVD
public int getYear() {
return movieyear;
}
//Overrides getItemValue in Product class by calling the base class version and adding a 5% restocking fee
@Override
public double getItemValue() {
return super.getItemValue() * 1.05;
}
//Gets base class values and figures out the 5% fee
public double getRestockingFee() {
return super.getItemValue() * .05;
}
}
and:
CODE
import java.text.DecimalFormat; //Import the format class to format values into currency
public class Inventory {
//Set up an array of Products to hold 30 items
int inventorySize = 30;
private Product items[] = new Product[inventorySize];
//Set formatter to format values into currency
static DecimalFormat formatter = new DecimalFormat ("$##,###.00");
public static void main (String args[]) {
DVD[] library = new DVD[4];
library[0] = new DVD("1", "Hurricane Charley", "2004", 101, 3, 16.95);
library[1] = new DVD("2", "Eight Legged Freaks", "2002", 102, 5, 9.99);
library[2] = new DVD("3", "Major Payne", "1995", 103, 2, 14.99);
library[3] = new DVD("4", "Phil the Alien", "2004", 104, 6, 5.99);
GUI gui = new GUI();
gui.setDVD(library);
Double totalInv = library[0].CalculateTotalInventoryValue(library);
int index;
Double restockingFee = library[index].getRestockingFee();
}
//Adds a product to the array, in the first empty slot found
public void addProduct(Product item){
for (int i = 0; i < inventorySize; i++) {
if (items[i] == null) {
items[i] = item;
return;
}
}
}
/**Loop through array and add the total value, item by item; adding the quantity on hand multiplied by its price,
* and add that value to a running total accumulator variable
*/
public double getTotalInvValue() {
double sumOfInventory = 0.0;
//Condensed for loop to itterate the array of items
for (Product item : items) {
//Check for an item
if (item != null) {
sumOfInventory += item.getItemValue();
}
}
return sumOfInventory;
}
//Prints the inventory list with 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 (Product item : items) {
if (item != null) {
hasItems = true;
System.out.println(item.toString() + "Quantity: " + item.getQuantityOnHand() + "Value of Stock: " + formatter.format(item.getItemValue()));
}
}
//Prints message stating the inventory is empty, if no items are found
if (!hasItems) {
System.out.println("Inventory is currently empty.\n");
}
}
}
and:
CODE
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package InventoryProgram;
/**
*
* @author Tammy
*/
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
public class GUI extends JFrame implements ActionListener {
private JLabel label2; //Item number
private JLabel label3; //Movie title
private JLabel label4; //Movie year
private JLabel label5; //Stock number
private JLabel label6; //Number of item in stock
private JLabel label7; //Restocking fee
private JLabel label8; //Purchase Price
private JLabel label9; //Inventory Value
private JLabel label10; //Total Inventory Value
private JTextField jtfItem; //
private JTextField jtfVideo;
private JTextField jtfYear;
private JTextField jtfitmNumber;
private JTextField jtfStock;
private JTextField jtfReStock;
private JTextField jtfPrice;
private JTextField jtfValue;
private JTextField jtftotalInv;
private JLabel label11; //JLabel icon
private JButton btnFirst;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnLast;
private JButton btnAdd;
private JButton btnDelete;
private JButton btnModify;
private JButton btnSave;
private JButton btnSearch;
private JButton btnLoadfile;
DVD[] dvd;
int index = 0;
public GUI() {
super("Inventory of DVD Movies");
JLabel jl;
JPanel jp;
setLayout( new FlowLayout()); //Set frame layout
label2 = new JLabel("Item#: ");
label2.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.TOP);
jtfItem = new JTextField (30);
jtfItem.setEditable(false);
add(label2); //Add label2 to JFrame
add(jtfItem);
label3 = new JLabel("Title of Movie: ");
label3.setHorizontalTextPosition(SwingConstants.RIGHT);
label2.setVerticalTextPosition(SwingConstants.CENTER);
jtfVideo = new JTextField (30);
jtfVideo.setEditable(false);
add(label3);
add(jtfVideo);
label4 = new JLabel("Year of Movie: ");
label4.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.TOP);
jtfYear = new JTextField (30);
jtfYear.setEditable(false);
add(label4);
add(jtfYear);
label5 = new JLabel("Item Number is: ");
label5.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.TOP);
jtfitmNumber = new JTextField (30);
jtfitmNumber.setEditable(false);
add(label5);
add(jtfitmNumber);
label6 = new JLabel("Number of Items in Stock: ");
label6.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.TOP);
jtfStock = newJTextField (30);
jtfStock.setEditable(false);
add(label6);
Component add = add(jtfStock);
label7 = new JLabel("Restocking Fee is: ");
label7.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.TOP);
jtfReStock = new JTextField (30);
jtfReStock.setEditable(false);
add(label7);
add(jtfReStock);
label8 = new JLabel("Purchase Price is: ");
label8.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.TOP);
jtfPrice = new JTextField (30);
jtfPrice.setEditable(false);
add(label8);
add(jtfPrice);
label9 = new JLabel("Inventory Value is: ");
label9.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.TOP);
jtfValue = new JTextField (30);
jtfValue.setEditable(false);
add(label9);
add(jtfValue);
label10 = new JLabel("Total Inventory Value is: ");
label10.setHorizontalTextPosition(SwingConstants.LEFT);
label2.setVerticalTextPosition(SwingConstants.TOP);
jtftotalInv = new JTextField (30);
jtftotalInv.setEditable(false);
add(label10);
add(jtftotalInv);
Icon DVD = new ImageIcon(getClass().getResource ("dvd.jpg"));
label11 = new JLabel(DVD, SwingConstants.LEFT);
label11.setVerticalTextPostition(SwingConstants.TOP);
add(label11);
btnFirst = new JButton("First");
btnFirst.addActionListener(this);
add(btnFirst);
btnPrevious = new JButton("Previous");
btnPrevious.addActionListener(this);
add(btnPrevious);
btnNext = new JButton("Next");
btnNext.addActionListener(this);
add(btnNext);
btnLast = new JButton("Last");
btnLast.addActionListener(this);
add(btnLast);
btnAdd = new JButton("Add");
btnAdd.addActionListener(this);
add(btnAdd);
btnDelete = new JButton("Delete");
btnDelete.addActionListener(this);
add(btnDelete);
btnModify = new JButton("Modify");
btnModify.addActionListener(this);
add(btnModify);
btnSave = new JButton("Save");
btnSave.addActionListener(this);
add(btnSave);
btnSearch = new JButton("Search");
btnSearch.addActionListener(this);
add(btnSearch);
btnLoadfile = new JButton("Load file");
btnLoadfile.addActionListener(this);
add(btnLoadfile);
setSize(800,500);
setVisible(true);
}//end Label Frame constructor
void setDVD(DVD[] dvd) {
this.dvd = dvd;
index = 0;
display();
}
void display() {
jtfItem.setText("" + dvd[index].getName());
jtfVideo.setText("" + dvd[index].getTitle());
jtfYear.setText("" + dvd[index].getYear());
jtfitmNumber.setText("" + dvd[index].getNumber());
jtfStock.setText("" + dvd[index].getQuantityOnHand());
jtfReStock.setText("" + dvd[index].getRestockingFee());
jtfPrice.setText("" + dvd[index].getPrice());
jtfValue.setText("" + dvd[index].getItemValue());
jtftotalInv.setText("" + dvd[index].CalculateTotalInventoryValue());
}
void displayFirst() {
index = 0;
display();
}
void displayNext() {
index++;
if(index >= dvd.length) {
index = 0;
}
display();
}
void displayPrevious() {
index--;
if(index < 0) {
index = dvd.length - 1;
}
display();
}
void displayLast() {
index = dvd.length - 1;
display();
}
public void actionPerformed(ActionEvent e) {
Object buttonPressed = e.getSource();
if (buttonPressed == btnFirst) {
index = 0;
displayFirst();
return;
}
if (buttonPressed == btnNext) {
displayNext();
return;
}
if (buttonPressed == btnPrevious) {
displayPrevious();
return;
}
if (buttonPressed == btnLast) {
displayLast();
return;
}
display();
}
private JTextField newJTextField(int i) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Here are my errors:
Compiling 3 source files to C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\InventoryProgram\build\classes
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\InventoryProgram\src\InventoryProgram\GUI.java:144: cannot find symbol
symbol : method setVerticalTextPostition(int)
location: class javax.swing.JLabel
label11.setVerticalTextPostition(SwingConstants.TOP);
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\InventoryProgram\src\InventoryProgram\GUI.java:203: cannot find symbol
symbol : method getTitle()
location: class InventoryProgram.DVD
jtfVideo.setText("" + dvd[index].getTitle());
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\InventoryProgram\src\InventoryProgram\GUI.java:205: cannot find symbol
symbol : method getNumber()
location: class InventoryProgram.DVD
jtfitmNumber.setText("" + dvd[index].getNumber());
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\InventoryProgram\src\InventoryProgram\GUI.java:210: cannot find symbol
symbol : method CalculateTotalInventoryValue()
location: class InventoryProgram.DVD
jtftotalInv.setText("" + dvd[index].CalculateTotalInventoryValue());
C:\Documents and Settings\Tammy\My Documents\NetBeansProjects\InventoryProgram\src\InventoryProgram\Inventory.java:36: cannot find symbol
symbol : method CalculateTotalInventoryValue(InventoryProgram.DVD[])
location: class InventoryProgram.DVD
Double totalInv = library[0].CalculateTotalInventoryValue(library);
5 errors
BUILD FAILED (total time: 0 seconds)
Thanks for any help I can get.
This post has been edited by javagreenhorn: 27 Jun, 2008 - 04:39 PM