for my assignment I am to only add the sort and total inventory methods in the inventory class. And not create an array in the inventory class at all. My inventory class should contain all the variables needed to use with the two new methods: sort and calculate total inventory.By not creating any array in the inventory class and not touching the variables at all.
For the inventoryTest class, I only need to declare an array of the inventory class by making an instance of the inventory. I do not know how to fix the errors and I do not know what went wrong. What I have is:

public class inventory
{

private int prodNumber; // product number
private String prodName; // product name
private int unitsTotal; // total units in stock
private double unitPrice; // price per unit
private double totalInventory; // amount of total inventory

// initialize four-argument constructor

public inventory( int number, String name, int total, double price)
{

prodNumber = number;
prodName = name;
setUnitsTotal (total);
setUnitPrice (price);

} // end four-argument constructor

public void setProdNumber( int number )
{
prodNumber = number;
}

public int getprodNumber()
{
return prodNumber;
}

public void setprodName( String name)
{
prodName = name;
}

public String getprodName()
{
return prodName;
}

public void setUnitsTotal( int total )
{
unitsTotal = ( total = 20 );
}

public int getUnitsTotal()
{
return unitsTotal;
}

public void setUnitPrice( double price )
{
unitPrice = ( price = 0.0 );
}

public double getUnitPrice()
{
return unitPrice;
}

// calculate total iventory

public double getValue()
{
return unitsTotal * unitPrice;
}

//product sorting

public int compareTo (Object o)
{
inventory s = (inventory)o;

return prodName.compareTo ( s. getprodName() );
}

//return String inventory infomation

public String toString()
{

System.out.println();return"prodNumber;" +prodNumber+ "\nprodName;" +prodName+ "\nunitPrice:$" +unitPrice+   "\nunitTotal:"+unitsTotal+ "\nValue:$" +getValue();

}

} // end class inventory



public class inventoryTest
{
public static void main( String[] args )
{
//create product array for baby items
inventory[]products = new inventory[5];

// baby item inventory
inventory p1 = new inventory(101, "Diapers", 20, 7.0);
inventory p2 = new inventory(201, "Bottles", 20, 5.0);
inventory p3 = new inventory(301, "Formula", 20, 10.0);
inventory p4 = new inventory(401, "Pacifiers", 20, 2.0);
inventory p5 = new inventory(501, "Wipes", 20, 4.0);

product[0] = p1;
product[0] = p2;
product[0] = p3;
product[0] = p4;
product[0] = p5;

double total = 0.0;

for(int i=0;i<6;i++)
{
total = total + products[i].getValue();
}
//Diplay Inventory total Value

system.out.printf("Total value of entire Inventory is: $%f", total);

system.out.println();

Arrays.sort(products);

for(inventory s: products)
{

system.out.println(s);
system.out.println();

}
} // end main method
} // end class inventoryTest

The errors I am getting are:

F:\Java Programming\inventoryTest.java:15: error: cannot find symbol
product[0] = p1;
^
symbol: variable product
location: class inventoryTest
F:\Java Programming\inventoryTest.java:16: error: cannot find symbol
product[0] = p2;
^
symbol: variable product
location: class inventoryTest
F:\Java Programming\inventoryTest.java:17: error: cannot find symbol
product[0] = p3;
^
symbol: variable product
location: class inventoryTest
F:\Java Programming\inventoryTest.java:18: error: cannot find symbol
product[0] = p4;
^
symbol: variable product
location: class inventoryTest
F:\Java Programming\inventoryTest.java:19: error: cannot find symbol
product[0] = p5;
^
symbol: variable product
location: class inventoryTest
F:\Java Programming\inventoryTest.java:28: error: package system does not exist
system.out.printf("Total value of entire Inventory is: $%f", total);
^
F:\Java Programming\inventoryTest.java:30: error: package system does not exist
system.out.println();
^
F:\Java Programming\inventoryTest.java:32: error: cannot find symbol
Arrays.sort(products);
^
symbol: variable Arrays
location: class inventoryTest
F:\Java Programming\inventoryTest.java:36: error: package system does not exist
system.out.println(s);
^
F:\Java Programming\inventoryTest.java:38: error: package system does not exist
system.out.println();
^
10 errors

The compiler does a very good job in telling you what is confusing it. Its your job to figure out what can you do to fix it. Lets just examine few of the errors that you've got.

symbol: variable product
location: class inventoryTest
F:\Java Programming\inventoryTest.java:16: error: cannot find symbol
product[0] = p2;

This above tells you that a variable called 'product' hasn't been defined But is being used. Isn't this true? Look around your code and see if you are missing out on the declaring 'product'. Solving this would essentially solve 5 errors from your code.

Next lets examine this,
F:\Java Programming\inventoryTest.java:32: error: cannot find symbol Arrays.sort(products);

In the above error, the compiler tells you that on line 32, you have used a term called 'products' but it's not able to find it. You would then want to see if products has been defined correctly. (Think about it.. are you missing any spaces.)

This would solve one more error out of your 10. And only 4 errors of the same type remain.

This is a mistake in using system.out.println, Refer back to a working helloworld program in java. Are you missing out on something? Isn't java case sensitive..

If you figure that out.. you can fix your last four.

De-bugging compiler errors are fun. Your compiler tells you exactly where it has trouble all we need to do is solve those. :)

Hope this helps.

for the product, your variable actually is named 'products' with an s.

but besides that, do you see why this part of your code:

product[0] = p1;
product[0] = p2;
product[0] = p3;
product[0] = p4;
product[0] = p5;

is pointless?

you'll have only the details of p5 at the end of this block, since you've overwritten all the other ones.

I've fixed the errors and compiled successfully and when I run it I get Exception in thread "main java.lang.ArrayIndexOutOfBoundsException:5 at inventoryTest.main <inventoryTest.java: 28>
my new coded program with the changes.

import java.util.Arrays;

public class inventoryTest
{
public static void main( String[] args )
{

//create product array for baby items
inventory[]inventory = new inventory[5];

// baby item inventory
inventory p1 = new inventory(101, "Diapers", 20, 7.0);
inventory p2 = new inventory(201, "Bottles", 20, 5.0);
inventory p3 = new inventory(301, "Formula", 20, 10.0);
inventory p4 = new inventory(401, "Pacifiers", 20, 2.0);
inventory p5 = new inventory(501, "Wipes", 20, 4.0);

inventory[0] = p1;
inventory[1] = p2;
inventory[2] = p3;
inventory[3] = p4;
inventory[4] = p5;

double total = 0.0;

for(int i=0; i <6;i++)
{
total = total + inventory[i].getValue();
}

//Diplay Inventory total Value
System.out.printf("Total value of entire Inventory is: $%f", total);
System.out.println();

Arrays.sort(inventory);

for(inventory s: inventory)
{
System.out.println(s);
System.out.println();
}
} // end main method
} // end class inventoryTest 

Your array has entries, [0] thru [4].
You try to access a sixth elementinventory[5] because your loop on line 26 goes from i=0 to i=5

that's normal

for(int i=0; i <6;i++)

here you are iterating over the arrays first six elements, but the array only has five.

so, you are trying to read the elements with indices 0 through 5,
but, an array with five elements only has elements from 0 through 4.

change the 6 in that line to 5

Thanks for helping me, I've made changes and now I get
Total value of entire Inventory is: $0.00
Exception in thread "main" java.lang.ClassCastException: inventory cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending <ComparableTimSort.java:316>
atjava.util.ComparableTimSort.sort <ComparableTimSort.java: 184>
at inventoryTest.main <inventoryTest.java:36>

you are trying to sort your array, in a way that uses the implementation of the Comparable interface, but you didn't provide an implementation, and your class doesn't implement Comparable.

implement that interface, and in the method to implement, write the code that defines how your elements will should be sorted.

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.