Hello,
I am supposed to modify my last inventory program. Here is the instructions. 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 product. 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.

I feel completely lost as this is an online class and I have no one to ask for help. I have a hard time learning from readings.

Here is my code so far, if anyone can help me it would be so great cause I am stuck.

/* Public class: Inventory1.java
 * Program displays inventory.
 */
/** Inventory1.java
 *  Author Melissa
 *  IT215 Java
 *  10/29/2014
 */





 class Pizza {

       private Integer itemNumber;// Item number
       private String productName;// Product name
       private Integer productInStock;// Number of product in stock
       private double productPrice;// Price per product

     // Consturctor
      public Pizza( Integer inlItemNumber, String inlProductName, Integer inlProductInStock, double inlProductPrice)
    {
        // accepts four parameters
       setItemNumber(inlItemNumber);
       setProductName(inlProductName);
       setProductInStock(inlProductInStock);
       setProductPrice(inlProductPrice);
    }//End constructor

    public Integer getItemNumber(){
        return this.itemNumber;
    }
    public void setItemNumber(Integer inlItemNumber){
        this.itemNumber = inlItemNumber;
    }
    public String getProductName(){
        return this.productName;
    }
    public void setProductName(String inlProductName){
        this.productName = inlProductName;
    }        
    public Integer getProductInStock(){
        return this.productInStock;
    }
    public void setProductInStock(Integer inlProductInStock){
        this.productInStock = inlProductInStock;
    }
    public double getProductPrice(){
        return this.productPrice;
    }
    public void setProductPrice(double inlProductPrice){
        this.productPrice = inlProductPrice;
    }//end




    // Calculation methods
    public double CalculateTotalProductValue ()  // begin calculateTotalProductValue method
    {
          return getProductInStock() * getProductPrice();
    }  // end calculateTotalProductValue method



    public double CalculateTotalInventoryValue()
    {
        return CalculateTotalProductValue() * getProductInStock();

    }

} // end  Pizza class


public class Inventory1 {


    public static void main(String[] args) 
    {

        //instantiate Pizza array

        Pizza myPizza[] = new Pizza[5];

        myPizza[0] = new Pizza (1234, "Dough", 22, 1.59);
        myPizza[1] = new Pizza (1235, "Sauce", 35, 5.99);
        myPizza[2] = new Pizza (1236, "Cheese", 4, 15.99);
        myPizza[3] = new Pizza (1237, "Pepperoni", 3, 14.99);
        myPizza[4] = new Pizza (1238, "Ham", 2, 16.99);



        //output
        for (Pizza myPizza1 : myPizza) {
            System.out.println(" The product Number: " + myPizza1.getItemNumber());
            System.out.println(" The product Name: " + myPizza1.getProductName());
            System.out.println(" The number of product in Stock: " + myPizza1.getProductInStock());
            System.out.printf(" The price per product is: ", +myPizza1.getProductPrice());
            System.out.printf(" The total product value is: ", +myPizza1.CalculateTotalProductValue());
        }   
         //output total inventory value
         double total = 0.0;
         for (int i = 0; i < 5; i++){ 
         total +=  myPizza[i].CalculateTotalInventoryValue();            
        }
           System.out.printf("Total Value of Inventory is: ", total);





    }  // end main method
}//end inventory class

This is the output which is not desired:

The product Number: 1234
The product Name: Dough
The number of product in Stock: 22
The price per product is: The total product value is: The total inventory value is: 0.0
Item number is 1234
The product name is Dough
The number of products in stock are: 22
The price of each product is: $1.59
The product inventory value is: $34.98
The Total inventory value is: $0.00
The product Number: 1235
The product Name: Sauce
The number of product in Stock: 35
The price per product is: The total product value is: The total inventory value is: 0.0
Item number is 1235
The product name is Sauce
The number of products in stock are: 35
The price of each product is: $5.99
The product inventory value is: $209.65
The Total inventory value is: $0.00
The product Number: 1236
The product Name: Cheese
The number of product in Stock: 4
The price per product is: The total product value is: The total inventory value is: 0.0
Item number is 1236
The product name is Cheese
The number of products in stock are: 4
The price of each product is: $15.99
The product inventory value is: $63.96
The Total inventory value is: $0.00
The product Number: 1237
The product Name: Pepperoni
The number of product in Stock: 3
The price per product is: The total product value is: The total inventory value is: 0.0
Item number is 1237
The product name is Pepperoni
The number of products in stock are: 3
The price of each product is: $14.99
The product inventory value is: $44.97
The Total inventory value is: $0.00
The product Number: 1238
The product Name: Ham
The number of product in Stock: 2
The price per product is: The total product value is: The total inventory value is: 0.0
Item number is 1238
The product name is Ham
The number of products in stock are: 2
The price of each product is: $16.99
The product inventory value is: $33.98
The Total inventory value is: $0.00
BUILD SUCCESSFUL (total time: 0 seconds)

Recommended Answers

All 7 Replies

Can you point out what is wrong in the output?

The output is not supposed to repeat the same thing. Just needs to print out each product once. And the total inventory value is 0.00. Not sure how to fix that.

The error's are in line 99,100 and 107. because ur calling methond 'printf' and inside, u doesn't put variables for this values. example:

System.out.printf("the double value is: %f", obj.getAnDoubleValue());

see the %f <-- is an variable that indicates an double value can be pushed.
u can use %d <-- if u want use digits (integer, long, byte, short)
u can use %s <-- if u want use Strings
...
here are the solutions, both are valids and works! using System.out.println and System.out.printf
line 99

System.out.println(" The price per product is: " + myPizza1.getProductPrice());
System.out.printf(" The price per product is: %f", myPizza1.getProductPrice());

line 100

System.out.println(" The total product value is: " + myPizza1.CalculateTotalProductValue());
System.out.printf(" The total product value is: %f", myPizza1.CalculateTotalProductValue());

and finally line 107

System.out.println("Total Value of Inventory is: "+ total);
System.out.printf("Total Value of Inventory is: %f", total);

Please read more about printf method in java and variables inside of this methods. If this thread help u, make as solved, another posts too. do not leave the thread open.

Have a nice day.

Hello,

I changed the code as you said and now I am getting this as an output:

 The product Number: 1234
 The product Name: Dough
 The number of product in Stock: 22
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%f'
 The price per product is: 1.590000 The total product value is: 0.000000 The total inventory value is:  at java.util.Formatter.format(Formatter.java:2519)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at Inventory1.main(Inventory1.java:125)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

error at line 125? .... sure you added more logic ! ...
i use this! ... is the same (line 82-line107)

//instantiate Pizza array
        Pizza myPizza[] = new Pizza[5];
        myPizza[0] = new Pizza (1234, "Dough", 22, 1.59);
        myPizza[1] = new Pizza (1235, "Sauce", 35, 5.99);
        myPizza[2] = new Pizza (1236, "Cheese", 4, 15.99);
        myPizza[3] = new Pizza (1237, "Pepperoni", 3, 14.99);
        myPizza[4] = new Pizza (1238, "Ham", 2, 16.99);
        //output
        for (Pizza myPizza1 : myPizza) {
            System.out.println(" The product Number: " + myPizza1.getItemNumber());
            System.out.println(" The product Name: " + myPizza1.getProductName());
            System.out.println(" The number of product in Stock: " + myPizza1.getProductInStock());
            System.out.println(" The price per product is: " + myPizza1.getProductPrice());
            System.out.println(" The total product value is: " + myPizza1.CalculateTotalProductValue());
            System.out.printf(" The price per product is: %f", myPizza1.getProductPrice());
            System.out.printf(" The total product value is: %f", myPizza1.CalculateTotalProductValue());
        }   
         //output total inventory value
         double total = 0.0;
         for (int i = 0; i < 5; i++){ 
         total +=  myPizza[i].CalculateTotalInventoryValue();            
        }
           System.out.println("Total Value of Inventory is: "+ total);
           System.out.printf("Total Value of Inventory is: %f", total);

and the results are...
how u can use... there are duplicated values, is only for example purposes.

The product Number: 1234
 The product Name: Dough
 The number of product in Stock: 22
 The price per product is: 1.59
 The total product value is: 34.980000000000004
 The price per product is: 1,590000 The total product value is: 34,980000 The product Number: 1235
 The product Name: Sauce
 The number of product in Stock: 35
 The price per product is: 5.99
 The total product value is: 209.65
 The price per product is: 5,990000 The total product value is: 209,650000 The product Number: 1236
 The product Name: Cheese
 The number of product in Stock: 4
 The price per product is: 15.99
 The total product value is: 63.96
 The price per product is: 15,990000 The total product value is: 63,960000 The product Number: 1237
 The product Name: Pepperoni
 The number of product in Stock: 3
 The price per product is: 14.99
 The total product value is: 44.97
 The price per product is: 14,990000 The total product value is: 44,970000 The product Number: 1238
 The product Name: Ham
 The number of product in Stock: 2
 The price per product is: 16.99
 The total product value is: 33.98
 The price per product is: 16,990000 The total product value is: 33,980000Total Value of Inventory is: 8566.019999999999
Total Value of Inventory is: 8566,020000

Thankyou so much for the help. I am getting 0.0 for the total inventory value, which prints out fine for you. What am I doing wrong? Here is the updated code:

  /* Public class: Inventory1.java
     * Program displays inventory.
     */
    /** Inventory1.java
     *  Author Melissa
     *  IT215 Java
     *  10/25/2014
     */




     class Pizza {

           private int itemNumber;// Item number
           private String productName;// Product name
           private int productInStock;// Number of product in stock
           private double productPrice;// Price per product
           private double totalProductValue;// Products total value
            //Default constructor
        public Pizza(){

            itemNumber = 0;
            productName = "";
            productInStock = 0;
            productPrice = 0.0;
            totalProductValue = productPrice * productInStock;

        }
        // Consturctor
        public Pizza( int number, String name, int inStock, double price)
        {
           itemNumber = number;
           productName = name;
           productInStock = inStock;
           productPrice = price;

        }//End constructor

        public int getItemNumber(){
            return itemNumber;
        }
        public void setItemNumber(int number){
            this.itemNumber = number;
        }
        public String getProductName(){
            return productName;
        }
        public void setProductName(String name){
            this.productName = name;
        }        
        public int getProductInStock(){
            return productInStock;
        }
        public void setProductInStock(int inStock){
            this.productInStock = inStock;
        }
        public double getProductPrice(){
            return productPrice;
        }
        public void setProductPrice(double price){
            this.productPrice = price;
        }
        public double getTotalProductValue(){
            return totalProductValue;
        }
        public void setTotalProductValue(double pValue){
            this.totalProductValue = pValue;
        }    


        // Calculation methods
        public Double calculateTotalProductValue ()  // begin calculateTotalProductValue method
        {
              // total product value = number of units in stock * price of each unit
              return getProductInStock() * getProductPrice();
        }  // end calculateTotalProductValue method

        public Double calculateTotalInventoryValue () // begin calculateTotalInventoryValue method
        {
            // total inventory value = total product value * productInStock
            return getTotalProductValue() * getProductInStock();
        }// end calculateTotalInventoryValue


    } // end  Pizza class


    public class Inventory1 {


        public static void main(String[] args) 
        {
            //instantiate Pizza array
            Pizza myPizza[] = new Pizza[5];

            myPizza[0] = new Pizza (1234, "Dough", 22, 1.59);
            myPizza[1] = new Pizza (1235, "Sauce", 35, 5.99);
            myPizza[2] = new Pizza (1236, "Cheese", 4, 15.99);
            myPizza[3] = new Pizza (1237, "Pepperoni", 3, 14.99);
            myPizza[4] = new Pizza (1238, "Ham", 2, 16.99);
            //output
            for (Pizza myPizza1 : myPizza) {
                System.out.println(" The product Number: " + myPizza1.getItemNumber());
                System.out.println(" The product Name: " + myPizza1.getProductName());
                System.out.println(" The number of product in Stock: " + myPizza1.getProductInStock());
                System.out.println(" The price per product is: $" + myPizza1.getProductPrice());
                System.out.println(" The total product value is: $" + myPizza1.calculateTotalProductValue());
            }    

             //output total inventory value
             double total = 0.0;
             for (int i = 0; i < 5; i++){ 
             total +=  myPizza[i].calculateTotalInventoryValue();            

               System.out.println("Total Value of Inventory is: $"+ total);


            }
        }  // end main method
    }//end inventory class

The output is

The product Number: 1234
The product Name: Dough
The number of product in Stock: 22
The price per product is: $1.59
The total product value is: $34.980000000000004
The product Number: 1235
The product Name: Sauce
The number of product in Stock: 35
The price per product is: $5.99
The total product value is: $209.65
The product Number: 1236
The product Name: Cheese
The number of product in Stock: 4
The price per product is: $15.99
The total product value is: $63.96
The product Number: 1237
The product Name: Pepperoni
The number of product in Stock: 3
The price per product is: $14.99
The total product value is: $44.97
The product Number: 1238
The product Name: Ham
The number of product in Stock: 2
The price per product is: $16.99
The total product value is: $33.98
Total Value of Inventory is: $0.0
Total Value of Inventory is: $0.0
Total Value of Inventory is: $0.0
Total Value of Inventory is: $0.0
Total Value of Inventory is: $0.0
BUILD SUCCESSFUL (total time: 0 seconds)

is easy, because when u call the constructor method new Pizza(1234, "Dough", 22, 1.59). if u see in your contructor method, are not saving the product for price an qty...

public Pizza( int number, String name, int inStock, double price)
        {
           itemNumber = number;
           productName = name;
           productInStock = inStock;
           productPrice = price;
           //here is missing totalProductValue???? 
           //then default value is = 0.0 for totalProductValue
           // maybe totalProductValue = productInStock * productPrice;
        }//End constructor

why this generate error???? or an value missing???
is because when u call the method in...

//output total inventory value
             double total = 0.0;
             for (int i = 0; i < 5; i++){ 
             total +=  myPizza[i].calculateTotalInventoryValue();            
               System.out.println("Total Value of Inventory is: $"+ total);

this... call the method ... calculateTotalInventoryValue ... an then, this method call directly to getTotalProductValue. look

public Double calculateTotalInventoryValue () // begin calculateTotalInventoryValue method
        {
            // total inventory value = total product value * productInStock
            //HERE U NOT SAVE THIS VALE AT THE TIME IN THE OBJECT IS CREATED.
            //then call the default value = 0.0 ... 
            return getTotalProductValue() * getProductInStock();
        }// end calculateTotalInventoryValue
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.