Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Wednesday 13 July 2011

Program to demonstrate the Switch case and If statement


This a Program to demonstrate the Switch case and If statement by calculating the discount amount of a cloth products

Program:
 
import java.io.*;
import java.util.*;
class Cloth1
{
void purchase()
                {
                Scanner s=new  Scanner(System.in);
                System.out.println("What do  you want to Purchase? \n 1)Mill Cloth \n 2)Handloom Cloth \n 3)Exit");

                int x=s.nextInt();
                switch(x)
                                {
                                                case 1:
                                                               
                                                                                System.out.println("Enter the amount of mill cloth: ");
                                                                                float d=s.nextFloat();
                                                                                discountmill(d);
                                                                                break;
                                                               
                                                case 2:
                                                               
                                                                                System.out.println("Enter the amount of handloom cloth: ");
                                                                                float d1=s.nextFloat();
                                                                                discounthl(d1);
                                                                                break;
                                                case 3:
                                                                                System.out.println("Thank you!!!");
                                                                                break;
                                    }
                }
                void discountmill(float a)
                {
                                double dis;
                                double res;
                                if(a<=100)
                                {
                                                System.out.println("No discount!!!");
                                }
                                else if(a>100 && a<=200)
                                {
                                                System.out.println("5% Discount for this item:");
                                                dis=0.05*a;
                                                res=a-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else if(a>200 && a<=300)
                                {
                                                System.out.println("7.5% Discount for this item:");
                                                dis=0.075*a;
                                                res=a-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else
                                {
                                                System.out.println("10% Discount for this item:");
                                                dis=0.1*a;
                                                res=a-dis;
                                                System.out.println("Net Amount with Discount: "+res);                               
                                }
                                purchase();
                }
                void discounthl(float b)
                {
                               
                                double dis;
                                double res;
                                if(b<=100)
                                {
                                                System.out.println("5% Discount for this item:");
                                                dis=0.05*b;
                                                res=b-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else if(b>100 && b<=200)
                                {
                                                System.out.println("7.5% Discount for this item:");
                                                dis=0.075*b;
                                                res=b-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else if(b>200 && b<=300)
                                {
                                                System.out.println("10% Discount for this item:");
                                                dis=0.1*b;
                                                res=b-dis;
                                                System.out.println("Net Amount with Discount: "+res);
                                }
                                else
                                {
                                                System.out.println("15% Discount for this item:");
                                                dis=0.15*b;
                                                res=b-dis;
                                                System.out.println("Net Amount with Discount: "+res);                               
                                }
                                purchase();
                }
                               
}
class Cloth
{
                public static void main(String args[])
                {
                                Cloth1 c1=new Cloth1();
                                c1.purchase();
                }
}             

OUTPUT
D:\Java>javac Cloth.java

D:\Java>java Cloth
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
1
Enter the amount of mill cloth:
50
No discount!!!
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
1
Enter the amount of mill cloth:
140
5% Discount for this item:
Net Amount with Discount: 133.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
1
Enter the amount of mill cloth:
240
7.5% Discount for this item:
Net Amount with Discount: 222.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
1
Enter the amount of mill cloth:
340
10% Discount for this item:
Net Amount with Discount: 306.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
2
Enter the amount of handloom cloth:
40
5% Discount for this item:
Net Amount with Discount: 38.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
2
Enter the amount of handloom cloth:
140
7.5% Discount for this item:
Net Amount with Discount: 129.5
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
2
Enter the amount of handloom cloth:
240
10% Discount for this item:
Net Amount with Discount: 216.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
2
Enter the amount of handloom cloth:
340
15% Discount for this item:
Net Amount with Discount: 289.0
What do you want to Purchase?
 1)Mill Cloth
 2)Handloom Cloth
 3)Exit
3
Thank you!!!

No comments:

Post a Comment

Followers