- /*
- 有五个学生,每个学生有三门课的成绩,
- 从键盘输入以上数据(包括姓名,三门课的成绩),
- 输入的格式,如;zhangsan,30,40,60,计算出总成绩,
- 并把学生的信息和计算出的总分数高低顺序存放在磁盘文件“stud.txt”中
- */
- import java.io.*;
- import java.util.*;
- class Student implements Comparable<Student>
- {
- private String name;
- private int zongfen;
- private int en,end,end1;
- Student(String name,int en,int end ,int end1)
- {
- this.name= name;
- this.en = en;
- this.end = end;
- this.end1 = end1;
- zongfen = en + end +end1;
- }
- public int compareTo(Student s)
- {
- int num = new Integer(this.zongfen).compareTo(new Integer(s.zongfen));
- if(num==0)
- return this.name.compareTo(s.name);
- return num;
-
- }
- public int hashCode()
- {
- return 60;
- }
- public boolean equals(Object obj )
- {
- if(!(obj instanceof Student))
- throw new RuntimeException("类型不匹配");
- Student s= (Student)obj;
- return this.name.equals(s.name) && this.zongfen==s.zongfen;
- }
- public String getName()
- {
- return this.name;
- }
- public int getFen()
- {
- return zongfen;
- }
-
- }
- class StudentTool
- {
- public static Set<Student> show()throws IOException
- {
- BufferedReader br =
- new BufferedReader(new InputStreamReader(System.in));
- Set<Student> set= new TreeSet<Student>();
-
- String len= null;
- while((len = br.readLine())!=null)
- {
- if("over".equals(len))
- break;
- String[] arr = len.split(",");
- Student s= new Student(arr[0],Integer.parseInt(arr[1])
- ,Integer.parseInt(arr[2])
- ,Integer.parseInt(arr[3]));
- set.add(s);
- }
- br.close();
- return set;
- }
- public static void run(Set<Student> set)throws IOException
- {
- BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
- for(Student se : set)
- {
- bw.write(se.getName());
- bw.write(se.getFen());
- bw.newLine();
- bw.flush();
-
- }
- bw.close();
-
- }
- }
- class IOTestDemo
- {
- public static void main(String[] args) throws IOException
- {
- Set<Student> set= StudentTool.show();
- StudentTool.run(set);
- }
- }
复制代码 |
|