I have completed a program and it compiles fine. When I run it, it seems to run fine. The only thing is that I am missing element data for two of the objects I am printing out in the text area of this JFrame. I an array called printerProduct[5], that has three objects of type Supplier, and two of type Printer. I am needing to display the information for each object, plus a restocking fee for each object, and a total value for the entire inventory. What I don't understand is why I am missing information on one object from Supplier, and on one object from Printer. This program isn't the only one that does this. I had the instructor send me a program without the GUI code included and her's does the same thing...missing information on objects from different classes. I am not using an IDE, I am using TextPad, but I have never had a problem with this text editor before. I have included the full program below if you want to compile it and see for yourself. The items that are missing are the Supplier Name, and the Restocking Fee. I don't have many notes in the program yet, so please subdue the critique monster. I want to make sure everything works fine before I add them all.

import java.util.Arrays;//  Importsw in order for
import java.io.*;       //  the program to run
import java.text.NumberFormat;//  correctly
import java.util.Locale;      // without compile or runtime errors
import javax.swing.*;

public class InventoryPart3//  declares class for InventoryPart1

{
	public static  Printer[] sortArray (Printer[] product)//  sort method for sorting the arrays by product name

	    {
			String[] names = new String [product.length];
			Printer [] sorted = new Printer[product.length];

			for (int i = 0; i < product.length; i++)
			 {
			    names[i] = product[i].getProductName();
			 }

				Arrays.sort(names);

				for (int i = 0; i < product.length; i++)
					{
						for (int j = 0; j < names.length; j++)
						{
							if (names[j].equalsIgnoreCase(product[i].getProductName()))
							{
								sorted[j] = product[i];
								break;
							}
						}
					}
					  return sorted;// returns arrays sorted by product name
			}

	public static double calculateInventoryTotal(Printer[]printerProduct){
		double total = 0;
		for (int i = 0; i < printerProduct.length; i++){
			total+= printerProduct[i].getInventoryValue();
			}
			return total;

		}

    public static void main(String args[])//  main method

    {
		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);//  formats the numbers for dollar values



  		Printer[]printerProduct = new Printer[5];//  instantiates new array from Printer class

        Supplier kodak = new Supplier(1437, "InkSmart", 17, 29.99, "Kodak");
        Printer lexmark = new Printer(2598, "All-In-One", 23, 34.89 );
        Supplier saavin = new Supplier(9956, "PortaPrint", 21, 21.99, "Saavin" );
        Printer kodak1 = new Printer(1112, "PageSmart", 15, 59.99);
		Supplier canon = new Supplier(1145, "LaserJet", 14, 24.99, "Canon");

		printerProduct [0] = kodak;
		printerProduct [1] = lexmark;
		printerProduct [2] = saavin;
		printerProduct [3] = kodak1;
		printerProduct [4] = canon;

		printerProduct = sortArray(printerProduct);

		double inventoryTotal = calculateInventoryTotal(printerProduct);






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



		for (int i = 0; i < printerProduct.length; i++){
			textArea.append("\n" +printerProduct[i].toString() + "\n");

		}

		textArea.append("\n\nInventory Total Value: " + nf.format(inventoryTotal)+ "\n\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);
		frame.setTitle("PRINTER INVENTORY");




     }// ends main method





}// ends public class InventoryPart3
import javax.swing.*;
import java.text.NumberFormat;
import java.util.Locale;

 class Printer// class Printer declaration
{

	public int itemNumber;// variable declarations for class Printer
	public String productName;
	public int inStock;
	public double unitPrice;
	NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);




	public Printer (int itemNumber, String productName, int inStock, double unitPrice )//  declares object's variavle types, and names

	{
		this.itemNumber = itemNumber;
		this.productName = productName;
		this.inStock = inStock;
		this.unitPrice = unitPrice;

	}



	public void setItemNumber (int itemNumber)//  method sets itemNumber variable
	{
		this.itemNumber = itemNumber;
	}

	public int getItemNumber ()//  get method for itemNumber

	{
		return itemNumber;
	}


	public void setProductName (String productName)// set method for productName

	{
		this.productName = productName;
	}


	public String getProductName()//  get method for productName

	{
		return productName;
	}

	public void setInStock(int inStock)// set method for inStock

	{
		this.inStock = inStock;
	}

	public int getInStock()//  get method for inStock

	{
		return inStock;
	}

	public void setUnitPrice (double unitPrice)//  set method for unitPrice

	{
		this.unitPrice = unitPrice;
	}

	public Double getUnitPrice ()//  get method for unitPrice

	{
		return  unitPrice;
	}

	public Double getInventoryValue ()//  get method for inventoryValue. Calculates value of entire inventory

	{
		return  unitPrice * inStock;
	}

	public  String toString (){

		return  "Printer Name:  "  + productName + "\n" +
				"Item Number:   "  + itemNumber + "\n" +
				"In Stock:      "  + inStock + "\n" + "Unit Price:    "
				+ nf.format(unitPrice) + "\n" + "Item Total:    " +
				nf.format(getInventoryValue());
				}
}


	class Supplier extends Printer// class Supplier declaration extending class Printer
	{

		private  String supplierName;//  declares attribute

		public  Supplier(int itemNumber, String productName, int inStock, double unitPrice, String supplierName)
		{
			super ( itemNumber, productName, inStock, unitPrice);

			this.supplierName = supplierName;
		}

		public void setSupplierName(String supplierName)// set method for supplierName variable
		{
			this.supplierName = supplierName;
		}

		public String getSupplierName()// get method for supplierName variable
		{
			return supplierName;
		}



		public double calculateRestockFee()//  method for calcualating restock fee
		{
			return super.getInventoryValue() * .05;
		}

		public  String toString (){
			return super.toString() + "\n" + "Supplier Name: " + supplierName + "\n"
				+ "Restock Fee:   " + nf.format(calculateRestockFee());
				}

	}

Recommended Answers

All 2 Replies

I am missing information on one object from Supplier, and on one object from Printer.

Can you show us the output where that info is missing (add comments like <<< XXX missing here
and show us what you want the output to look like?

What are the variable names for the missing fields?

I want to make sure everything works fine before I add them all.

That implies that you don't have a design before you write the code.
Normally you'd design the program and write comments describing what each section of the code was supposed to do and then write the code to do it.

Can you show us the output where that info is missing (add comments like <<< XXX missing here
and show us what you want the output to look like?


That implies that you don't have a design before you write the code.
Normally you'd design the program and write comments describing what each section of the code was supposed to do and then write the code to do it.

I was incorrect. The missing attributes are from the two Printer objects in the array. After finding this, I understand why they are not printing out.I changed the code and everything works as it should now. Thanks for the response. Now I get to ad the rest of my notes.

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.