hello all,
I am trying to create a class for an inventory program that will store certain values like name, item number number of units and price. I have written a code but it is apparently not correct because I get several error messages. can you please look at what I have and help me with this problem thank you.

public class Cars
{
   private String productName;
   private String itemNumber;
   private String numberOfUnits;
   private double pricePerUnit;

   public Cars( String product, String item, String number, 
      double price )
   {

      productName = product;
      itemNumber = item;
      numberOfUnits = number;
      setPrice( price );
   }

   public void setProductName( String product )
   {
      productName = product;
   }

   public String getProductName()
   {
      return productName;
   }

   public void setItemNumber( String item )
   {
      itemNumber = item;
   }

   public String getItemNumber()
   {
      return itemNumber;
   }

   public void setNumberOfUnits( String number )
   {
      numberOfUnits = number;
   }

   public String getNumberOfUnits()
   {
      return numberOfUnits;
   }
  
   public void setPrice( double price )
   {
      price = ( price < 0.0 ) ? 0.0 : price;
   }

   public double getPrice()
   {
      return price;
   }

   public String toString()
   {
      return String.format( "%s:  %s\n%s: %s\n%s: %s\n%s: %.2f",
         "Cars", product,
         "item number", item,
         "number of units", number,
         "price per unit", price );

   }

} // end class Cars

Code tags. Please use them.

What errors do you get? I see some obvious ones regarding the variable names.

public void setPrice( double price )
{
price = ( price < 0.0 ) ? 0.0 : price;
}

public double getPrice()
{
return price;
}

public String toString()
{
return String.format( "%s: %s\n%s: %s\n%s: %s\n%s: %.2f",
"Cars", product,
"item number", item,
"number of units", number,
"price per unit", price );

}

There is no class variable called price. There's one called pricePerUnit. Fix that and I imagine most of your errors will go away. You have the same problem with some of the other class variables. Make sure you are using the right variable names.

Are you sure you want these to be Strings? The description makes them sound like they should be integers.

private String itemNumber;
private String numberOfUnits;
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.