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