Thursday, 12 April 2018

Corejava-Slip3


Slip3.Write a java program to accept the details of ‘n’ employees (EName ,Salary) from the user, store them into the Hashtable and displays the Employee Names havin maximum Salary.

import java.util.*;
import java.io.*;
public class Slip3
{
public static void main(String args[])throws Exception
{
int n,sal=0;
String name="";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Hashtable h = new Hashtable();
System.out.println("\nEnter number of Employee : ");
n = Integer.parseInt(br.readLine());
for(int i = 1; i <= n; i++)
{
System.out.println("\nEnter name");
name = br.readLine();
System.out.println("\nEnter Salary : ");
sal=Integer.parseInt(br.readLine());
h.put(name,sal);
}

Enumeration v = h.elements(); //sal
Enumeration k = h.keys(); //name
while(k.hasMoreElements())
{
System.out.println(k.nextElement()+" "+v.nextElement());
}
int max = 0;String str="";
k = h.keys();
v = h.elements();
while(v.hasMoreElements())
{
sal=(Integer)v.nextElement();
name = (String)k.nextElement();
if(sal>max)
{
max = sal;
str = name;
}
}
System.out.println(str +" has maximum salary is "+max);
}
}

Another way

/*Title:  Write a java program to accept the details of  ‘n’ employees (EName ,Salary) from the user, 
 store them into the Hashtable and displays the Employee Names having maximum Salary. */ 
import java.util.*;
import java.io.*;
class col
  {
   public static void main(String args[])
     {
      Hashtable ht=new Hashtable();
      System.out.println();
      int n;
      while(true)
        {
         System.out.println("How many elements you wish to enter:");
         try
           {
            n=Integer.parseInt(Accept());
            if(n>0)
            break;

            else
            System.out.println("\n\tNegative number is not allowed.");
           }
         catch(Exception e)
           {
            System.out.println("\nInvalid");
           }
        }
      for(int i=0;i<n;i++)
        {
         System.out.println("\n\tEnter the name of employee:");
         String name=Accept();
         Float salary=null;
         while(true)
           {
            System.out.println("Enter the salary of the employee:");
            try
              {
               salary=new Float(Accept());
               if(salary.floatValue()>0)
               break;
       
               else
               System.out.println("\n\tNegative Salary not allowed.");
              }
            catch(Exception e)
              {
               System.out.println("Invalid.");
              }
           }
         ht.put(name,salary);
        }
      Enumeration e=ht.keys();
      if(e.hasMoreElements())
      System.out.println("\nDetails of Employees are:");
      while(e.hasMoreElements())
        {
         Object name=e.nextElement();
         Object salary=ht.get(name);
         System.out.println("\n\tName:"+name+"Salary:"+salary);
        }
      Object m=Collections.max(ht.values());
      System.out.println("\n\tThe maximum salary is:"+m);
     }
   public static String Accept()
     {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      String str=null;
      try
        {
         str=br.readLine();
        }
      catch(Exception e)
        {
        }
      return str;
     }

  }