黑马程序员技术交流社区

标题: 这个代码有什么需要改进的吗?求大神指导 [打印本页]

作者: 刘德坤    时间: 2015-10-7 20:25
标题: 这个代码有什么需要改进的吗?求大神指导
/*4、 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
             输入格式为:name,30,30,30(姓名,三门课成绩),
             然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
             要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
*/
public class Test4 {
        public static void main(String[] args) throws IOException{
                Comparator<Student> cmp =Collections.reverseOrder();
                Set<Student> stus = StudentInfoTool.getStudent(cmp);
                StudentInfoTool.write2file(stus);
        }

}
class Student implements Comparable<Student>{
        private String name;
        private int math,cn,en;
        private int sum;
        Student(String name, int math, int cn, int en) {
                this.name = name;
                this.math = math;
                this.cn = cn;
                this.en = en;
                sum = math + 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);
                return num;
        }
        public String getName() {
                return name;
        }
        public int getSum() {
                return sum;
        }
        public int hashCode(){
                return name.hashCode()+sum*78;
        }
        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+","+cn+","+en+"]";
        }       
}
class StudentInfoTool{
        public static Set<Student> getStudent() throws IOException{
              return getStudent(null);
        }
        public static Set<Student> getStudent(Comparator<Student> cmp) throws IOException{
                BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
                String line = null;
                Set<Student> stus = null;
                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 write2file(Set<Student> stus)throws IOException{
                BufferedWriter bufw = new BufferedWriter(new FileWriter("stuinfo.txt"));
                for(Student stu: stus){
                        bufw.write(stu.toString()+"\t");
                        bufw.write(stu.getSum()+"");
                        bufw.newLine();
                        bufw.flush();
                }
                bufw.close();
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2