Monday, November 10, 2014

Finding out top nth salary

My SQL version:-n=3
----------------------
    SELECT TOP 1 salary  
    FROM ( 
    SELECT TOP 3 salary 
    FROM employee_table 
    ORDER BY salary DESC ) AS emp 
    ORDER BY salary ASC; 
_____________________________


Oracle version:-n=5
--------------------------
1.
 select * from (SELECT *
FROM (select * from emp ORDER BY sal DESC)
WHERE rownum <= 5
ORDER BY rownum) order by sal asc;

2.
select min(sal) from (SELECT *
FROM (select * from emp ORDER BY sal DESC)
WHERE rownum <= 5
ORDER BY rownum) 
order by sal asc ;

Sunday, November 9, 2014

ATM logic in JAVA

Version 1:-
---------------------------------------------------------------------------------------------------------------------


import java.util.Scanner;
public class MyATM {
int n1000,n500,n100,totalAmmount,userInputAmmount;

public static void main(String... h)
{
       MyATM myAtm=new MyATM();
       myAtm.fillMoneyInATM();
       myAtm.Calculate();
}

       void fillMoneyInATM()
       {
              Scanner sc = new Scanner(System.in);
              System.out.println("Enter 1000 Rs note");
              n1000 = sc.nextInt();
           System.out.println("Enter 500 Rs note");
           n500=sc.nextInt();
           System.out.println("Enter 100 Rs note");
           n100=sc.nextInt();
           System.out.println("Total 1000 Rs Note "+n1000);
           System.out.println("Total 500 Rs Note "+n500);
           System.out.println("Total 100 Rs Note "+n100);
           System.out.println(n1000+n500+n100);
           //totalAmmount=(n1000*1000)+(n500*500)+(n100*100);
           //System.out.println("Total Ammount "+totalAmmount);
       }
       void Calculate()
       {
              totalAmmount=(n1000*1000)+(n500*500)+(n100*100);
               System.out.println("Total Ammount "+totalAmmount);
              if(totalAmmount<userInputAmmount)
              {
                     System.out.println("ATM does not have valid ammount");
              }
              else if((userInputAmmount%100)!=0)
              {
                     System.out.println("Enter Valid Ammount");
              }
              else
              {
                    
                    
              }
             
             
       }
}

 Version 2:-
--------------------------------------------------------------------------------------------

import java.util.Scanner;
public class MyATM {
int n1000,n500,n100,totalAmmount,userInputAmmount,gn1000,gn500,gn100;
Scanner sc;

public static void main(String... h)
{
       MyATM myAtm=new MyATM();
       myAtm.fillMoneyInATM();
       myAtm.Calculate();
}

       void fillMoneyInATM()
       {
               sc = new Scanner(System.in);
              System.out.println("Enter 1000 Rs note");
              n1000 = sc.nextInt();
           System.out.println("Enter 500 Rs note");
           n500=sc.nextInt();
           System.out.println("Enter 100 Rs note");
           n100=sc.nextInt();
           System.out.println("Total 1000 Rs Note "+n1000);
           System.out.println("Total 500 Rs Note "+n500);
           System.out.println("Total 100 Rs Note "+n100);
           System.out.println(n1000+n500+n100);
           //totalAmmount=(n1000*1000)+(n500*500)+(n100*100);
           //System.out.println("Total Ammount "+totalAmmount);
       }
       void Calculate()
       {
               sc = new Scanner(System.in);
              totalAmmount=(n1000*1000)+(n500*500)+(n100*100);
              // System.out.println("Total Ammount in ATM "+totalAmmount);
             
               System.out.println("Please Enter Ammout");
                     userInputAmmount = sc.nextInt();
              if((totalAmmount<userInputAmmount))
              {
                     System.out.println("\nEither ATM does not have Entered ammount \n OR \n Empty ATM");
              }
              else if((userInputAmmount%100)!=0)
              {
                     System.out.println("Enter Valid Ammount In multiple of 100");
              }
              else if(userInputAmmount==0)
              {
                     System.out.println("No Ammount is filled");                  
              }
              else
              {
                     //Main logic
                    
                     //Given 1000 Rs
                     if(n1000!=0 && userInputAmmount!=0)
                     {      gn1000=0;
                     System.out.println("Entered Ammount ="+userInputAmmount);
                           if((userInputAmmount/1000)!=0 && n1000>=gn1000)
                           {
                                  gn1000=userInputAmmount/1000;
                                  userInputAmmount=userInputAmmount%1000;
                                  //System.out.println("Given notes of 1000 ="+gn1000);
                                  n1000=n1000-gn1000;
                           }
                           //System.out.println("Given notes of 1000="+gn1000);
                     }
                    
                     //Given 500 Rs
                     if(n500!=0 && userInputAmmount!=0)
                     {      gn500=0;
                     System.out.println("Entered Ammount ="+userInputAmmount);
                           if((userInputAmmount/500)!=0 && n500>=gn500)
                           {
                                  gn500=userInputAmmount/500;
                                  userInputAmmount=userInputAmmount%500;
                                  //System.out.println("Given notes of 500 ="+gn500);
                                  n500=n500-gn500;
                           }
                           //System.out.println("Given notes of 500="+gn500);
                     }
                    
                     //Given 100 Rs
                     if(n100!=0 && userInputAmmount!=0)
                     {      gn100=0;
                     System.out.println("Entered Ammount ="+userInputAmmount);
                           if((userInputAmmount/100)!=0 && n100>=gn100)
                           {
                                  gn100=userInputAmmount/100;
                                  userInputAmmount=userInputAmmount%100;
                                  //System.out.println("Given notes of 100 ="+gn100);
                                  n100=n100-gn100;
                           }
                           //System.out.println("Given notes of 100="+gn100);
                     }
                     //Main Logic End
                     //Display Result
                     if(userInputAmmount!=0){
                           System.out.println("\nEither the proper ammount is not in this ATM:Please try later\n OR \nEnter different ammount");               
                     }
                     else if(userInputAmmount==0)
                     {
                           System.out.println("Given notes of 1000="+gn1000);
                           System.out.println("Given notes of 500="+gn500);
                           System.out.println("Given notes of 100="+gn100);

                     }

              }
             
             
       }
}