博客信息

IO流小练习

发布时间:『 2019-06-16 09:45』  博客类别:java基础  阅读(687)

集合Io流小练习

有五个学生,每个学生有3门课的成绩;

从键盘输入以上数据(包括姓名,三门课成绩);

输入的格式:如 zhangsan30,40,50计算出总成绩;

并把学生的信息和计算出的总分数高低顺序存放在磁盘文件stu.txt中;

 

思路:

1、描述学生对象(通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象)

2、定义一个可操作学生对象的工具类

(1) 因为学生有很多,那么就需要存储,使用到集合,因为要对学生的总分排序,所以可以使用到TreeSet集合;

(2) 将集合中的信息写入到一个文件夹中;

3、为了考虑排序规则的变化,程序的拓展性,需要对对应的代码进行优化;

 

代码如下

 

实体类

package com.javaxl.io;

/**
 * @author 小李飞刀
 * @site www.javaxl.com
 * @company
 * @create  2019-06-16 21:04
 */
public class Student implements Comparable<Student>{
    private String name;
    private int n1;
    private int n2;
    private int n3;
    private int total;

    public Student(String name, int n1, int n2, int n3) {
        this.name = name;
        this.n1 = n1;
        this.n2 = n2;
        this.n3 = n3;
        this.total = n1+n2+n3;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getN1() {
        return n1;
    }

    public void setN1(int n1) {
        this.n1 = n1;
    }

    public int getN2() {
        return n2;
    }

    public void setN2(int n2) {
        this.n2 = n2;
    }

    public int getN3() {
        return n3;
    }

    public void setN3(int n3) {
        this.n3 = n3;
    }

    public int getTotal() {
        return total;
    }

    @Override
    public int compareTo(Student o) {
        int num = this.getTotal() - o.getTotal();
        if(num == 0)
            return this.getName().compareTo(o.getName());
        return num;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", n1=" + n1 +
                ", n2=" + n2 +
                ", n3=" + n3 +
                ", total=" + total +
                '}';
    }
}


实体类工具

package com.javaxl.io;

import java.io.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author 小李飞刀
 * @site www.javaxl.com
 * @company
 * @create  2019-06-16 21:07
 */
public class StudentInfoTool {


    public static Set<Student> getStudents(Comparator comp)throws IOException{
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

        String line = null;
        Set<Student> stus = comp == null ? new TreeSet<>() : new TreeSet<>(comp);
        while ((line = bufr.readLine()) !=null){
            if("over".equals(line))
                break;

            String[] info = line.split(",");

            Student stu = new Student(info[0],
                    Integer.valueOf(info[1]),
                    Integer.valueOf(info[2]),
                    Integer.valueOf(info[3]));

            stus.add(stu);
        }
        return stus;
    }

    public static void write2File(Set<Student> stus) throws IOException{
        BufferedWriter bufw = new BufferedWriter(new FileWriter("C:\\stuInfo.txt"));
        for (Student stu : stus) {
            bufw.write(stu.toString());
            bufw.newLine();
            bufw.flush();
        }
        bufw.close();
    }

    public static void main(String[] args) throws IOException {
//        Set<Student> stus = StudentInfoTool.getStudents();
//        优化后
//        Set<Student> stus = StudentInfoTool.getStudents(null);
        Comparator comp = Collections.reverseOrder();
        Set<Student> stus = StudentInfoTool.getStudents(comp);
        StudentInfoTool.write2File(stus);
    }
}


效果图如下:


小李飞刀_IO流





关键字:     Java基础       IO流       集合  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有