Search This Blog

Sunday, October 14, 2012

Use of arrays, functions, constructors in JAVA & BLUE J

import java.io.*;
class usecanteen
{
    public static void main(String args[])throws IOException
    {
       canteen ob=new canteen();  //object creation
       
        int itemnumber=2;
        int quantity=3;
        canteen ob1=new canteen(itemnumber, quantity);  //calling parameterised constructor
       
        ob.new_order();
        ob.display_bill();
        int amount=ob.return_bill();
        System.out.println("Kindly pay a total of Rs. "+amount);
    }
}






class canteen
{

        String items[]={"TV", "DVD Player", "Tape recorder", "LG mobile"};  //items array
        int price[]={20000, 5000, 3000, 11000}; //rate array
        int q; //quantity
        int bill;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
        canteen()  //default constructor
        {
            q=0;
            bill=0;
        }
       
        canteen(int itemnum, int quant)  //parameterised constructor
        {
            bill=price[itemnum-1]*quant;
        }
       
        void new_order() throws IOException   //function to place a new order
        {
            System.out.println("************************MENU**********************");
            System.out.println("Enter 1 to order TV @20 000 Rs. ");
            System.out.println("Enter 2 to order DVD player @5000 Rs.");
            System.out.println("Enter 3 to order Tape recorder @3000 Rs.");
            System.out.println("Enter 4 to order LG mobile @11000 Rs.");
            System.out.println("****************************************************");
            int ch,i;
            String ans;
            int count=0;
           
            for(i=1;i<=4;i++)
            {
                count++;
                System.out.println("Please enter your choice of item(1-4)");
                ch=Integer.parseInt(br.readLine());
               
                if(ch>=1 && ch<=4)
                {
                    System.out.println("Please enter the number of " + items[ch-1] + " you want" );                   
                    q=Integer.parseInt(br.readLine());
                }
                else
                {
                    break;
                }
     
                bill=bill + price[ch-1]*q;
                if(count==4)
                {
                   break;
                }
                System.out.println("Do you want to place another order YES or NO?Please note you cannot order more than 4 items");
                ans=br.readLine();
                if(ans.equalsIgnoreCase("NO"))
                {
                    break;
                }
               
        
                           
            }
        }
        void display_bill()
        {
            System.out.println("ITEMISED BILL is of Rs."+bill);
        }
        int return_bill()
        {
            return bill;
        }
         
    }

1 comment: