黑马程序员-JAVA基础-练习之存储学生信息

  题目:有一批学生,每个学生有3门课程,从键盘上输入以下数(姓名,三门成绩)输入格式:如zhangsan,30,40,60计算出总成绩。并把学生的信息和计算出的总分数高低顺序存放到磁盘文件中。

  思路:

  1、创建一个描述学生信息的类:Student类,包含如下属性:姓名(name)、三门成绩(A、B、C) 和总成绩(sum);

  2、建立学生工具类:StudentTool 。其中的方法主要是从键盘上录入学生信息studentInfo() 和 将学生信息写入到磁盘文件中的方法writeToFile() 。

  3、因为学生数量较多,而且还要把学生的信息按分数的高低排序,则选择Set集合中的TreeSet集合。 

  通过理清思路,可以得到如下代码:

  学生类:

  思路:1、定义学生属性:姓名、三门成绩、总成绩。

     2、因为用TreeSet集合存储,则要实现Comparable接口来完成自然排序以及保证元素的唯一性(详情见:黑马程序员-JAVA基础-Java 集合之Set 接口

)。   3、重写compareTo方法、hashCode方法、equals方法,以及为了方便输出重写toString() 方法。

 1 public class Student implements Comparable{
 2     private String name ; 
 3     private int a , b , c ; 
 4     private int sum ;
 5     public Student(String name , int a , int b ,int c) {
 6         this.name = name ; 
 7         this.a = a ;
 8         this.b = b ; 
 9         this.c = c ;
10     }
11 //    get和set方法。
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public int getA() {
19         return a;
20     }
21     public void setA(int a) {
22         this.a = a;
23     }
24     public int getB() {
25         return b;
26     }
27     public void setB(int b) {
28         this.b = b;
29     }
30     public int getC() {
31         return c;
32     }
33     public void setC(int c) {
34         this.c = c;
35     } 
36     public int getSum() {
37         return sum;
38     }
39     public void setSum(int sum) {
40         this.sum = sum;
41     }
42 //   覆写compareTo方法
43     public int compareTo(Object o) { 
44         if ( !(o instanceof Student))
45             throw new RuntimeException("匹配错误") ;
46         Student stu = (Student) o ; 
47 //        按照学生的名字排序
48         return this.name.compareTo(stu.getName());
49     }
50 //    重写hashCode方法
51     public int hashCode() {
52         return this.name.hashCode() * 87 + sum ; 
53     }
54 //    重写equals方法
55     public boolean equals(Object o) {
56         if ( !(o instanceof Student))
57             throw new RuntimeException("匹配错误") ;
58         Student stu = (Student) o ; 
59         if(this.name.equals(stu.getName())&&this.a == stu.getA()&&this.c == stu.getC())
60             if (this.sum == this.getSum())
61                 return true ;
62         return false ;
63     }
64     public String toString() {
65         return "Stuent["+this.name+","+this.a+","+this.b+","+this.c+"]" ;
66     }
67 }

  学生工具类:

  思路:1、建立学生录入方法和学生信息存入磁盘的方法。

     2、学生录入方法:通过重载,一个实现自定义比较器,一个则按自然排序。 

 1 public class StudentTool {
 2 //    建立一个学生读入方法,按自然顺序排序。
 3     public Set<Student> studentInfo() throws IOException {
 4         return studentInfo(null);
 5     }
 6 //    建立一个学生读入方法,支持自定义比较器
 7     public Set<Student> studentInfo(Comparator comp) throws IOException {
 8         
 9 //        判断比较器是否为空。
10         TreeSet<Student> StudentSet = null ;
11         if ( comp == null)   
12             StudentSet = new TreeSet<Student>( ) ;
13         else
14             StudentSet = new TreeSet<Student>(comp) ;
15 //        建立键盘录入方法
16         BufferedReader br = 
17              new BufferedReader(new InputStreamReader(System.in)) ; 
18         
19         String line = null ; 
20         while ((line = br.readLine()) != null) {
21 //            判断是否停止录入
22             if ("over".equals(line))
23                 break;
24             String[] info = line.split(",") ;  
25 //            创建学生对象
26             Student stu = new Student(info[0] , new Integer(info[1]).intValue()
27                     ,new Integer(info[2]).intValue(),new Integer(info[3]).intValue());
28             stu.setSum(stu.getA()+stu.getB() +stu.getC()) ;
29 //            加入TreeSet集合。
30             StudentSet.add(stu) ; 
31         }
32         br.close() ;
33         return StudentSet ; 
34     }
35 //    将学生信息写入文件:
36     public void writeToFile(Set<Student> studentSet)throws IOException {
37         BufferedWriter bufw = 
38             new BufferedWriter(new FileWriter("D:\\StudentInfo.txt")) ;
39         for(Student stu : studentSet) {
40             bufw.write(stu.toString()+"\t") ;
41             bufw.write(stu.getSum() + "") ;
42             bufw.newLine(); 
43         }
44         bufw.close() ;
45     }
46 }

  主函数:

 1 public class StudentDemo {
 2     public static void main(String[] args) throws IOException {
 3 //        创建一个集合对象,用于装学生类。
 4         Set<Student> stuSet = new TreeSet() ; 
 5 //        创建学生工具类对象。
 6         StudentTool stuTool = new StudentTool() ; 
 7 //        录入学生,按成绩的高低排序。
 8 //        stuSet = stuTool.studentInfo(new Comparator() { 
 9 //            public int compare(Object o1, Object o2) { 
10 //                Student stu1 = (Student) o1 ; 
11 //                Student stu2 = (Student) o2 ; 
12 //                if (stu1.getSum() == stu2.getSum())
13 //                    return stu1.getName().compareTo(stu2.getName());
14 //                return stu2.getSum() - stu1.getSum() ;
15 //                
16 //            }
17 //        }) ; 
18         stuSet = stuTool.studentInfo() ;
19 //        将学生信息写入文档。
20         stuTool.writeToFile(stuSet) ; 
21     }
22 }

  

  

 

 

 

 

 

 

 

posted @ 2013-05-11 23:43  贺佐安  阅读(847)  评论(0编辑  收藏  举报