- import java.io.*;
- import java.util.*;
- //学生类
- class Student implements Comparable<Student>
- {
- private String name;
-
- private int ma,cn,en;
- private int sum;
- Student(String name,int ma,int cn,int en)
- {
- this.name=name;
- this.ma=ma;
- this.cn=cn;
- this.en=en;
- sum=ma+cn+en;
- }
- public String getName()
- {
- return name;
- }
- public int getSum()
- {
- return sum;
- }
- public int hashCode()
- {
- return name.hashCode()+sum*7;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("类型不匹配");
- Student s=(Student)obj;
- return this.name.equals(s.name) && this.sum==s.sum;
- }
- public int compareTo(Student s)//让对象本身具备比较性 在数据传入的时候会自动进行比较无需再次调用
- {
- int num=new Integer(this.sum).compareTo(new Integer(s.sum));
- if(num==0)
- return this.name.compareTo(s.name);
- return num;
- }
- public String toString()
- {
- return "Student[" + name +","+ "ma" + "," + "cn" + "," + "en" +"]";
- }
- }
- //学生工具类 输入信息并存入集合
- class StudentInfoTool<T>
- {
- public static Set<Student> getStudents() throws IOException
- {
- BufferedReader bufr=
- new BufferedReader(new InputStreamReader(System.in));
- String line=null;
-
- Set<Student> stus=new TreeSet<Student>();
- while( (line=bufr.readLine() )!=null )
- {
- if("voer".equals(line))
- break;
- String[] in=line.split(",");
- Student stu=new Student(in[0],
- Integer.parseInt(in[1]),
- Integer.parseInt(in[2]),
- Integer.parseInt(in[3]));//学生对象建立好了 然后放入集合
- stus.add(stu);//对象放入集合
- }
- bufr.close();
- return stus;
- }
- public static void write2File(Set<Student> stus) throws IOException
- {
- BufferedWriter bufw=new BufferedWriter(new FileWriter("Student.txt"));
- for(Student stu:stus)
- {
- bufw.write(stu.toString()+"\t");
-
- // bufw.write(stu.getSum());
-
- bufw.newLine();
- bufw.flush();
- }
- }
- public static Set<Student> getStudent() {
- // TODO Auto-generated method stub
- return null;
- }
- }
- //主函数类
- class A
- {
- public static void main(String[] args) throws IOException
- {
- Set<Student> stus=StudentInfoTool.getStudents();
- StudentInfoTool.write2File(stus);
- }
- }
复制代码 |