Hello,

I am having trouble with my inventory program. I made the first part last week and thought I did a good job. Come to find out I did not and the feedback I recieved from my instructor was not very helpful. Is there anyone that can help me fix my error for part 1 so I can continue to make part 2? My feedback was that I did not call the class method to calculate the value of the inventory. Here is my code:

`

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


import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;

 class Pizza {

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

        itemNumber = "";
        productName = "";
        productInStock = 0;
        productPrice = 0.0;
        totalInventoryValue = productInStock * productPrice;
    }
    // Consturctor
    public Pizza( String number, String name, int inStock, double price )
    {
       itemNumber = number;
       productName = name;
       productInStock = inStock;
       productPrice = price;
       totalInventoryValue = productInStock * productPrice;
    }//End constructor

    public String getItemNumber(){
        return itemNumber;
    }
    public void setItemNumber(String 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 getTotalInventoryValue(){
        return totalInventoryValue;
    }
    public void setTotalInventoryValue(double value){
        this.totalInventoryValue = productInStock * productPrice;
    }
}


public class Inventory1 {


    public static void main(String[] args) 
    {
        System.out.println( "Welcome to the inventory program" );

       // create a Scanner to obtain input from the command window
        Scanner input = new Scanner( System.in);

        String itemNumber;// Item number
        String productName;// Product name
        double productPrice;// Product price
        int productInStock;// productInStock
        double totalInventoryValue;// Total inventory value

        {   
        System.out.println( "Enter the item number: "); // Prompt user
        itemNumber = input.nextLine(); // read user input

        System.out.println( "Enter the product name: ");// Prompt user for product name
        productName = input.nextLine();

        System.out.print( "Enter the product price: ");// Prompt user for the product price
        productPrice = input.nextDouble();

        System.out.print( "Enter the total products in stock: ");// Prompt user for total products in stock
        productInStock = input.nextInt();


       totalInventoryValue = productInStock * productPrice; 

        } 


    Pizza pizza = new Pizza();

     NumberFormat nf = NumberFormat. getCurrencyInstance(Locale.US);

    //Use method from class Pizza to output inventory 
    {
    System.out.println(" Item number is " + itemNumber);
    System.out.println(" The product name is " + productName);
    System.out.println(" The number of products in stock are: " + productInStock);
    System.out.print(" The price of each product is: ");
    System.out.println(nf.format(productPrice));
    System.out.print(" The inventory value is: ");
    System.out.println(nf.format(totalInventoryValue));


    }// End Main class
    }
    }  

`I just recieved the feedback yesterday and the next part is due today, so please if someone can help me please do. Thanks.

Hello Slavi,

I did not get an error. However the instructor feedback says that the program does not call the class method to calculate the value of the inventory. I am confused by the feedback as I am new to Java and trying to learn with vague feedback. I did ask that he show me what I missed, but I have yet to heaar from him.

Hello, in line 59-60

public void setTotalInventoryValue(double value){
        this.totalInventoryValue = productInStock * productPrice;
    }

why exactly mean a variable (value?) it does not make nothing, because it is doesn't used.

an feedback maybe can be:

public void setTotalInventoryValue(){
        this.totalInventoryValue = productInStock * productPrice;
    }

then... in the line 104, this value are not needed. because this value are calculated inside of method -setTotalInventory().

totalInventoryValue = productInStock * productPrice; 

after... you make an Pizza (line 109) with the default constructor and all values are set with default values.
Pizza pizza = new Pizza();
Here, after you make a Pizza, u need put all values collected with the Scanner (itemNumber, productName, double productPrice, productInStock, productInStock)... whit the setMethods (Maybe...)

pizza.setItemNumber(itemNumber);
//... all the set operations...
//finally caluculate the totalInventoryValue. is an operation inside of method.
pizza.setTotalInventoryValues();

finally... use please methods of the pizza object after u put values in pizza (object). in line 113-121

//Use method from class Pizza to output inventory
{
    System.out.println(" Item number is " + pizza.getItemNumber());
    //more of ur elements here
   System.out.println(nf.format(pizza.getTotalInventoryValue()));
    }
// End Main class

is all. Have a nice day.

Edited 2 Years Ago by zolymo: aua

I think you should create a method that calculates the value of the inventory rather than calculating it in the main and also as an instance variable in your class pizza

double totalInventoryValue(){
return poductInstock * productPrice;
}

then call this method in your main to get the value

System.out.println(nf.format(totalInventoryValue())); 

Hello Zolymo,

I am new  to Java and I am confused as to what you mean.

Line 104 -Detele this row!!!
Line 109-124 change this

Pizza pizza = new Pizza();
     NumberFormat nf = NumberFormat. getCurrencyInstance(Locale.US);
    //Use method from class Pizza to output inventory 
    {
    System.out.println(" Item number is " + itemNumber);
    System.out.println(" The product name is " + productName);
    System.out.println(" The number of products in stock are: " + productInStock);
    System.out.print(" The price of each product is: ");
    System.out.println(nf.format(productPrice));
    System.out.print(" The inventory value is: ");
    System.out.println(nf.format(totalInventoryValue));
    }// End Main class

for this:

Pizza pizza = new Pizza(itemNumber, productName,productInStock,productPrice);

         NumberFormat nf = NumberFormat. getCurrencyInstance(Locale.US);
        //Use method from class Pizza to output inventory 
        {
        System.out.println(" Item number is " + pizza.getItemNumber());
        System.out.println(" The product name is " + pizza.getProductName);
        System.out.println(" The number of products in stock are: " + pizza.getProductInStock);
        System.out.print(" The price of each product is: ");
        System.out.println(nf.format(pizza.getProductPrice));
        System.out.print(" The inventory value is: ");
        System.out.println(nf.format(pizza.getTotalInventoryValue));
        }// End Main class

...
Here not do all your homework. I have taken the time to detail above in that you may be failing. The least you should do is try the proposed solution. My English is not very fluent, I speak Spanish. No more post for you. Bye

I am sorry I just did not understand, and no I don't want anyone to do my homework. I actually really like programming it is just hard to learn from nothing but readings. Thanks for all the help.

This article has been dead for over six months. Start a new discussion instead.