Basically I want my program to change the current price of an object with a new one, but I'm having trouble doing so. Here's my code:

//-----------------------------------------------------------------
   //  Updates the product's price.
   //-----------------------------------------------------------------
	
	public void setPrice(double newPrice)
	{
      this.newPrice = newPrice;
	}
//-----------------------------------------------------------------
   //  Changes the price of the product from the collection.
   //-----------------------------------------------------------------
		
	public void changePrice(int prodID, double newPrice)
	{
      int index = findProduct(prodID);
      if (index > -1)
         collection[index].setPrice(newPrice);
      else
         System.out.println("The product having the ID number " + prodID 
                            + " was not found.\n");	
	}
System.out.print("\nEnter the ID number of product to modify: ");
                        idNum = userInput.nextInt();
                        System.out.print("\nWhat is the new price? ");
                        price = userInput.nextDouble();
                        userInput.nextLine();
                        data.changePrice(idNum, price);

The error I get from my program is:

Product.java:70: cannot find symbol
symbol : variable newPrice
location: class Product
this.newPrice = newPrice;

Am I coding this incorrectly or is there an alternative for what I hope to acheive?

Recommended Answers

All 2 Replies

well you should have a product class that has a price field and have a set and get method. This would allow you to easily change a product's price from any other class

It'd be helpful if you showed us the code segment that gave you the compiler error.

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.