- /*
- 需求:
- 有五个学生,每个学生有3门课程的成绩,从键盘输入以上数据(包括姓名,三名课的成绩),输入的格式:
- 张三,30,40,60计算出总成绩,并把学生的信息和计算出的总分数低高顺序存放在磁盘文件"studentInfo.txt"中;
- 思路:
- 1.因为有五个学生对象,它们属于同一类事物,所以要建立一个类,用于描述他们;
- 2.因为学生的信息需要排序,对象的排序,我们自然的想到TreeSet集合;
- 3.最后,我们要定义一个IO输出流,将集合中的学生信息写入到指定的文件中去;
- */
- import java.util.*;
- import java.io.*;
- class StudentInfo
- {
- public static void main(String[] args)
- {
- StudentInfoTool.sortStudent();
- }
- }
- //学生工具类,用于建立学生对象,通过输入流获取学生信息,并把学生对象存入到Set集合中,在Set集合中自动排序后,使用输出流,将学生信息写入到指定的文件中去;
- class StudentInfoTool
- {
- public static void sortStudent()
- {
- Set<Student> studentInfo=new TreeSet<Student>();
- File file=new File("studentInfo.txt");
- BufferedWriter bufw=null;
- BufferedReader bufr=null;
- try
- {
- if (file.exists())
- {
- file.createNewFile();
- }
- bufw=new BufferedWriter(new FileWriter(file));
- bufr=new BufferedReader(new InputStreamReader(System.in));
- try
- {
- String line;
- while ((line=bufr.readLine())!=null)
- {
- if (line.equals("over"))
- {
- break;
- }
- String[] info=line.split(",");
- studentInfo.add(new Student(info[0],Double.parseDouble(info[1]),Double.parseDouble(info[2]),Double.parseDouble(info[3])) );
- }
- }
- catch (ClassCastException c)
- {
- throw new ClassCastException("类型不匹配!");
- }
- for (Student s:studentInfo )
- {
- bufw.write(s.toString());
- bufw.newLine();
- bufw.flush();
- }
- }
- catch (IOException io)
- {
- throw new RuntimeException("输出流,输入流读取失败!");
- }
- finally
- {
- try
- {
- if (bufw!=null)
- {
- bufw.close();
- }
- }
- catch (IOException i)
- {
- throw new RuntimeException("输出流关闭失败!");
- }
- try
- {
- if (bufr!=null)
- {
- bufr.close();
- }
- }
- catch (IOException i)
- {
- throw new RuntimeException("输入流关闭失败!");
- }
- }
- }
- }
- class Student implements Comparable<Student>
- {
- //声明表示学生属性的变量;
- private String name;
- private double chineseScore,mathScore,englishScore,sumScore;
- //Student类的构造函数;
- Student(String name,double chineseScore,double mathScore,double englishScore)
- {
- this.name=name;
- this.chineseScore=chineseScore;
- this.mathScore=mathScore;
- this.englishScore=englishScore;
- this.sumScore=chineseScore+mathScore+englishScore;
- }
- //定义学生属性的get和set方法;
- public void setName(String name)
- {
- this.name=name;
- }
- public String getName()
- {
- return name;
- }
- public void setChineseScore(double chineseScore)
- {
- this.chineseScore=chineseScore;
- }
- public double getMathScore()
- {
- return mathScore;
- }
- public void setEnglishScore(double englishScore)
- {
- this.englishScore=englishScore;
- }
- public double getEnglishScore()
- {
- return englishScore;
- }
- //复写继承至Comparable接口的compareTo方法;
- public int compareTo(Student s)
- {
- if (Double.valueOf(this.sumScore).compareTo(Double.valueOf(s.sumScore))==0)
- {
- return this.name.compareTo(s.name);
- }
- return Double.valueOf(this.sumScore).compareTo(Double.valueOf(s.sumScore));
- }
- //应为学生对象可能会存入到hashSet集合中,所以复写hashCode()和equals();
- public int hashCode()
- {
- return this.name.hashCode()+(int)this.sumScore*38;
- }
- public boolean equals(Student s)
- {
- return this.name.equals(s.name)&&(this.sumScore==s.sumScore);
- }
- //复写继承至Object类中的toString(),便于输出打印;
- public String toString()
- {
- return this.name+"\t"+this.chineseScore+"\t"+this.mathScore+"\t"+this.englishScore+"\t"+this.sumScore;
- }
- }
复制代码
问题在图片上 |