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

What are the problem's synmptoms?

When you write this:

public void setName(String name)
{
   name = name;
}

Both "name" in name = name; refer to the argument. So you are just changing the value of the argument and you are putting the value of the argument back at the argument.

If you want to put the value of the argument "name" into the private property of the class then:

public void setName(String name)
{
   this.name = name;
}

Now "this.name" is the name defined in the class and "name" is the argument.

I changed it but I still got this error message:
Total value of entire inventory is:$0.00
Exception in thread "main" java.lang.NullPointerException
at Book compareTo(inventoryPart2.java:114
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at InventoryPart2.main(InventoryPart2.java:180)

Exception in thread "main" java.lang.NullPointerException
at Book compareTo(inventoryPart2.java:114

At line 114 of the inventoryPart2.java class you are trying to use something that it is null. There is a reason why you get such an explanatory message; so you can look it up yourself.
Not to mention that you didn't post the part of the code where you got that error.

Thanks, I got it. Big 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.