- import java.io.*;
- import java.util.*;
- class Student
- {
- private int ma,cn,en;
- private String name;
- 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 int compareTo(Student s)
- {
- int num = new Integer(this.sum).compareTo(new Integer(s.sum));
- if(num == 0)
- return this.name.compareTo(s.name);
- }
- public int hashCode()
- {
- return name.hashCode+sum*26;
- }
- public boolean equales(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("类型不匹配");
- Student s = (Student)obj;
- return this.name.equals(s.name)&&this.sum == s.sum;
- }
- public String toString()
- {
- return "student["+name+","+ma+","+cn+","+en+"]";
- }
- }
- class StudentInfotool
- {
- public static Set<Student> getStudent() throws IOException
- {
- BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- Set<Student> stus = new TreeSet<Student>();
- while((lend=buff.readLine())!=null)
- {
- if("over".equeals(line))
- break;
- String[] info = line.spilt(",");
- Student stu = new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[3]));
- stus.add(stu);
- }
- buff.close();
- return stus;
- }
- public static void write2File(Set<Student> stus)
- {
- BufferedWriter bufu = BufferedWriter(new FileWriter ("stuinfo.txt"));
- for(Student stu : stus)
- {
- bufu.Write(stu.getSum()+"");
- bufu.Write(stu.toString());
- bufu.newLine();
- bufu.flush();
- }
- }
- }
- class StudentInfotest
- {
- public static void main(String[] args) throws IOException
- {
- Set<Student> stus = StudentInfotoo.getlStudent();
- StudentInfotool.write2File(stus);
- }
- }
复制代码 |