基于控制台执行的,io流,学生管理系统

package com.qfedu.b_zuoye01;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

class Student {
    private String Id;
    private String name;

    public Student() {
    }

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "Id=" + Id +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Id == student.Id && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(Id, name);
    }

    public Student(String id, String name) {
        Id = id;
        this.name = name;
    }
}
public class Demo_02 {
    public static void main(String[] args) throws IOException {
        //输入流:
        BufferedReader br = new BufferedReader(new FileReader(new File("D:\\aaa\\User\\student.txt")));
        //输出流:
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\aaa\\User\\student.txt"),true));
        /**
         * List数组,存学生对象
         */
        List<Student> list = new ArrayList<>();
        while (true){
            Scanner scanner = new Scanner(System.in);
            //读取文件中的学生信息,存入list学生集合中
            String stu;
            while ((stu = br.readLine())!=null ){
                String[] split = stu.split(":");//用 : 把读取出来的信息如 01:张三 切割成一个字符串数组[01,张三]
                list.add(new Student(split[0],split[1]));//创建学生对象,并赋值,存入list集合中
            }
            System.out.println(list);
            System.out.println("-----欢迎来到学生管理系统--------");
            System.out.println("菜单:1、查询所有学生 2、添加学生 3、删除学生 4、修改学生 5、查询学生 6、退出");
            int a = scanner.nextInt();
            switch (a){
                case 1:
                    System.out.println("查询所有学生");
                    for (Student student : list) {
                        System.out.println("Id:"+student.getId()+"  "+"姓名:"+student.getName());
                    }
                    break;
                case 2:
                    System.out.println("添加学生");
                    System.out.println("输入学生id:");
                    String a1 = scanner.next();
                    System.out.println("输入学生姓名:");
                    String a2 = scanner.next();
                    bw.write(a1+":"+a2);
                    bw.newLine();
                    bw.flush();
                    System.out.println("添加成功!!!");
                    break;
                case 3:
                    System.out.println("删除学生");
                    System.out.println("请输入你要删除的学生的Id或者名字:");
                    String name = scanner.next();
                    /**
                     * 遍历list集合,删除指定的对象
                     */
                    for (int i = 0; i < list.size(); i++) {
                        Student student1 = new Student();
                        student1 = list.get(i);
                        if (list.get(i).getName().equals(name)||list.get(i).getId().equals(name)){
                            list.remove(student1);
                        }
                    }
                    System.out.println("删除成功!!");
                    /**
                     * 遍历删除后的list集合,然后把它重新存入txt文档中
                     */
                    //输出流:(没加true,所以会覆盖之前的文档中的内容);
                    BufferedWriter bw2 = new BufferedWriter(new FileWriter(new File("D:\\aaa\\User\\student.txt")));
                    for (Student student1 : list) {
                        bw2.write(student1.getId()+":"+student1.getName());
                        bw2.newLine();
                        bw2.flush();
                    }
                    bw2.close();
                    break;

                case 4:
                    System.out.println("修改学生");
                    System.out.println("请输入你要修改的学生id:");
                    String num = scanner.next();
                    int k = 0;//控制最后是输出修改成功,还是修改失败!!
                    for (Student student1 : list) {
                        if (student1.getId().equals(num)){
                            System.out.println("开始修改");
                            System.out.println("请输入你要修改的id:");
                            String num1 = scanner.next();
                            System.out.println("请输入你要修改的姓名:");
                            String num2 = scanner.next();
                            student1.setId(num1);
                            student1.setName(num2);
                            /**
                             * 开始遍历list集合,把它写入磁盘中
                             */
                            //输出流:(没加true,所以会覆盖之前的文档中的内容);
                            BufferedWriter bw3 = new BufferedWriter(new FileWriter(new File("D:\\aaa\\User\\student.txt")));
                            for (Student student2 : list) {
                                bw3.write(student2.getId()+":"+student2.getName());
                                bw3.newLine();
                            }
                            bw3.close();//关闭,输出流
                            k++;
                        }
                    }
                    switch (k){
                        case 0:
                            System.out.println("该学生不存在,请重新输入!!");
                            break;
                        default:
                            System.out.println("修改成功!!");
                            break;
                    }
                case 5:
                    System.out.println("查询学生");
                    System.out.println("请输入需要查询的学生的id或者姓名:");
                    String n1 = scanner.next();
                    for (Student student1 : list) {
                        if (student1.getId().equals(n1)||student1.getName().equals(n1)){
                            System.out.println("查询到的信息如下:");
                            System.out.println("Id:"+student1.getId()+"  "+"姓名:"+student1.getName());
                            break;
                        }
                    }
                    System.out.println("该学生不存在!!");
                    break;
                default:
                    System.out.println("退出");
                    bw.close();
                    br.close();
                    System.exit(0);
            }
        }
    }
}

运行效果图:

存储学生信息的tex文档

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值