2010-12-01 50 views
2

我已经编写了一个用ID和名字排序名称的代码。按ID和名字排序员工详细信息的集合

import java.io.*; 
import java.util.*; 
public class TestEmployeeSort { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     String option=null; 
     System.out.println("Enter on which order sorting should be done \n1.Id \n2.FirstName \n3.LastName"); 
     List<Employee> coll = Name_Insert.getEmployees(); 

     BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
     option=br.readLine(); 

     int a=Integer.parseInt(option); 

      switch(a) 
      { 
      case 1: 
      Collections.sort(coll); 
      printList(coll); 
      break; 
      case 2: 
      Collections.sort(coll,new EmpSortByFirstName());// sort method 
      printList(coll); 
      break; 
      case 3: 
      Collections.sort(coll,new SortByLastName());// sort method 
      printList(coll); 
      } 
      } 
    private static void printList(List<Employee> list) { 
     System.out.println("EmpId\tFirstName\tLastName\tDate Of Joining\tDate of Birth"); 

     for (int i=0;i<list.size();i++) { 
      Employee e=list.get(i); 
      System.out.println(e.getEmpId() + "\t" + e.getFirstName() + "\t" + e.getLastname() +"\t" + e.getDate_Of_Joining()+"\t"+e.getDate_Of_Birth()); 
      } 
     } 

    } 

用于通过ID和第一名称排序我在Sort_this类

public class EmpSortByFirstName implements Comparator<Employee>{ 
     public int compare(Employee o1, Employee o2) { 
      return o1.getFirstName().compareTo(o2.getFirstName()); }} 

类似地对于ID此代码。 现在我想改变我的程序,就像我必须从你想要排序的基础上得到输入。如果用户给出id,我必须按id排序。如果用户给出名字,然后按名字排序。我想用if语句。如果用户输入1,则必须按ID 2排序,它必须按名排序

+0

您遇到的问题是什么?你似乎已经有了做你想做的事的代码。 – casablanca 2010-12-01 05:36:22

+0

你是什么意思'如果用户给firstname'。用户应该在输入中输入'名字'? – 2010-12-01 05:37:49

回答

1

创建用户输入令牌(字符串,整数等)到Comparator<Employee>的地图,并使用恰当的一个。

0

你的意思是你想摆脱使用switch?如果是的话,你可以尝试有地图的注册SOTER的:

import java.io.*; 
import java.util.*; 

public class TestEmployeeSort { 

    private static class EmployeeSortingManager { 

     private final List list; 
     private final Map<Integer, Comparator> registeredSorter = new HashMap<Integer, Comparator>(); 

     public EmployeeSortingManager(List list) { 
      this.list = list; 
      registerAvailableSorters(); 
     } 

     private void registerAvailableSorters() { 
      registeredSorter.put(1, null); 
      registeredSorter.put(2, new EmpSortByFirstName()); 
      registeredSorter.put(3, new SortByLastName()); 
     } 

     public void sortBy(int i) { 
      Comparator comparator = registeredSorter.get(i); 
      if (registeredSorter.get(i) != null) { 
       Collections.sort(list, comparator); 
      } else { 
       Collections.sort(list); 
      } 
     } 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     String option = null; 
     System.out.println("Enter on which order sorting should be done \n1.Id \n2.FirstName \n3.LastName"); 
     List<Employee> coll = Name_Insert.getEmployees(); 
     EmployeeSortingManager employeeSortingManager = new EmployeeSortingManager(coll); 

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     option = br.readLine(); 

     int a = Integer.parseInt(option); 

     employeeSortingManager.sortBy(a); 

     printList(coll); 
    } 

    private static void printList(List<Employee> list) { 
     System.out.println("EmpId\tFirstName\tLastName\tDate Of Joining\tDate of Birth"); 

     for (int i = 0; i < list.size(); i++) { 
      Employee e = list.get(i); 
      System.out.println(e.getEmpId() + "\t" + e.getFirstName() + "\t" + e.getLastname() + "\t" + e.getDate_Of_Joining() + "\t" + e.getDate_Of_Birth()); 
     } 
    } 
} 

另一个学尝试通过使用反射来去除执行每一个比较。这是非常复杂的,如果你使用的值不是Comparable,例如, StringIntegerDouble。你必须小心。

import java.io.*; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.util.*; 

public class TestEmployeeSort { 

    private static class EmployeeSortingManager { 

     private final List list; 
     private final Map<Integer, Method> registeredSorter = new HashMap(); 
     private BasicComparator comparator = new BasicComparator(); 

     public EmployeeSortingManager(List list) { 
      this.list = list; 
      registerAvailableSorters(); 
     } 

     private void registerAvailableSorters() { 
      registeredSorter.put(1, null); 
      registeredSorter.put(2, getEmployeeGetMethod("firstName")); 
      registeredSorter.put(3, getEmployeeGetMethod("lastName")); 
     } 

     private Method getEmployeeGetMethod(String fieldName) { 
      Method method = null; 
      try { 
       // create java style get method name from field name, e.g. getFieldName from fieldName 
       String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); 
       method = Employee.class.getMethod(getMethodName); 
      } catch (NoSuchMethodException ex) { 
      } catch (SecurityException ex) { 
      } 
      // null is return if you give invalid field name 
      return method; 
     } 

     public void sortBy(int i) { 
      Method get = registeredSorter.get(i); 
      if (get != null) { 
       comparator.setGetMethod(get); 
       Collections.sort(list, comparator); 
      } else { 
       Collections.sort(list); 
      } 
     } 
    } 

    private static class BasicComparator implements Comparator<Employee> { 

     private Method aGetMethod = null; 

     public void setGetMethod(Method aGetMethod) { 
      this.aGetMethod = aGetMethod; 
     } 

     @Override 
     public int compare(Employee o1, Employee o2) { 
      try { 
       Object value1 = aGetMethod.invoke(o1); 
       Object value2 = aGetMethod.invoke(o2); 
       if (value1 instanceof Comparable && value2 instanceof Comparable) { 
        // this should work with String, Integer, Double, etc. They all implement Comparable interface. 
        return ((Comparable) value1).compareTo((Comparable) value2); 
       } else { 
        // you will need to add your own comparision for other type of variable; 
        // obviously it is not possible to have a single comparison 
        // if your get method return something else. 
       } 
      } catch (IllegalAccessException ex) { 
      } catch (IllegalArgumentException ex) { 
      } catch (InvocationTargetException ex) { 
      } 
      // if cannot compare then they are equal. 
      return 0; 
     } 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     String option = null; 
     System.out.println("Enter on which order sorting should be done \n1.Id \n2.FirstName \n3.LastName"); 
     List<Employee> coll = Name_Insert.getEmployees(); 
     EmployeeSortingManager employeeSortingManager = new EmployeeSortingManager(coll); 

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     option = br.readLine(); 

     int a = Integer.parseInt(option); 

     employeeSortingManager.sortBy(a); 

     printList(coll); 
    } 

    private static void printList(List<Employee> list) { 
     System.out.println("EmpId\tFirstName\tLastName\tDate Of Joining\tDate of Birth"); 

     for (int i = 0; i < list.size(); i++) { 
      Employee e = list.get(i); 
      System.out.println(e.getEmpId() + "\t" + e.getFirstName() + "\t" + e.getLastname() + "\t" + e.getDate_Of_Joining() + "\t" + e.getDate_Of_Birth()); 
     } 
    } 
}