Hello,

Can anyone please help me... I need to create a GUI program that holds product inventory.

I have five errors and I can't figure out what they are... I need a second look from someone... Thanks in advance!

import javax.swing.*;
import java.awt.*;
import java.text.NumberFormat;
import java.util.Locale;

public class Product extends JFrame {

   public Product()
   {
		super("Product Inventory");


		Container container = getContentPane();

        container.setLayout(new FlowLayout());

  		ProductList[] inventory = new ProductList[4];

  		inventory[0] = new ProductList( "CEB32000", "Highlighter", 30, 5.75, .05 );
  		inventory[0] = new ProductList( "CEB32001", "Manila Folder", 45, 3.25, .05 );
  		inventory[0] = new ProductList( "CEB32002", "Scissors", 36, 2.75, .05 );
  		inventory[0] = new ProductList( "CEB32003", "Comb Bind", 50, 5.35, .05 );
  		inventory[0] = new ProductList( "CEB32004", "Glue Sticks", 24, 1.29, .05 );


        //Sorted ProductList

        Supplies supplies = new Supplies();
        supplies.sortSupplies(inventory);


		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

		for(int j1=0; j1 < inventory.length; ++j1)

		{
			String labelText =
			" Item Number: " + inventory[j1].getItemNumber() +
			" Product Name: " + inventory[j1].getProductName() +
			" Quantity: " + inventory[j1].getQuantity() +
			" Unit Price: " + inventory[j1].getUnitPrice() +
			" Restock Fee: " + inventory[j1].getRestockFee() +
			" Total Value: " + inventory[j1].value();


			JLabel textLabel = new JLabel(labelText, JLabel.LEFT);
			container.add(textLabel);
		}


  		String labelText =
  		"<html><br>Total value of all printers in stock: " + (supplies.getTotalValue(inventory));
  		JLabel TotalValue = new JLabel(labelText, JLabel.CENTER);
  		container.add(TotalValue);

   }//end HPprinters constructor

   class Supplies {

	   private String itemNumber;
	   private String itemName;
	   private int quantity;
	   private double unitPrice;

	   public Supplies() {

	   }

	   public  Supplies( String itemNumber, String itemName, int quantity, double unitPrice ) {

		   itemNumber = itemNumber;
		   itemName = itemName;
		   quantity = quantity;
		   unitPrice = unitPrice;

	   } //end four-argument constructor

		public void setItemNumber(String itemNumber) {
			itemNumber = itemNumber;
		} //end method

		public String getItemNumber() {
			return itemNumber;
		} //end method

		public void setItemName(String itemName) {
			itemName = itemName;
		} //end method

		//return
		public String getItemName() {
			return itemName;
		} //end method

		public void setQuantity(int quantity) {
			quantity = quantity;
		} //end method

		//return
		public int quantity() {
			return quantity;
		} //end method

		public void setUnitPrice(double unitprice) {
			unitPrice = unitPrice;
		} //end method

		//return
		public double unitprice() {
			return unitPrice;
		} //end  method

		//calculate inventory value
		public double value() {
			return unitPrice * quantity;
		} //end method value

		public double getTotalValue(Supplies [] inventory)
		{
			double total = 0.0;

			for(int i = 0; i < inventory.length; i++)
			{
				total += inventory[i].value();
			}
			return total;

		}


		public Supplies[] sortSupplies(Supplies[] inventory)
		{
			Supplies temp[] =  new Supplies[1];

			int i, k;

			for (i=1; i <inventory.length; i++)
			{
				for (k=0; k < inventory.length-i; k++)
				{
					if(inventory[k].getItemNumber().compareToIgnoreCase(inventory[k+1].getItemNumber())>0)
		            {
						// exchange elements
						temp [0] = inventory[k];
						inventory[k] = inventory[k+1];
						inventory[k+1] = temp[0];
		            }
				}
			}
		return inventory;
		}

	}//end class Supplies

	class ProductList extends Supplies {

		double restockFee;

		public ProductList(String itemNumber, String itemName, int quantity, double unitPrice, double restockFee )
		{
			super(itemNumber, itemName, quantity, unitPrice);
			this.restockFee = restockFee;
		}

		public double Fee(){
			return super.unitPrice() * restockFee;
		}

		public double value() {
			return  super.value() + (super.value() * restockFee);
		}

	}


	public static void main(String args []) {
		Product product = new Product();

		//get Content Pane

		product.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		product.setSize(1100,300);
		product.setVisible(true);
	}

}//end class Product

Oops... I forgot to include the error message.

Here it is...

C:\Documents and Settings\McDonley\My Documents\InventoryProgramPart3\Product.java:42: cannot find symbol
symbol : method getProductName()
location: class Product.ProductList
" Product Name: " + inventory[j1].getProductName() +
^
C:\Documents and Settings\McDonley\My Documents\InventoryProgramPart3\Product.java:43: cannot find symbol
symbol : method getQuantity()
location: class Product.ProductList
" Quantity: " + inventory[j1].getQuantity() +
^
C:\Documents and Settings\McDonley\My Documents\InventoryProgramPart3\Product.java:44: cannot find symbol
symbol : method getUnitPrice()
location: class Product.ProductList
" Unit Price: " + inventory[j1].getUnitPrice() +
^
C:\Documents and Settings\McDonley\My Documents\InventoryProgramPart3\Product.java:45: cannot find symbol
symbol : method getRestockFee()
location: class Product.ProductList
" Restock Fee: " + inventory[j1].getRestockFee() +
^
C:\Documents and Settings\McDonley\My Documents\InventoryProgramPart3\Product.java:169: cannot find symbol
symbol : method unitPrice()
location: class Product.Supplies
return super.unitPrice() * restockFee;
^
5 errors

Tool completed with exit code 1

Apparently you are tying to use methods that don't exist.
You don't just call methods just because you like it. You need yo declare them first

Hi javaAddict,

I made some changes...

now it is giving me one error;

C:\Documents and Settings\McDonley\My Documents\InventoryProgramPart3\Product.java:25: cannot access product
bad class file: .\product.class
class file contains wrong class: Product
Please remove or make sure it appears in the correct subdirectory of the classpath.
product highlighter = new Product( "CEB32000", "Highlighter", 30, 5.75, .05 );
^
1 error

Tool completed with exit code 1

I am new to Java and I am very confused. I would really appreciate any help.

here is my new code:

import javax.swing.*;
import java.awt.*;
import java.text.NumberFormat;
import java.util.Locale;

public class Product extends JFrame {

   public Product()
   {

		super("Product Inventory");


		Container container = getContentPane();

        container.setLayout(new FlowLayout());

  		Product[] product = new Product[5];

  		product highlighter = new Product( "CEB32000", "Highlighter", 30, 5.75, .05 );
		product manilaFolder = new Product( "CEB32001", "Manila Folder", 45, 3.25, .05 );
		product scissors = new Product( "CEB32002", "Scissors", 36, 2.75, .05 );
		product combbind = new Product( "CEB32003", "Comb Bind", 50, 5.35, .05 );
  		product gluesticks = new Product( "CEB32004", "Glue Sticks", 24, 1.29, .05 );

  		product[0] = highlighter;
  		product[1] = manilaFolder;
  		product[2] = scissors;
  		product[3] = combbind;
  		product[4] = gluesticks;



        //Sort ProductList

        Supplies supplies = new Supplies();
        supplies.sortSupplies(inventory);


		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

		for(int j1=0; j1 < inventory.length; j1++)

		{
			String labelText =
			" Item Number: " + inventory[j1].getItemNumber() +
			" *** Product Name: " + inventory[j1].getProductName() +
			" *** Quantity: " + inventory[j1].getQuantity() +
			" *** Unit Price: $" + inventory[j1].getUnitPrice() +
			" *** Restock Fee: $" + inventory[j1].getRestockFee() +
			" *** Total Value: $" + inventory[j1].value();

			JTextArea textArea = new JTextArea (80,70);
			textArea.setText(" ");
			textArea.setEditable(false);

			JFrame frame = new JFrame();
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.pack();
			frame.setLocationRelativeTo(null);
			frame.setVisible(true);
		}


  		String labelText =
  		"<html><br>Total value of Supply Inventory: " + (supplies.getTotalValue(inventory));
  		JLabel TotalValue = new JLabel(labelText, JLabel.CENTER);
  		container.add(TotalValue);

   }//end Product constructor

   class Supplies {

	   private String itemNumber;
	   private String itemName;
	   private int quantity;
	   private double unitPrice;

	   public Supplies() {

	   }

	   public  Supplies( String itemNumber, String itemName, int quantity, double unitPrice ) {

		   itemNumber = itemNumber;
		   itemName = itemName;
		   quantity = quantity;
		   unitPrice = unitPrice;

	   } //end four-argument constructor

		public void setItemNumber(String itemNumber) {
			itemNumber = itemNumber;
		} //end method

		public String getItemNumber() {
			return itemNumber;
		} //end method

		public void setItemName(String itemName) {
			itemName = itemName;
		} //end method

		//return
		public String getItemName() {
			return itemName;
		} //end method

		public void setQuantity(int quantity) {
			quantity = quantity;
		} //end method

		//return
		public int quantity() {
			return quantity;
		} //end method

		public void setUnitPrice(double unitPrice) {
			unitPrice = unitPrice;
		} //end method

		//return
		public double unitPrice() {
			return unitPrice;
		} //end  method

		//calculate inventory value
		public double value() {
			return unitPrice * quantity;
		} //end method value

		public double getTotalValue(Supplies [] inventory)
		{
			double total = 0.0;

			for(int i = 0; i < inventory.length; i++)
			{
				total += inventory[i].value();
			}
			return total;

		}


		public Supplies[] sortSupplies(Supplies[] inventory)
		{
			Supplies temp[] =  new Supplies[1];

			int i, k;

			for (i=1; i <inventory.length; i++)
			{
				for (k=0; k < inventory.length-i; k++)
				{
					if(inventory[k].getItemNumber().compareToIgnoreCase(inventory[k+1].getItemNumber())>0)
		            {
						temp [0] = inventory[k];
						inventory[k] = inventory[k+1];
						inventory[k+1] = temp[0];
		            }
				}
			}
		return inventory;
		}

	}//end class Supplies

	class ProductList extends Supplies {

		double restockFee;

		public ProductList(String itemNumber, String itemName, int quantity, double unitPrice, double restockFee)
		{
			super(itemNumber, itemName, quantity, unitPrice);
			this.restockFee = restockFee;
		}

		public double Fee(){
			return super.unitPrice() * restockFee;
		}

		public double value() {
			return  super.value() + (super.value() * restockFee);
		}

	}

}//end class Product

Java is case sensitive

Hello again,

I was able to compile it but my problem now is when I run it, it is not showing the entire inventory.

I know I am missing something but I can't find it. I would appreciate any help...

here is my updated code:

import javax.swing.*;
import java.awt.*;
import java.text.NumberFormat;
import java.util.Locale;

public class Product extends JFrame {


   public Product()
   {
		super("Product Inventory");

		Container container = getContentPane();
        container.setLayout(new FlowLayout());


		ProductList[] inventory = new ProductList[5];

  		inventory[0] = new ProductList( "CEB32000", "Highlighter", 30, 5.75, .05 );;
  		inventory[1] = new ProductList( "CEB32001", "Manila Folder", 45, 3.25, .05 );
  		inventory[2] = new ProductList( "CEB32002", "Scissors", 36, 2.75, .05 );
  		inventory[3] = new ProductList( "CEB32003", "Comb Bind", 50, 5.35, .05 );
  		inventory[4] = new ProductList( "CEB32004", "Glue Sticks", 24, 1.29, .05 );

  		JTextArea textArea = new JTextArea (40,30);
		textArea.setText(" ");
		textArea.setEditable(false);

		for( int i = 0; i < inventory.length; i++)
		{
			textArea.append("Product" + i + ": \t" + inventory[i] + "\n");
		}

		JFrame frame = new JFrame();
		frame.getContentPane().add(new JScrollPane(textArea));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

        //Sort ProductList

        Supplies supplies = new Supplies();
        supplies.sortSupplies(inventory);


		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

		for(int j1=0; j1 < inventory.length; j1++)

		{
			String labelText =
			" Item Number: " + inventory[j1].getItemNumber() +
			" *** Product Name: " + inventory[j1].getItemName() +
			" *** Quantity: " + inventory[j1].quantity() +
			" *** Unit Price: $" + inventory[j1].unitPrice() +
			" *** Restock Fee: $" + inventory[j1].restockFee() +
			" *** Total Value: $" + inventory[j1].value();
		}

			String labelText =
			 "<html><br>Total value of Supply Inventory: " + (supplies.getTotalValue(inventory));
			  JLabel TotalValue = new JLabel(labelText, JLabel.CENTER);
			  container.add(TotalValue);


   }//end Product constructor

   class Supplies {

	   private String itemNumber;
	   private String itemName;
	   private int quantity;
	   private double unitPrice;

	   public Supplies() {

	   }

	   public  Supplies( String itemNumber, String itemName, int quantity, double unitPrice ) {

		   itemNumber = itemNumber;
		   itemName = itemName;
		   quantity = quantity;
		   unitPrice = unitPrice;

	   } //end four-argument constructor

		public void setItemNumber(String itemNumber) {
			itemNumber = itemNumber;
		} //end method

		public String getItemNumber() {
			return itemNumber;
		} //end method

		public void setItemName(String itemName) {
			itemName = itemName;
		} //end method

		//return
		public String getItemName() {
			return itemName;
		} //end method

		public void setQuantity(int quantity) {
			quantity = quantity;
		} //end method

		//return
		public int quantity() {
			return quantity;
		} //end method

		public void setUnitPrice(double unitPrice) {
			unitPrice = unitPrice;
		} //end method

		//return
		public double unitPrice() {
			return unitPrice;
		} //end  method

		//calculate inventory value
		public double value() {
			return unitPrice * quantity;
		} //end method value

		public double getTotalValue(Supplies [] inventory)
		{
			double total = 0.0;

			for(int i = 0; i < inventory.length; i++)
			{
				total += inventory[i].value();
			}
			return total;

		}


		public Supplies[] sortSupplies(Supplies[] inventory)
		{
			Supplies temp[] =  new Supplies[1];

			int i, k;

			for (i=1; i <inventory.length; i++)
			{
				for (k=0; k < inventory.length-i; k++){

				}
			}
		return inventory;
		}

	}//end class Supplies

	class ProductList extends Supplies {

		double restockFee;

		public ProductList(String itemNumber, String itemName, int quantity, double unitPrice, double restockFee)
		{
			super(itemNumber, itemName, quantity, unitPrice);
			this.restockFee = restockFee;
		}

		public double restockFee(){
			return super.unitPrice() * restockFee;
		}

		public double value() {
			return  super.value() + (super.value() * restockFee);
		}

	}


	public static void main(String args[])
    {
    	Product product = new Product();
    }

}//end class Product

Are you having problems displaying this?

String labelText =
			 "<html><br>Total value of Supply Inventory: " + (supplies.getTotalValue(inventory));

JLabel TotalValue = new JLabel(labelText,JLabel.CENTER);
container.add(TotalValue);

Then the answer is simple.

First, your class extends JFrame, but inside you create a new JFrame and put the inventory elements in it, and you display that.

But the above code:

JLabel TotalValue = new JLabel(labelText,JLabel.CENTER);
container.add(TotalValue);

You add the "TotalValue" label to the "container". That "container" is only initialized and not used anywhere. You display the local JFrame:

JFrame frame = new JFrame();

frame.getContentPane().add(new JScrollPane(textArea));		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);

frame.setVisible(true);

But the total value you put it in the container taken from the class you extend. And you don't have code that displays it.

You don't need to create a new JFrame because your class is already a JFrame.
When you do this: frame.getContentPane() is like doing this: Container container = getContentPane(); because you call the getContentPane of "this" class, which is a JFrame.

I am not familiar with using contentPane. I use another approach when it comes to GUI, but try this:

//JFrame frame = new JFrame();

container.add(new JScrollPane(textArea));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

....
....

container.add(TotalValue);

pack();
setLocationRelativeTo(null);
setVisible(true);

instead of this:

JFrame frame = new JFrame();

frame.getContentPane().add(new JScrollPane(textArea));		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);

frame.setVisible(true);

Hi,

I was able to compile and run it and get the intended output.

I was missing one method. Thank you so much for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.