(95)键盘输入学生信息,存入文件

有5个学生,每个学生有三门课程,从键盘输入以上数据(包括姓名、三门课成绩)输入格式:如zhangsan,30,40,50计算出总成绩,并将学生的信息和计算出的总分数从高到底存放在磁盘文件“stu.txt”
1,描述学生对象
2,定义一个可以操作学生对象的工具类
思想:
1,通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象
2,因为你学生有很多,那么就需要存储,使用集合,因为要对学生的总分数进行排序,所以可以用用TreeSet
3,将集合的信息写入到文件

//学生类
public class Student implements Comparable<Student> {

    private String name;
    private  int  chinGrade;
    private  int  mathGrade;
    private  int   engGrade;
    private   int  sumGrade;
    Student(String name,int chinGrade,int   mathGrade,int   engGrade ){
        this.name=name;
        this.chinGrade=chinGrade;
        this.mathGrade=mathGrade;
        this.engGrade=engGrade;
        sumGrade= chinGrade+mathGrade+ engGrade;
    }


    //可能被二叉树使用
    public int compareTo(Student stu) {
        if(this.sumGrade>stu.sumGrade)
            return -1;
        if(this.sumGrade==stu.sumGrade)
            return this.name.compareTo(stu.name);
        return 1;
    }

    public int getSumGrade() {
        return sumGrade;
    }


    public String getName() {
        return name;
    }

    //可能被哈希用到
    public int hashCode() {
        return name.hashCode()+sumGrade*85;
    }
    public boolean equals(Object obj) {
        if(!(obj instanceof Student ))
            throw new ClassCastException();
        Student stu=(Student)obj;
        return this.getName().equals(stu.getName())&&this.sumGrade==stu.sumGrade;
    }
    public String toString() {
        return    "姓名:"+name+"  语文:"+chinGrade+"  数学:"+mathGrade+"  英语"+engGrade+"   总分:"+sumGrade;
    }
}

//
import java.io.*;
import java.util.*;
public class StuDemo {
    public static TreeSet<Student> getStudents() throws IOException{
        return getStudents(null);
    }

    public static TreeSet<Student> getStudents(Comparator<Student> cmp) throws IOException//将数据写入集合
    {


        TreeSet<Student> ts=null;
        if(cmp==null)
            ts=new TreeSet<Student>();
        else
            ts=new TreeSet<Student>(cmp);
        int count=1;

          while(count<=5) 
          {
            System.out.println("请输入第"+count+"名学生的信息");
            BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
            String line=bufr.readLine();
            String[] every=line.split(",");

            //将键盘输入的数据封装成对象
            ts.add(new Student(every[0],Integer.parseInt(every[1]),Integer.parseInt(every[2]),Integer.parseInt(every[3])));

            count++;
         }
          return ts;
    }
    public static void  write2File(TreeSet<Student> ts)throws IOException//将集合中的数据写入文件
    {
         //将集合中的数据写入文件中
          FileWriter  fos=new FileWriter("E:\\grade.txt");//因为要用到一行一行的写,所以用Writer
          BufferedWriter bufw=new BufferedWriter(fos);
          bufw.write("学生成绩以及排名如下:");
          bufw.newLine();
          Iterator<Student> it=ts.iterator();

          while(it.hasNext()) {
             Student stu=it.next();
             bufw.write(stu.toString()); 
             bufw.newLine();
             bufw.flush();
          }
          bufw.close();
    }
    public static void main(String[] args)throws IOException {

        //Comparator<Student> cmp=Collections.reverseOrder(new MyComp());
        //TreeSet ts=getStudents(cmp);//用比较器比较
        TreeSet ts=getStudents();//用compareTo比较
        write2File(ts);   
    }

}
//升序比较器
public class MyComp implements  Comparator<Student>{

    public int compare(Student stu1,Student stu2){

        if(stu1.getSumGrade()>stu2.getSumGrade())
            return 1;
        if(stu1.getSumGrade()>stu2.getSumGrade())
            return stu1.getName().compareTo(stu2.getName());
        return -1;
    }


}
posted @ 2017-07-26 17:17  测试开发分享站  阅读(156)  评论(0编辑  收藏  举报