Could someone check my program, I cannot figure out what I am doing wrong.
I did this part and the program ran just fine - Part 1; Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit. Create a Java application that displays the product number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory (the number of units in stock mulitplied by the price of each unit).
Part 2 (This is the code that is listed and having a problem with) Modify the inventory Program so the application can handle multiple items. Use an array to store the items. The output 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 products. In addition, the output should display the value of the entire inventory. Create a method to calculate the value of the entire inventory. Create another method to sort the array items by the name of the product.
/Creating Inventory Program for Books
import java.util.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;
class Book
{
private String name;// class variable stores item name
private int number;// class variable stores item number
private double price;// class variable stores price
private int quantity;// class variable stores quantity
public Book(String name, int number, double price, int quantity)// Constructor
{
name = name;
number = number;
price = price;
quantity = quantity;
}
// method to set book name
public void setName(String name)
{
name = name;
}
// method to get book name
public String getName()
{
return name;
}
// method to set book number
public void setNumber(int number)
{
number = number;
}
// method to get book number
public int getNumber()
{
return number;
}
// method to set book price
public void setPrice(double price)
{
price = price;
}
// method to get price
public double getdouble()
{
return price;
}
// method to set book quantity
public void setQuantity(int quantity)
{
quantity = quantity;
}
// method to get book quantity
public int getQuantity()
{
return quantity;
}
// method to calculate inventory value
public double calculateInventoryValue()
{
return price * quantity;
}
// product sorting
public int compareTo (Object o)
{
Book s = (Book)o;
return name.compareTo(s.getName());
}
// return string book information
public String toString()
{
System.out.println();return"Name;"+name+ "\nNumber;"+number+"\nPrice:$"+price +"\nQuantity:"+quantity+ "\nValue:$ "+calculateInventoryValue();
}
}// end class book
public class InventoryPart2
{
//main method begin execution of java application
public static void main(String argus[])
{
//create product array for books
Book[]products = new Book[5];
// book inventory
Book p1 = new Book("Harry Potter", 10, 15.99, 27);
Book p2 = new Book("Eragon", 14, 10.99, 45);
Book p3 = new Book("Letters to my Daughter", 02, 25.99, 14);
Book p4 = new Book("Twlight", 20, 24.79, 50);
Book p5 = new Book("Eldest", 15, 11.99, 25);
products[0] = p1;
products[1] = p2;
products[2] = p3;
products[3] = p4;
products[4] = p5;
double total = 0.0;
for(int i=0;i<6;i++)
{
total = total + products[i].calculateInventoryValue();
}
// Display Inventory total value
System.out.printf("Total value of entire Inventory is:$%.2f",total);
System.out.println();
Arrays.sort(products);
for(Book s:products)
{
System.out.println(s);
System.out.println();
}
}// end main method
}// end class InventoryPart2