- /*
- 有五个学生,每个学生有3门课的成绩,
- 从键盘输入以上数据(包括姓名,三门课成绩)。
- 输入格式:如,zhangsan ,30, 40, 60计算出总成绩,
- 并把学生的信息和计算出的总分数高低顺序存放在磁盘文件“stud。txt”中。
- 1、描述学生对象。
- 2、定义一个可以操作学生对象的工具类。
- 思路:
- 1、通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
- 2、因为学生对象有很多,那么就需要存储,使用到集合。因为要对学生总分排序,
- 所以可以使用TreeSet集合。
- 3、将集合的信息写入到一个文件中。
- */
- import java.util.*;
- import java.io.*;
- class Student implements Comparable<Student>
- {
- private String name;
- private int ma,cn,en;
- private int sum;
- Student(String name,int ma,int cn,int en)
- {
- this.name = name;
- this.ma = ma;
- this.cn = cn;
- this.en = en;
- sum = ma+cn+en;
- }
- public String getName()
- {
- return name;
- }
- public int getMa()
- {
- return ma;
- }
- public int getCn()
- {
- return cn;
- }
- public int getEn()
- {
- return en;
- }
- public int getSum()
- {
- return sum;
- }
- public String toString()
- {
- return name+"\t"+ma+"\t"+cn+"\t"+en;
- }
- public int hashCode()
- {
- return name.hashCode()+sum*28;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("类型不匹配!");
- Student stu = (Student)obj;
- return(name.equals(stu.getName())&& ma==stu.getMa() && cn==stu.getCn() && en==stu.getEn());
- }
- public int compareTo(Student stu)
- {
- int flag = new Integer(sum).compareTo(new Integer(stu.getSum()));
- if (flag==0)
- return name.compareTo(stu.getName());
- return flag;
- }
- }
- class StudentInfoTool
- {
- public static Set<Student> getStudent()
- {
- return getStudent(null);
- }
- public static Set<Student> getStudent(Comparator<Student> cmp)
- {
- BufferedReader bufr = null;
- try
- {
- bufr = new BufferedReader(new InputStreamReader(System.in));
- TreeSet<Student> ts = null;
- if(cmp==null)
- ts = new TreeSet<Student>();
- ts = new TreeSet<Student>(cmp);
- String line;
- 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]));
- ts.add(stu);
- }
- return ts;
- }
- catch (IOException e)
- {
- throw new RuntimeException("输入流创建失败!");
- }
- finally
- {
- try
- {
- if(bufr!=null)
- bufr.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("读取流关闭失败!");
- }
- }
- }
- public static void writeToText(Set<Student> set,String textName)
- {
- BufferedWriter bufw = null;
- try
- {
- bufw = new BufferedWriter(new FileWriter(textName));
- for (Student stu : set )
- {
- bufw.write(stu.toString()+"\t");
- bufw.write(stu.getSum()+"");
- bufw.newLine();
- bufw.flush();
- }
- }
- catch (IOException e)
- {
- throw new RuntimeException("数据写入文件失败!");
- }
- finally
- {
- try
- {
- if(bufw!=null)
- bufw.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("写入流关闭失败!");
- }
- }
- }
- }
- class StudentInfoTest
- {
- public static void main(String[] args)
- {
- Comparator<Student> cmp = Collections.reverseOrder();
- Set<Student> set = StudentInfoTool.getStudent(cmp);
- StudentInfoTool.writeToText(set,"STUDENTINFO.txt");
- }
- }
复制代码 |