Variable off an Else If Condition statement

I have a couple of else/if statements in my code below. However I want to set a final variable that will set the final if statement depending on the users input. Lets say the user chooses to buy 6 "lathe". The discount applied will be lathecost * 0.10. I want to store that variable so I can use it in the future. However, I do not want to create a seperate variable if the user chooses 2 or 0. I want to the variable to know what the user chose and depending on what if/else statement itll store that. If the user chooses 2- itll store the final cost of lathecost * 0.05, if the user chooses 10 itll store the final cost of lathecost * 0.10, and so on. How can I achieve this?

  double numlathe;

    numlathe = input.nextFloat();
    final double priceoflathe = 20000;
    double lathecost = priceoflathe * numlathe;


    if (numlathe<0) {
        System.out.println("No discount applicable for 0 number of lathe purchase");
    }
    else if(numlathe<2) {
        System.out.println("Discount of lathe matchine purchase = 0 ");
    }

    else if(numlathe<5){
        System.out.println("There is discount that can be applied");

        System.out.println("Total cost so far is" + lathecost * 0.05 + " dollars");
    }

    else if(numlathe>=5){
        System.out.println("There is discount that can be applied.");

        System.out.println("Total cost so far with discount is "  +  lathecost * 0.10 + " dollars");
    }
Jon Skeet
people
quotationmark

You'll want to use the final result whether there's a discount or not, so you should have a variable for it whether there's a discount or not. If there isn't a discount, just set the value of the variable to the original.

In fact, I would change your design slightly to store the proportion which is discounted - so 0 for no discount, 0.05 for 5% etc. You can then separate the "compute discount" from "display discount" part:

private static final BigDecimal SMALL_DISCOUNT = new BigDecimal("0.05");
private static final BigDecimal LARGE_DISCOUNT = new BigDecimal("0.10");
private static BigDecimal getDiscountProportion(int quantity) {
    if (quantity < 0) {
        throw new IllegalArgumentException("Cannot purchase negative quantities");
    }
    return quantity < 2 ? BigDecimal.ZERO
        : quantity < 5 ? SMALL_DISCOUNT
        : LARGE_DISCOUNT;
}

Then:

int quantity = ...; // Wherever you get this from
BigDecimal discountProportion = getDiscountProportion(quantity);
BigDecimal originalPrice = new BigDecimal(quantity).multiply(new BigDecimal(20000));
BigDecimal discount = originalPrice.multiply(discountProportion);
// TODO: Rounding
if (discount.equals(BigDecimal.ZERO)) {
    System.out.println("No discount applied");
} else {
    System.out.println("Discount: " + discount);
}
BigDecimal finalCost = originalPrice.subtract(discount);

Note the use of BigDecimal here instead of double - double is generally unsuitable for currency values.

people

See more on this question at Stackoverflow