Thursday 23 April 2015

A PROGRAM TO GENERATE RECEIPT AS IN A GENERAL STORE AFTER SCANNING THE BARCODE !!!

/* Wonder how are receipts generated in a General Store after Scanning the barcode ??
 * So here is what to do :
 * 1. Initialise 2 arrays of product name and price.
 * Remember the codes entered are respective index numbers of the elements in the array eg. 0 for Ear phones , 1 for Headphone , etc
 * Using Switch Case to check for matching the codes
 * We have to calculate discount on products with codes {2,4,5,7,8,9}
 * I have first calculated the final discount and subtracted it at the last, while printing
 * We also have to find out the discount on each product liable for discount
 * The Discount is liable only the product is bought in multiple quantities
 * So here it is :
   */   

My Input
 class Receipt_Generator
    {
        void main(int code[] , int quantity[

],String date)//Taking parameterized input
        {
            double cost = 0.0 , dis=0.0, final_discount=0.0;
            int c =0;
            String product_name[] = {"Ear phones" , "Headphone" , "Tablet    " , "Memory Card" , "Hard disk" , " Phone " , "Wrist Watch " ,"Laptop    ","Television","Desktop"};//An array of each Array
            int price[] = {20 , 100 , 800 , 250 , 500, 700 , 200 ,3000,10000,2500};//Price of each Product
            //We have discounts on the following products = {2,4,5,7,8,9};
            for(int i = 0 ;i<code.length;i++)
            {
            switch (code[i])//The codes are respective index numbers of the elements in the array eg. 0 for Ear phones , 1 for Headphone , etc
            {
             
                case 0:
                cost += quantity[i]*20.0;
                break ;

                case 1:
                cost += quantity[i]*100.0;
                break ;
             
                case 2:
                c = (quantity[i]*800);
                 if((c>=1600)&&(c<2400))
                dis = (10/100.0)*c ;
                else if(c>=2400&&c<=32000)
                dis = (15/100.0)*c;
                else if(c>3200)
                dis = (20/100.0)*c;
                final_discount = dis+final_discount ;
                cost+=c;
                break ;
                case 3:
                cost += quantity[i]*250.0;
                break;
                case 4:
                c = (quantity[i]*500);
              if((c>=1000)&&(c<1500))
                dis = (10/100.0)*c ;
                else if(c>=2000&&c<=25000)
                dis = (15/100.0)*c;
                else if(c>3000)
                dis = (20/100.0)*c;
                final_discount = dis+final_discount ;
                cost+=c;
             
                break ;
                case 5:
                c = (quantity[i]*500);
                if((c>=1000)&&(c<1500))
                dis = (10/100.0)*c ;
                else if(c>=2000&&c<=2500)
                dis = (15/100.0)*c;
                else if(c>3000)
                dis = (20/100.0)*c;
             
                final_discount=dis+final_discount;
                cost+=c;
                break ;
                case 6:
                cost += quantity[i]*700;
                break;
                case 7:
                 c = (quantity[i]*200);
                if((c>=400)&&(c<600))
                dis = (10/100.0)*c ;
                else if(c>=600&&c<=800)
                dis = (15/100.0)*c;
                else if(c>1000)
                dis = (20/100.0)*c;
                final_discount = dis+final_discount;
                cost+=c;
             
                break ;
                case 8:
                c = (quantity[i]*10000);
                if((c>=20000)&&(c<30000))
                dis = (10/100.0)*c ;
                else if(c>=30000&&c<=40000)
                dis = (15/100.0)*c;
                else if(c>50000)
                dis = (20/100.0)*c;
                final_discount=dis+final_discount;
                cost+=c;
             
                break ;
                case 9:
             
                c = (quantity[i]*2500);
                if((c>=5000)&&(c<7500))
                dis = (10/100.0)*c ;
                else if(c>=7500&&c<=10000)
                dis = (15/100.0)*c;
                else if(c>12500)
                dis = (20/100.0)*c;
                final_discount=dis+final_discount;
             
                cost+=c;
             
                break ;
                default :
                System.out.println("WRONG CHOICE");
                System.out.println("RE-ENTER THE CODE");
            }
        }
       System.out.println("Date: "+date);
       System.out.println("###########################################YOUR BILL###############################################");
        System.out.println("Product ID \t Product Name \t Price \t Quantity \t Total Cost");
        for(int j = 0 ; j<quantity.length;j++)//for printing all the items
        {
             
        System.out.println(code[j]+"         \t "+product_name[code[j]]+"\t"+price[code[j]]+"   \t"+quantity[j]+"     \t \t"+(price[code[j]]*quantity[j]));
        }
        System.out.println("Total Cost: "+cost+"$");
        System.out.println("Total discount: "+final_discount+"$");
        System.out.println("Grand Total :"+(cost-final_discount)+"$");
        System.out.println("***********************THANK YOUR FOR SHOPPING***********************");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$SHOP AGAIN$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.println(" \n");
    System.out.println("________________________________");
    System.out.println("           SIGNATURE");
    }
}
My Output
PLEASE GIVE SUGGESTIONS AND ASK  QUESTIONS DOWN IN THE COMMENTS SECTION

2 comments:

  1. The code is nice but its very lengthy . Can't u make it shorter ?

    ReplyDelete
    Replies
    1. Oh yes I can shorten the code by using double dimensional arrays . I will soon publish a prototype. Thank you for suggestion :)

      Delete