Reputation: 408
Code:
public void checkHitDiscount(WineCase wineCase1)
{
if(hits%10==0)
{
wineCase1.setPrice()=0.9*wineCase1.getPrice;
System.out.println("Congratulations! You qualify for 10% discount.");
} else
System.out.println("You do not qualify for discount.");
}
The error I get here is:
Method setPrice cannot be applied to given types. required double, found no argument.
I am trying to get a price
field in the WineCase
class modified. It is a double.
Upvotes: 0
Views: 392
Reputation: 1016
If price field is double type then you simply do like below.
public void checkHitDiscount(WineCase wineCase1)
{
if(hits%10==0)
{
wineCase1.setPrice(0.9*wineCase1.getPrice());
System.out.println("Congratulations! You qualify for 10% discount.");
} else
System.out.println("You do not qualify for discount.");
}
And in WineCase.java the setPrice must be like below.
pubilc void setPrice(double price) {
this.price = price;
}
You cannot assign any value to methods, but method can return values.
Upvotes: 1
Reputation: 62062
setPrice()
is a method. You seem to also have a method called getPrice()
, and these both probably corresponded to an instance variable called price
within your object.
If price
is private
, then you call getPrice()
as such:
wineCase1.getPrice();
This returns a double
(assuming price
is of type double).
And again, if price
is private
, then you'll need to set it as such:
wineCase1.setPrice(somePrice);
So in you're above example, if you want to set price
to 90% of what it's currently at, the proper syntax would look like this:
wineCase1.setPrice(0.9*wineCase1.getPrice());
Alternatively, you could potentially write a public
method for this class that looks like this:
public void discountBy(double disc) {
price *= 1.0 - disc;
}
// or like this:
public void discountTo(double disc) {
price *= disc;
}
// or both...
To use this method and apply a 10% discount to wineCase1
, you'd do this:
wineCase1.discountBy(0.1);
// or like this:
wineCase1.discountTo(0.9);
Then you'll still use:
wineCase1.getPrice();
To retrieve the private variable, price
, from the object.
And finally, this might potentially be the best solution, add these methods:
public double getPriceDiscountedBy(double disc) {
return price*(1.0-disc);
}
public double getPriceDiscountedTo(double disc) {
return price*disc;
}
These methods will allow you to retrieve the value of the discounted price without changing the original price of the item. These would be called in the same place you'd get a getPrice
, but take a discount argument to modify only the returned price. For example:
double discountedPriceOutsideOfObject = wineCase1.getPriceDiscountedTo(0.9);
//or like this:
double discountedPriceOutsideOfObject = wineCase1.getPriceDiscountedBy(0.1);
Upvotes: 2