dimanche 6 septembre 2020

This if statement ladder can't figure out the discount

This is a program that I made and everything works except it doesn't display the discounted price and thus the final cost too. I am using if else ladder and scanner (This line was unnecessary but stacks overflow won't let me post my question if I don't write enough text) I would be really thankful if you can help me thanks. Here's the code :

/**A Shopkeeper has decided to give out discount and an assured gifts to his customer on the basis of total cost of the item purchased:
TOTAL COST   DISCOUNT GIFT
<= 2000       5%      WALL CLOCK
2001 – 5000   10%     BAG
5001 – 10000   15%    ELECTRIC IRON
>10000         20%    WRIST WATCH
Write a program to input the total cost. Compute discount. Display the total cost, discount obtained,
final amount to be paid and the gift received by the customer.*/
import java.util.*;
class P1
{
    public static void main(String a[])
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Input the total cost of the product...");
        double cost = in.nextDouble();
        if(cost<=0)
        {
            System.out.println("Invalid Price");
        }
        else if(cost<=2000)
        {
            System.out.println("Total cost of the product "+cost);
            System.out.println("Discount is 5%");
            double dis = 5/100*cost;
            double finalcost = dis+cost;
            System.out.println("Discounted price is "+dis);
            System.out.println("Final amount to be paid is "+finalcost);
            System.out.println("Gift recieved is a Wall Clock");
        }
        else if(cost>=2001 && cost<=5000)
        {
            System.out.println("Total cost of the product "+cost);
            System.out.println("Discount is 10%");
            double dis = 10/100*cost;
            double finalcost = dis+cost;
            System.out.println("Discounted price is "+dis);
            System.out.println("Final amount to be paid is "+finalcost);
            System.out.println("Gift recieved is a Bag");
        }
        else if(cost>=5001 && cost<=10000)
        {
            System.out.println("Total cost of the product "+cost);
            System.out.println("Discount is 15%");
            double dis = 15/100*cost;
            double finalcost = dis+cost;
            System.out.println("Discounted price is "+dis);
            System.out.println("Final amount to be paid is "+finalcost);
            System.out.println("Gift recieved is an Electric Iron");
        }
        else if(cost>10000)
        {
            System.out.println("Total cost of the product "+cost);
            System.out.println("Discount is 20%");
            double dis = 20/100*cost;
            double finalcost = dis+cost;
            System.out.println("Discounted price is "+dis);
            System.out.println("Final amount to be paid is "+finalcost);
            System.out.println("Gift recieved is a Wrist Watch");
        }
        else
        {
            System.out.println("Invalid Price");
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire