-2

The user must input the total purchase amount and how old they are, and then calculate the final payment.

If the total amount is $100 or over, there is a 20% discount off the total price. If the age is 65 or older, there is a 10% discount off the total price.

double discount1 = 0.10;
double discount2 = 0.20;
double totalPrice = 0.0;
double finalPrice = 0.0;

System.out.print("Enter total amount: ");
double purchase = input.nextDouble();
System.out.print("Enter age: ");
int age = input.nextInt();

if (purchase >= 100) {
  totalPrice = purchase * discount2;
  finalPrice = purchase - totalPrice;
  System.out.print("The final amount is $" + finalPrice);
}
else if (purchase < 100 && age < 65) {
  System.out.println("The final amount is $" + purchase);
}
else if (age >= 65) {
  totalPrice = purchase * discount1;
  finalPrice = purchase - totalPrice;
  System.out.print("The final amount is $" + finalPrice);
}

The user would input 200 as the total amount and 75 as the age. The output is supposed to be $140.00. However, I receive the output as $160.00.

  • 1
    Why you think it should be 140? 200-(200*0.2)=160 – Ori Marko Sep 22 '19 at 16:23
  • Because the problem says it outputs 140.00. –  Sep 22 '19 at 16:25
  • 1
    According to your written text, both discounts can be applied at the same time, but your code doesn't do that, so you get the wrong result. Change the code so it will calculate both discounts, if applicable. --- If you had **debugged** the code, it would have been obvious that the code for the second discount is never executed, so: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Sep 22 '19 at 16:27
  • Read your question again, you may need to edit it – Ori Marko Sep 22 '19 at 16:27

3 Answers3

1

The first if statement will be executed first. Because the price is above 100. So the other statements will not be executed. Try to change the if expressions because of thats the problem why it's not giving the result you may expect

1

My approach would be to add all of the discounts together and then multiply once at the end.

Than you can add other discounts if needed

double totalDiscount = 0.0;

if (purchase >= 100) {
  totalDiscount += discount2;
}
if (age >= 65) {
  totalDiscount += discount1;
}

totalPrice = purchase * (1.0 - totalDiscount);
System.out.print("The final amount is $" + totalPrice);
Mason
  • 4,326
  • 1
  • 12
  • 19
0

You nee to change below code,

because when ever price is more then 100 it will run first if block and wont enter into last block.

so change it in below manner :-

if (purchase >= 100 && age < 65) {
  totalPrice = purchase * discount2;
  finalPrice = purchase - totalPrice;
  System.out.print("The final amount is $" + finalPrice);
}
else if (purchase < 100 && age < 65) {
  System.out.println("The final amount is $" + purchase);
}
else if (purchase < 100 &&age >= 65) {

  totalPrice = purchase * discount1;
  finalPrice = purchase - totalPrice;
  System.out.print("The final amount is $" + finalPrice);
}
else if (age >= 65) {
  totalPrice1 = purchase * discount2;
  totalPrice = purchase * discount1;
  finalPrice = purchase - totalPrice - totalPrice1 ;
  System.out.print("The final amount is $" + finalPrice);
}
SSP
  • 2,650
  • 5
  • 31
  • 49