Saturday, November 23, 2013

Java HashSet Example

Create a class Employee with class members firstName(String), lastName(String) and salary(int).Do the following:

  • Create a HashSet of Employees.
  • Print the employee details.
  • Sort the employees in HashSet by salary and print the result.
  • Print the employee with the least salary.
  • Print the employee with the highest salary.
  • Search for an employee.

SOLUTION: 

 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Scanner;

public class HashSetExample{
 HashSet< Employee > hashSet;

 public static void main(String[] args){
  HashSetExample obj = new HashSetExample();
  obj.hashSet = new HashSet< Employee >();
  Scanner input = new Scanner(System.in);
  
  int i=0;
  do{
   System.out.println("Please select an option:");
   System.out.println("1:Add employee\n2:Print all");
   System.out.println("3:Sort\n4:Print employee with Heighest salary");
   System.out.println("5:Print employee with least salary\n6:Search employee");
   System.out.println("7:Exit");
   i=input.nextInt();
   switch(i){
   case 1:
    obj.addEmp();
    break;
    
   case 2:
    obj.printAll();
    break;
    
   case 3:
    obj.sortEmp();
    break;
    
   case 4:
    obj.heighSal();
    break;
    
   case 5:
    obj.lowSal();
    break;
    
   case 6:
    obj.searchEmp();
    break;
    
   case 7:
    break;
    
   default :
    System.out.println("Invalid input!!");
    break;
   }
  }while(i!=7);
 }//End main

 private void searchEmp() {
  Scanner input = new Scanner(System.in);
  System.out.println("Search Employee ..");
  System.out.println("Enter First name of employee:");
  String fname=input.next();
  boolean find=false;
  for(Employee e : hashSet){
   if(e.fname.equals(fname)){
    System.out.println(e);
    find=true;
   }
  }
  if(find==false)
   System.out.println("Employee not found.");
  
 }

 private void lowSal() {
  ArrayList< Employee > list = new ArrayList< Employee >(hashSet);
  Employee e = Collections.min(list,new Employee());
  System.out.println(e);
  
 }

 private void heighSal() {
  ArrayList< Employee > list = new ArrayList< Employee >(hashSet);
  Employee e = Collections.max(list,new Employee());
  System.out.println(e);
  
 }

 private void sortEmp() {
  ArrayList< Employee > list = new ArrayList< Employee >(hashSet);
  Collections.sort(list,new Employee());
  for(Employee e : list){
   System.out.println(e);
  }
 }

 private void printAll() {
  for(Employee e : hashSet){
   System.out.println(e);
  }
  
 }

 private void addEmp() {
  Scanner input = new Scanner(System.in);
  System.out.println("Enter first name and last name");
  String first=input.next();
  String last=input.next();
  System.out.println("Enter salary:");
  int sal=input.nextInt();
  hashSet.add(new Employee(first,last,sal));
 }
 
}

class Employee implements Comparator< Employee >{
 String fname;
 String lname;
 int salary;
 
 //Default constructor
 public Employee(){
  this("No","Name",0);
 }
 //Parametrized constructor
 public Employee(String a, String b, int c){
  fname=a;
  lname=b;
  salary=c;
 }
 
 public String toString(){
  return(String.format("%-20s%-6d", (fname+" "+lname),salary));
 }
 
 @Override
 public int compare(Employee a, Employee b) {
  if(a.salary==b.salary)
   return 0;
  return a.salary>b.salary?1:-1;
 }
}

No comments:

Post a Comment