user11007886
user11007886

Reputation: 89

How to take discount calculation

Very new to programming. I would want the user to receive a 10% discount if he/she is over 60 (age>60) and 5% discount if he/she is between over 55 and equal to 60. (60<=age>55). I know that my code is completely wrong, but I would like to fix this step by step if possible.

import java.util.*;
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
int price, age;
double tax, payAmount;
double discountRate_56_to_60 = 0.05;
double discountRate_60_above = 0.1;
payAmount = price * tax;

System.out.print("Price?");
price = input.nextInt();

System.out.print("Tax(%)?");
tax = input.nextDouble();

System.out.print("Age?");
age = input.nextInt();

System.out.println("You pay: $");
payAmount = input.nextDouble();

if (age > 55) {
    payAmount;
}
else if (age >= 60) {
    payAmount;
}

}
}

Upvotes: 4

Views: 6596

Answers (7)

abdulwasey20
abdulwasey20

Reputation: 837

There are many errors in your code

  1. You cannot calculate the payAmount before you have gotten the input values from the user, so you move that statement after all the input statements.

  2. You are using the wrong formula to calculate the payAmount, the correct code would be:

    payAmount = price +(price* tax/100);

because the tax inputted is an integer and you want to use percentage tax, you divide by 100 and you also add the tax into the price.

  1. Another error is in the if else statement(s)

    payAmount=payAmount-(payAmount*discountRate_56_to_60); //or discountRate_60_above

you have to deduct the discount rate from the payAmount after the tax has been added in it.

The correct code would be:

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int price, age;
        double tax, payAmount;
        double discountRate_56_to_60 = 0.05;
        double discountRate_60_above = 0.1;


        System.out.print("Price?($)\n");
        price = input.nextInt();

        System.out.print("Tax(%)?\n");
        tax = input.nextDouble();

        System.out.print("Age?\n");
        age = input.nextInt();

        payAmount = price +(price* tax/100);        //adding the tax to the price

        if (age >= 55 && age<60) {
            payAmount=payAmount-(payAmount*discountRate_56_to_60);      //subtracting the appropriate discount according to age
        }
        else if (age >= 60) {
            payAmount=payAmount-(payAmount*discountRate_60_above);
        }
        System.out.println("You pay: $" +payAmount);

    }

Upvotes: 1

Malte K&#246;lle
Malte K&#246;lle

Reputation: 192

There are multiple problems, the first one is that the second if statement else if (age >= 60) is never reached, because if the age is over 60 the person is also over 55. To solve that you should change the statements like this:

if(age >+ 60){
   // do something
} else if(age >= 55){
   // do something different
}

Then you try to initialize payAmount before you have initialized price and tax. You should to the calculation after the User entered his age and the price etc.

// User enters all the stuff
payAmount = price * tax;

And then you can apply the right discount rate in the if statements.

And if I would be you I would declare the Integer price as a Double, because it could be that the price is like 16.50$.

Also you could check if the entered number is an integer or a double to avoid exceptions.

Upvotes: 1

Trebond
Trebond

Reputation: 57

With you current boolean statements, you should tweak your else if (age >=60) to be (age > 60) because, as you stated, you want a 5% discount over 55 and up to (and including) 60. Also, you should declare if (age > 60) first, or else your check for (age > 50) will not execute.

Now that you have declared a discount rate for 56 to 60 and 60 and above, you can return

payAmount * specified discount rate, 

For ages 56 to 60, your code would look like

payAmount * double discountRate_56_to_60.

Just as a general style comment however, you do not need to declare the discount rate outside of the if statement, unless you are using it again outside your statements. In fact, you could simply state

if (age > 55) {
    return payAmount * .05;
}

Which would lead to cleaner looking code. Hope this helps!

Upvotes: 1

Sweeper
Sweeper

Reputation: 273988

You made a few mistakes:

  • The line payAmount = price * tax; is executed too early. How can you calculate what the pay amount is before getting the price and tax from the user?

  • The line payAmount = input.nextDouble(); should not be there. The problem is supposed to output the payAmount, not ask for it as an input.

  • payAmount is not a statement.

  • Your if statements seem to be wrong. If an age is not greater than 55, it can never be greater than or equal to 60.

Here is the fixed code, corrections are written in comments:

Scanner input = new Scanner(System.in);
int price, age;
double tax, payAmount;
double discountRate_56_to_60 = 0.05;
double discountRate_60_above = 0.1;

System.out.print("Price?");
price = input.nextInt();

System.out.print("Tax(%)?");
tax = input.nextDouble();

payAmount = price * tax; // I moved the line here!

System.out.print("Age?");
age = input.nextInt();

// removed the line asking for pay amount

if (age > 60) { // first we check if age is greater than 60. If it is not, then we check if it is greater than 55.
                // with a little analysis you will see that this is equivalent to the stated conditions
    payAmount -= payAmount * discountRate_60_above; // calculate the payAmount by subtracting the pay amount times discount rate
}
else if (age > 55) {
    payAmount -= payAmount * discountRate_56_to_60;
}

System.out.println("You pay: $" + payAmount); // finally output the payAmount

Upvotes: 4

Abhishek Soni
Abhishek Soni

Reputation: 593

if(age>60) {

   payAmount = payAmount - payAmount*0.10;

   System.out.println("Paid Amount is "+ payAmount);

} else if(age>55 && age<=60) {
   payAmount = payAmount - payAmount*0.05;
   System.out.println("Paid Amount is "+ payAmount);
} else {
   System.out.println("Paid Amount is "+ payAmount);
}

Upvotes: 0

Chetan Joshi
Chetan Joshi

Reputation: 5711

Try below code to calculate discount:

  if (age > 55 && age <60) {
            payAmount = payAmount - (payAmount*5)/100;
        }
        else if (age >= 60) {
    payAmount = payAmount - (payAmount*10)/100;   
     }

Upvotes: 0

ManojP
ManojP

Reputation: 6248

Try the below code

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int price, age;
        double payAmount = 0;

        System.out.print("Price?");
        price = input.nextInt();

        System.out.print("Age?");
        age = input.nextInt();

        if (age > 55 && age <= 60) {
            payAmount = price - price * 5 / 100;
        } else if (age > 60) {
            payAmount = price - price * 10 / 100;
        }

        System.out.println("You pay: $ " + payAmount);

        input.close();
    }

Upvotes: 2

Related Questions