1. package d330;  
  2.  
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.util.ArrayList;  
  7. import java.util.Iterator;  
  8. import java.util.Map;  
  9. import java.util.Map.Entry;  
  10. import java.util.Set;  
  11. import java.util.TreeMap;  
  12.  
  13. public class StudentDemo {  
  14.     public static void main(String[] args)throws IOException {  
  15.         BufferedReader bufi=  
  16.             new BufferedReader(new InputStreamReader(System.in));  
  17.           
  18.         Controls con=Controls.getControl();  
  19.         con.add(new Student("1","zhangsan"));  
  20.         con.add(new Student("2","lisi"));  
  21.         con.add(new Student("3","wangwu"));  
  22.         con.add(new Student("4","zouqi"));  
  23.         con.show();  
  24.           
  25.         while(true)  
  26.         {  
  27.             String len=null;  
  28.             System.out.print("请输入候选人的代号和姓名:");  
  29.  
  30.             len=bufi.readLine();  
  31.                 con.select(len);  
  32.         }  
  33.     }  
  34.       
  35. }  
  36. class Student implements Comparable  
  37. {  
  38.     private String name;  
  39.     private String code;  
  40.     Student(String code,String name)  
  41.     {  
  42.         this.name=name;  
  43.         this.code=code;  
  44.     }  
  45.     public String getCode() {  
  46.         return code;  
  47.     }  
  48.     @Override 
  49.     public String toString() {  
  50.         return  code + " : "+name;  
  51.                   
  52.     }  
  53.     @Override 
  54.     public int compareTo(Object o) {  
  55.         if(!(o instanceof Student))  
  56.             throw new RuntimeException("输入的不是学生实例");  
  57.         Student s=(Student)o;  
  58.         return code.compareTo(s.code);  
  59.     }  
  60.       
  61. }  
  62. class Controls  
  63. {  
  64.     TreeMap<Student,Integer> tm=new TreeMap<Student,Integer>();  
  65.     Set<Map.Entry<Student, Integer>> entrySet=tm.entrySet();  
  66.     private Controls(){}  
  67.     private static Controls con=new Controls();  
  68.     public static Controls getControl()  
  69.     {  
  70.         System.out.println("请输入候选人的代号和姓名:");  
  71.         return con;  
  72.     }  
  73.     public void add(Student stu)  
  74.     {  
  75.         tm.put(stu,0);  
  76.     }  
  77.     public void show()  
  78.     {  
  79.         Iterator<Map.Entry<Student, Integer>> it=entrySet.iterator();  
  80.         while(it.hasNext())  
  81.         {         
  82.             Map.Entry<Student, Integer> s= it.next();  
  83.             System.out.println(s.getKey()+" \t【  "+s.getValue()+"票 】");  
  84.         }  
  85.     }  
  86.     public void select(String code)  
  87.     {  
  88.           
  89.         Iterator<Map.Entry<Student, Integer>> it=entrySet.iterator();  
  90.               
  91.         if(code.equals("0"))  
  92.         {  
  93.             show();  
  94.             getB();  
  95.             System.exit(0);  
  96.         }  
  97.         if(code.matches("[1-4]"))  
  98.         {  
  99.             while(it.hasNext())  
  100.             {     
  101.                 Entry<Student, Integer> s= it.next();  
  102.                   
  103.                 if(s.getKey().getCode().equals(code))  
  104.                 {  
  105.                     int count=s.getValue();  
  106.                     count=count+1;  
  107.     //              tm.put(s.getKey(), count);  
  108.                     s.setValue(count);  
  109.                 }  
  110.             }         
  111.         }  
  112.         else System.out.println("您输入的选号不正确,请重新输入");  
  113.     }  
  114.     public void getB()  
  115.     {     
  116.         String a[][]=new String[entrySet.size()][2];  
  117.         Iterator<Map.Entry<Student, Integer>> it=entrySet.iterator();  
  118.         int h=0;  
  119.         while(it.hasNext())  
  120.         {  
  121.             Entry<Student, Integer> max=it.next();  
  122.               
  123.              a[h][0]=max.getKey().toString();  
  124.              a[h][1]=max.getValue().toString();  
  125.                
  126.              h++;  
  127.         }  
  128.         int max=0;  
  129.         int index=0;  
  130.         for(int x=0;x<a.length;x++)  
  131.         {  
  132.             if(max<Integer.parseInt(a[x][1]))  
  133.             {  
  134.                 max=Integer.parseInt(a[x][1]);  
  135.                 index=x;  
  136.             }  
  137.         }  
  138.         System.out.println("投票最终结果是"+a[index][0]+"同学,最后以 "+a[index][1]+"最终当选班长");  
  139.     }  
  140.