A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

package com.heima.code;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/*
有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括姓名,三门科成绩),
输入的格式:
如:zhangsan,30,40,50计算总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stu.txt"中。

本程序程序实现过程:
1.描述学生对象;
2.定义一个可操作学生对象的工具类。

思想:
1.通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
2.因为学生对象有很多,那么就需要存储,使用到集合。因为要对学生的总分排序。
   所以使用TreeSet.
3.将集合中的信息写入到一个文件中。

* */

//描述学生类
class Student implements Comparable<Student>{
        private String name;
        private int    math;
        private int    chinaese;
        private int    english;
        private int     sum;
       
        Student(String name,int math,int chinaese,int english){
                this.name = name;
                this.math = math;
                this.chinaese = chinaese;
                this.english = english;
               
                 sum = math+chinaese+english;//每个学生成绩的总分
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getMath() {
                return math;
        }
        public void setMath(int math) {
                this.math = math;
        }
        public int getChinaese() {
                return chinaese;
        }
        public void setChinaese(int chinaese) {
                this.chinaese = chinaese;
        }
        public int getEnglish() {
                return english;
        }
        public void setEnglish(int english) {
                this.english = english;
        }
        public int getSum() {
                return sum;
        }
        public void setSum(int sum) {
                this.sum = sum;
        }
        public int hashCode(){
                return name.hashCode()+sum*4;
               
        }
        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 String toString(){
                return "Student["+name+","+math+","+chinaese+","+english+"]";
               
        }
        @Override
        public int compareTo(Student str) {
                int num = new Integer(this.sum).compareTo(new Integer(str.sum));
                return num;
        }
       
}
//学生工具类
class StudentInfoTool{
        public static Set<Student>getStudents() throws IOException{
                 return getStudents(null);
               
        }
        //定义比较器,对数据逆转
        public static Set<Student>getStudents(Comparator<Student> cmp) throws IOException{
                BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
                String line = null;
                Set<Student>  stus = new TreeSet<Student>();
                if(cmp==null)
                        stus = new TreeSet<Student>();
                else
                        stus = new TreeSet<Student>(cmp);
                while((line=bufr.readLine())!=null){
                        if("over".equals(line))
                                break;
                        String[] info = line.split(",");
                        Student stu = new Student(info[0],Integer.parseInt(info[1]),
                                                          Integer.parseInt(info[2]),
                                                            Integer.parseInt(info[3]));
                        stus.add(stu);
                }
                bufr.close();
                return stus;       
        }
   
     public static void WriterFile(Set<Student> stus) throws IOException{
                BufferedWriter bufw = new BufferedWriter(new FileWriter("E:\\studentInfo.txt"));
                for(Student stu : stus){
                        bufw.write(stu.toString()+"\t");
                        bufw.write(stu.getSum()+"");
                        bufw.newLine();
                        bufw.flush();
                }
                bufw.close();
        }
}
public class StudentDemo01 {

        public static void main(String[] args) throws IOException {
   
        //强行逆转比较器
    Comparator<Student> cmp =  Collections.reverseOrder();               
        Set<Student> stus = StudentInfoTool.getStudents(cmp);
    StudentInfoTool.WriterFile(stus);
        }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马