## java_starter

I am having trouble calculating the discount rate in java, I have the formula and equation but cannot figure out, what the problem. and my program would compile and work but would display the wrong amt of discount.

here the a part of the program..

``````public void setDiscountRate(double discountRateBeforeTaxes)
{
totalInvoiceAmount -= discountRate += discountRateBeforeTaxes * 0.20;    ;
totalPurchase++;
}

public void setInvoiceAmount(double invoiceAmountBeforeTaxes)
{
totalInvoiceAmount += invoiceAmountBeforeTaxes * 1.0775;           /
totalPurchase++;
}``````

## Ezzaral 2,714

Well, the math that it is performing is very straightforward. Do you mean to increase the "discountRate" by 20% of "discountRateBeforeTaxes" on every call to setDiscountRate()?

You would probably be better served by breaking those calculations into multiple steps if you aren't getting the results you expect. Then you can place System.out.println() calls to verify the calculations after each step.

## java_starter

This class file is calculating the amount of discount a customer receives on a purchase, the discount is fixed at 20% and while everything works fine, but it would not calculate the discount. after running the java class file, the discount amount would come to 0.00 instead of 38.00 which is discount amount should be for the nvoice .

can anyone tell me what wrong with the formula or is it right

thanks

## jwenting 1,649

If the discount is fixed at 20% you don't have to calculate the discount rate, it's 20%...

The amount is simply 0.2 times the amount of the invoice. Whether that should be taken before or after taxes is irrelevant to the invoice after taxes. It may however be relevant for tax purposes, I don't know your taxlaw so I can't say.

And oh, NEVER chain assignments like you did. It easily leads to confusion.
The ++ operator also seems unfamiliar to you. Do you really want to increase the totalPurchase amount only after calculating the discount over that purchase?
There's more wrong there, but that should be a start.

## java_starter

According to my prof, the discount rate is 20% for this class and when the program runs it should display the amount of discount and total invoice after tax including discount in the total invoice amount. I think i may got the equation wrong for the discount rate. what is the right way of calculating the discount.. any help would be good,.

thanks

## Ezzaral 2,714

If the discount rate is fixed, why do you even need a setDiscountRate method? It seems like all you need is a getDiscount() method that returns `.20 * invoiceAmountBeforeTaxes` . Then total invoice is `(invoiceAmountBeforeTaxes - getDiscount() ) * 1.0775` assuming that .0775 is your tax rate.

## jwenting 1,649

he says he wants to calculate the discount RATE, then goes on and produces code which seems to be a failed attempt to calculate the actual discount based on that rate...

And when confronted with that he gets agressive?