4
package entities;
public class Product {
public String name;
public double price;
public int quantity;
public double totalValueInStock() {
return price * quantity;
}
public void addProducts(int quantity) {
this.quantity += quantity;
}
public void removeProducts(int quantity) {
this.quantity -= quantity;
}
public String toString() {
return name
+ ", $ "
+ String.format("%.2f", price)
+ ", "
+ quantity
+ " units, Total: $ "
+ String.format("%.2f", totalValueInStock());
}
}`
I thought I understood the difference between these basic topics but I couldn’t explain the real difference between one and the other...
Look over there: this.quantity += quantity;
, was used the += assignment operator that says "this.Quantity GETS this.Quantity + Quantity".
But I can’t see the difference between it and the arithmetic operator +, both would do the same operation, right? I couldn’t understand this "RECEIVES".
What would be the real difference between one and the other in a clear way, without saying what example: a += 2
means "RECEIVES a + 2".
This I understood, if "a" is worth 10 the result of a += 2 is 12 however, a + 2 also results in 12. They understood me?
Why use one and not another?
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero