Hello, I am having some trouble compiling my code for the inventory program part 2, if someone could take a look and help me out I would really appreciate it. Thanks.

I am required to implement an array to store, monitor, and retrieve data for camera inventory.

// Fig 2.4: InventoryProgram2.java
// Creating Inventory Program for Camera Inventory

import java.util.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;

class Inventory
{

    private static class UnitName {

        public UnitName() {
        }
    }
   private String ItemName;// class variable stores item name
   private int ItemNumber;// class variable stores item number
   private int NumberofUnits;// class variable stores number of units
   private double UnitPrice;// class variable stores unit price

   public Inventory(String name, int number, int Number, double price)
  // Constructor
    {
      ItemName = ItemName;
      ItemNumber = ItemNumber;
      NumberofUnits = NumberofUnits;
      UnitPrice = UnitPrice;
    }



   // method to set item name
 
public void setName(String name)
    {
    this.ItemName = ItemName;
    }



// method to get Camera name
public String getItemName()
    {
    return ItemName;
}



// method to set unit number
public void setItemNumber(int number)
       {
    this.ItemNumber = ItemNumber;
    }



// method to get item number
public int getItemNumber()
    {
return ItemNumber;

    }


// method to set number of units
public int setNumberofUnits()
    {
    this.NumberofUnits = NumberofUnits;
    }

// method to get number of units
public int getNumberofUnits()
{
    return NumberofUnits;
}

// method to set unit price
public double setUnitPrice()
{
    this.UnitPrice = UnitPrice;

}


// method to get unit price
public double getUnitPrice()
{
 return UnitPrice;

}

// method to calculate inventory value
public double calculateInventoryValue()
        {
return NumberofUnits * UnitPrice;
    }


// product sorting
public int compareTo (Object o)
{

   Camera s = (Camera)o;

   return UnitName.compareTo(s.getUnitName());
}

// return string unit information
    @Override
public String toString()

{

 System.out.println();return"Name;"+UnitName+ "\nNumber;"+UnitNumber+"\nNumberofUnits:$"+NumberofUnits+
 "\nPrice:"+UnitPrice+ "\nValue:$"+calculateInventoryValue();

    } // end main method

} // end class InventoryProgram2


class Camera
{

    //main method begin execution of java application
    public static void main(String argus[], String value)

    {

       //create product array for camera

       Camera[]products = new Camera[5];

      // camera inventory
      
      Camera a1 = new Camera("Sony", 10, 44, 200.0);
      Camera a2 = new Camera("Toshiba", 10, 43, 150.0);
      Camera a3 = new Camera("Phillips", 10, 45, 250.0);

      products[1] = a1;
      products[2] = a2;
      products[3] = a3;

      double UnitPrice = 0.0;

      for(int i=0;i<6;i++)
        {

      value = NumberofUnits + UnitPrice[i].calculateInventoryValue();

}

      // Display Inventory total value

      System.out.printf("Total value of entire Inventory is:$%.2f",value);

      System.out.println();

      Arrays.sort(products);

      for(Camera s:products)

{

      System.out.println(s);

      System.out.println();



}

      }// end main method

    public Camera() {
    }

    }// end class InventoryPart2

Recommended Answers

All 16 Replies

Member Avatar for coil

What's the error & line number that it happens in?

These are my errors

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Camera.java:18: cannot find symbol
symbol : constructor Camera(java.lang.String,int,int,double)
location: class Camera

Camera a1 = new Camera("Sony", 10, 44, 200.0);

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Camera.java:19: cannot find symbol
symbol : constructor Camera(java.lang.String,int,int,double)
location: class Camera

Camera a2 = new Camera("Toshiba", 10, 43, 150.0);

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Camera.java:20: cannot find symbol
symbol : constructor Camera(java.lang.String,int,int,double)
location: class Camera

Camera a3 = new Camera("Phillips", 10, 45, 250.0);

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Camera.java:31: cannot find symbol
symbol : variable total
location: class Camera
total = NumberofUnits + UnitPrice.calculateInventoryValue();

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Camera.java:31: cannot find symbol
symbol : variable NumberofUnits
location: class Camera
total = NumberofUnits + UnitPrice.calculateInventoryValue();

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Camera.java:31: array required, but double found
total = NumberofUnits + UnitPrice.calculateInventoryValue();

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Camera.java:37: cannot find symbol
symbol : variable total
location: class Camera
System.out.printf("Total value of entire Inventory is:$%.2f",total);

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Camera.java:41: cannot find symbol
symbol : variable Arrays
location: class Camera
Arrays.sort(products);

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Inventory.java:98: cannot find symbol
symbol : class UnitName
location: class Inventory
UnitName s = (Camera)o;

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Inventory.java:100: cannot find symbol
symbol : variable UnitName
location: class Inventory
return UnitName.compareTo(s.getUnitName());

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Inventory.java:108: cannot find symbol
symbol : variable UnitName
location: class Inventory
System.out.println();return"Name;"+UnitName+ "\nNumber;"+UnitNumber+"\nNumberofUnits:$"+NumberofUnits+

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Inventory.java:108: cannot find symbol
symbol : variable UnitNumber
location: class Inventory
System.out.println();return"Name;"+UnitName+ "\nNumber;"+UnitNumber+"\nNumberofUnits:$"+NumberofUnits+
12 errors

Where are the constructors for the Camera class that match how you are trying to use it?
new Camera("Sony", 10, 44, 200.0);

What don't you understand about this error: cannot find symbol
The compile can NOT find a definition for the variable listed in the error messages.
You need to provide a definition for the variable.

Hello NormR1, ok I need to implement constructors for the items information, do i do this in the Inventory class?

Hello, I made some changes, but I am a still confused on how implement the data to where the data matches in order to output the stored data.

Can you give me a brief example on how I would go about matching the data for both classes? Thanks.

// Fig 2.4: InventoryProgram2.java
// Creating Inventory Program for Camera Inventory

import java.util.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;

class Inventory
{

    private static class UnitName {

        public UnitName() {
        }
    }
   private String ItemName;// class variable stores item name
   private int ItemNumber;// class variable stores item number
   private int NumberofUnits;// class variable stores number of units
   private double UnitPrice;// class variable stores unit price

   public Inventory(String name, int number, int Number, double price)
  // Constructor
    {
       
      ItemName = ItemName;
      ItemNumber = ItemNumber;
      NumberofUnits = NumberofUnits;
      UnitPrice = UnitPrice;
    }



   // method to set item name
 
public void setName(String name, String Sony, String Toshiba, String Phillips)
    {
    this.ItemName = Sony;
    this.ItemName = Toshiba;
    this.ItemName = Phillips;        
    }



// method to get Camera name
public String getItemName()
    {
    return ItemName;
}



// method to set unit number
public void setItemNumber(int number)
       {
    this.ItemNumber = ItemNumber;
    }



// method to get item number
public int getItemNumber()
    {
return ItemNumber;

    }


// method to set number of units
public int setNumberofUnits()
    {
    this.NumberofUnits = NumberofUnits;
    }

// method to get number of units
public int getNumberofUnits()
{
    return NumberofUnits;
}

// method to set unit price
public double setUnitPrice()
{
    this.UnitPrice = UnitPrice;

}


// method to get unit price
public double getUnitPrice()
{
 return UnitPrice;

}

// method to calculate inventory value
public double calculateInventoryValue()
        {
return NumberofUnits * UnitPrice;
    }


// product sorting
public int compareTo (Object o)
{

   Camera s = (Camera)o;

   return UnitName.compareTo(s.getUnitName());
}

// return string unit information
    @Override
public String toString()

{

 System.out.println();return"Name;"+UnitName+ "\nNumber;"+UnitNumber+"\nNumberofUnits:$"+NumberofUnits+
 "\nPrice:"+UnitPrice+ "\nValue:$"+calculateInventoryValue();

    } // end main method

} // end class InventoryProgram2




import java.sql.Array;

public class Camera
{

    //main method begin execution of java application
    public static void main(String argus[], String value)

    {

       //create product array for camera

       Camera[]products = new Camera[5];

      // camera inventory
      
      Camera a1 = new Camera("Sony", 10, 44, 200.0);
      Camera a2 = new Camera("Toshiba", 10, 43, 150.0);
      Camera a3 = new Camera("Phillips", 10, 45, 250.0);

      products[1] = a1;
      products[2] = a2;
      products[3] = a3;

      double UnitPrice = 0.0;

      for(int i=0;i<6;i++)
        {

      value = NumberofUnits + UnitPrice[i].calculateInventoryValue();

}

      // Display Inventory total value

      System.out.printf("Total value of entire Inventory is:$%.2f",value);

      System.out.println();

      Array.sort(products);

      for(Camera s:products)

{

      System.out.println(s);

      System.out.println();



}

      }// end main method

    public Camera() {
    }

    }// end class InventoryPart2

confused on how implement the data to where the data matches in order to output the stored data.
... how I would go about matching the data for both classes?

Can you explain what you mean?

Have you fixed the errors that were pointed out in my previous post?

Get those fixed first before moving to other things.

Member Avatar for coil

First of all, take the main method out of your Camera class. It should only be in the main class.

Second of all, this is how you write constructors:

public Camera() - initialize with: new Camera()
public Camera(int i) - initialize with: new Camera(5)
public Camera(String s, int i) - initalize with: new Camera("Name", 5)

So write a constructor that matches how you're instantiating it (or vice versa: instantiate according to your constructor).

Hello coil, erase the main method out of my Camera class? Also I input the constructors into my inventory class?

Do that and then compile the program and correct the errors etc.

Hello Norm, like so:

Also, which class to I input these constructors

public Camera() - initialize with: new Camera()
public Camera(int i) - initialize with: new Camera(5)
public Camera(String s, int i) - initalize with: new Camera("Name", 5)

in the Inventory class or Camera class?

import java.sql.Array;

public class Camera {

    //main method begin execution of java application
    {

        //create product array for camera

        Camera[] products = new Camera[5];

        // camera inventory

        Camera a1 = new Camera("Sony", 10, 44, 200.0);
        Camera a2 = new Camera("Toshiba", 10, 43, 150.0);
        Camera a3 = new Camera("Phillips", 10, 45, 250.0);

        products[1] = a1;
        products[2] = a2;
        products[3] = a3;

        double UnitPrice = 0.0;

        for (int i = 0; i < 6; i++) {
        }

        // Display Inventory total value

        System.out.printf("Total value of entire Inventory is:$%.2f", value);

        System.out.println();

        Array.sort(products);

        for (Camera s : products) {

            System.out.println(s);

            System.out.println();



        }

    }// end main method

    public Camera() {
    }
}// end class InventoryPart2

The name of the constructor matches the name of the class.

Member Avatar for coil

Just so you know, those were examples. You should write your own constructor to match your needs.

Ok here we go I restarted my code over.

I got my Camera class to compile and my InventoryProgram class to compile. I am just experiencing 3 errors in my Inventory class.

Here are the 3 errors:

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\src\Inventory.java:12: cannot find symbol
symbol : class myCamera
location: class Inventory
myInventory.addToInventory(new myCamera("Sony", 0, 0, 0.0));
1 error

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\nbproject\build-impl.xml:528: The following error occurred while executing this line:

C:\Users\aks\Documents\NetBeansProjects\InventoryProgram2\nbproject\build-impl.xml:261: Compile failed; see the compiler error output for details.

// Fig 2.4: InventoryProgram.java
// Inventory program stores and monitors camera inventory

public class Camera {
    private int Number;
    private double Price;
    private String Name;
    private double TotalValue;

    public Camera(String ItemName, int ItemNumber, int NumberofUnits, double UnitPrice, double Value) {
        this.ItemName = ItemName;
        this.ItemNumber = ItemNumber;
        this.NumberofUnits = NumberofUnits;
        this.UnitPrice = UnitPrice;
        this.Value = Value;
    }

    private String ItemName;
    private int ItemNumber;
    private int NumberofUnits;
    private double UnitPrice;
    private double Value;
   

    {

        ItemName = Name;
        ItemNumber = Number;
        NumberofUnits = Number;
        UnitPrice = Price;
        TotalValue = Value;

        } //end four-argument constructor

    Camera(String string, int i, int i0, double d) {
        throw new UnsupportedOperationException("Not yet implemented");
    }


    {
        ItemName = Name;
        ItemNumber = Number;
        NumberofUnits = Number;
        UnitPrice = Price;
        TotalValue = Value;
    } //end four-argument constructor

       // set Item Name
    public void setItemName(String Name) {
        ItemName = Name;
    } //end method  setItemName

    //return Item Name

    public String getItemName() {
        return ItemName;
    } //end method getItemName

    //set Item Number
    public void setItemNumber(int Number) {
        ItemNumber = Number;
    } //end method setItemNumber

    //return Item Number
    public double getItemNumber(int Number) {
        return ItemNumber;
    } //end method get ItemNumber

    // set Number of Units
    public void setNumberofUnits(int Number) {
        NumberofUnits = Number;
    } //end method setNumberofUnits

    //return Number of Units
    public double getNumberofUnits(int Number) {
        return NumberofUnits;
    } //end method get Number of Units

    public void setUnitPrice(double Price) {
        UnitPrice = Price;
    } //end method setUnitPrice

    //return Unit Price
    public double getUnitPrice(double Price) {
        return UnitPrice;
    } //end method getUnitPrice

    //calculate <strong class="highlight">inventory</strong> value
    public double TotalValue(double Value) {
        return NumberofUnits * UnitPrice;

} // camera inventory

} // end class InventoryPart2
class InventoryProgram2
{

   public static void main( String [] args)
   {
       Camera camera = null;
 
 Camera myCamera = new Camera("Sony", 0, 0, 0.0);
 System.out.println(camera);
 
    }
 private Camera myCamera = new Camera("Toshiba", 0, 0, 0.0);

    {
 Camera myCamera = new Camera("Phillips", 0, 0, 0.0);




   } // end class main

    private InventoryProgram2() {
    }

    /**
     * @return the myCamera
     */
    public Camera getMyCamera() {
        return myCamera;
    }

    /**
     * @param myCamera the myCamera to set
     */
    public void setMyCamera(Camera myCamera) {
        this.myCamera = myCamera;
    }


}//end class InventoryProgram2
public class Inventory {

  private Camera myCamera[] = new Camera[0];

  // Add to inventory
 public void addToInventory(Camera camera) {
     }
 // Get Inventory value
 public double getTotalValue() {
  Inventory myInventory = new Inventory();
  myInventory.addToInventory(new myCamera("Sony", 0, 0, 0.0));
  // Print out the inventory total value
  System.out.println("Total value of inventory is: " +
  myInventory.getTotalValue());
  return myInventory.getTotalValue();
 }

   
    }
Member Avatar for coil

Error:

public class Camera {
    private int Number;
    private double Price;
    private String Name;
    private double TotalValue;
 
    public Camera(String ItemName, int ItemNumber, int NumberofUnits, double UnitPrice, double Value) {
        this.ItemName = ItemName;
        this.ItemNumber = ItemNumber;
        this.NumberofUnits = NumberofUnits;
        this.UnitPrice = UnitPrice;
        this.Value = Value;
    }

You are assigning stuff to non-existent objects.

For example: this.ItemName
What's ItemName? You don't declare it in your class. It should be Name=ItemName; .

Hello coil, thanks I made some changes, when I try to compile the program using the cd, it says "InventoryProgram.java:6 class Inventory is public, should be declared in a file name Inventory.java

public class Inventory"

1 error

Here is my code so far:

// Fig 2.4: InventoryProgram2  
// Creating InventoryProgram2 Program for Camera InventoryProgram2

class Inventory {

  // Add to inventory
 private static void Inventory(Camera camera) {
     }
 // Get Inventory value
 public double getTotalValue() {
  Inventory myInventory = new Inventory();
 
  // Print out the inventory total value
  System.out.println("Total value of inventory is: " +
  myInventory.getTotalValue());
  return myInventory.getTotalValue();
 } // end main method

  } //  end class Inventory

class InventoryProgram2
{

   public static void InventoryProgram2( String [] args)
   {
       Camera camera = null;
 
 Camera myCamera = new Camera("Sony", 0, 0, 0.0);
 System.out.println(camera);
 
    }
 private Camera myCamera = new Camera("Toshiba", 0, 0, 0.0);

    {
 Camera myCamera = new Camera("Phillips", 0, 0, 0.0);

   } // end class main

    private InventoryProgram2() {
    }

    /**
     * @return the myCamera
     */
    public Camera getMyCamera() {
        return myCamera;
    }

    /**
     * @param myCamera the myCamera to set
     */
    public void setMyCamera(Camera myCamera) {
        this.myCamera = myCamera;
    }


}//end class InventoryProgram2


public class Camera {

    private int Number;
    private double Price;
    private String Name;
    private double TotalValue;

    public Camera(String ItemName, int ItemNumber, int NumberofUnits, double UnitPrice, double Value) {
        this.ItemName = ItemName;
        this.ItemNumber = ItemNumber;
        this.NumberofUnits = NumberofUnits;
        this.UnitPrice = UnitPrice;
        this.Value = Value;
    }

    private String ItemName;
    private int ItemNumber;
    private int NumberofUnits;
    private double UnitPrice;
    private double Value;
   

    {

        ItemName = Name;
        ItemNumber = Number;
        NumberofUnits = Number;
        UnitPrice = Price;
        TotalValue = Value;

        } //end four-argument constructor

    Camera(String string, int i, int i0, double d) {
        throw new UnsupportedOperationException("Not yet implemented");
    }


    {
        ItemName = Name;
        ItemNumber = Number;
        NumberofUnits = Number;
        UnitPrice = Price;
        TotalValue = Value;
    } //end four-argument constructor

    // method to set Item Name
    // set Item Name
    public void setItemName(String Name) {
        ItemName = Name;
    } //end method  setItemName

    //return Item Name

    public String getItemName() {
        return ItemName;
    } //end method getItemName

    //set Item Number
    public void setItemNumber(int Number) {
        ItemNumber = Number;
    } //end method setItemNumber

    //return Item Number
    public double getItemNumber(int Number) {
        return ItemNumber;
    } //end method get ItemNumber

    // set Number of Units
    public void setNumberofUnits(int Number) {
        NumberofUnits = Number;
    } //end method setNumberofUnits

    //return Number of Units
    public double getNumberofUnits(int Number) {
        return NumberofUnits;
    } //end method get Number of Units

    public void setUnitPrice(double Price) {
        UnitPrice = Price;
    } //end method setUnitPrice

    //return Unit Price
    public double getUnitPrice(double Price) {
        return UnitPrice;
    } //end method getUnitPrice

    //calculate <strong class="highlight">inventory</strong> value
    public double TotalValue(double Value) {
        return NumberofUnits * UnitPrice;

} // camera inventory

} // end class Camera


class InventoryProgram2

{

   public static void InventoryProgram2( String [] args)
   {
       Camera camera = null;
 
 Camera myCamera = new Camera("Sony", 0, 0, 0.0);
 System.out.println(camera);
 
    }
 private Camera myCamera = new Camera("Toshiba", 0, 0, 0.0);

    {
 Camera myCamera = new Camera("Phillips", 0, 0, 0.0);

   } // end class main

    private InventoryProgram2() {
    }

    /**
     * @return the myCamera
     */
    public Camera getMyCamera() {
        return myCamera;
    }

    /**
     * @param myCamera the myCamera to set
     */
    public void setMyCamera(Camera myCamera) {
        this.myCamera = myCamera;
    }


}//end class InventoryProgram2
Member Avatar for coil

You need to get rid of the public static void main in the Inventory class.

I think, before you do anything else, you really need to clean up your code. For example, I'm not sure why your Camera class is compiling at all. You should get rid of your previous constructor in Camera (the one you had that triggered errors) because it's wrong. You're missing the "public" keyword, and your indenting is somewhat random.

Doing this will reduce repetitive errors down the road, help you code better, and make your code more readable.

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.