简单的学生管理系统Java实现

**

简单的学生管理系统JAVA实现

通过集合+IO流实现本地存储和增删改查功能
主要是逻辑,联系练习集合运用和IO操作,细节像年龄范围、性别这些请忽略,可自己优化。
**

控制台操作:
在这里插入图片描述
本地文件存储的学生信息
在这里插入图片描述
JAVA代码实现:

import java.io.*;
import java.util.*;

/**
 * 学生管理系统
 * 对学生信息增删改查
 * 1、增加学生 2、删除学生 3、修改学生4、查询所有、0、退出系统
 */
public class StudentManager {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的操作:1、增加学生 2、删除学生 3、修改学生 4、查询所有、0、退出系统");
        while (scanner.hasNext()) {
            String next = scanner.next();

            if (next.equals("1")) {
                //增加学生
                //从控制台输入学生信息
                //学生学号
                System.out.println("请输入学号:");
                String no = scanner.next();
                //学生姓名
                System.out.println("请输入姓名:");
                String name = scanner.next();
                //学生性别
                System.out.println("请输入性别:");
                String sex = scanner.next();
                //学生年龄
                System.out.println("请输入年龄:");
                int age = scanner.nextInt();
                Student s = new Student(no, name, sex, age);
                addStudent(s);
                System.out.println("请输入您的操作:1、增加学生 2、删除学生 3、修改学生 4、查询所有、0、退出系统");
            } else if (next.equals("2")) {
                //输入学号根据学号删除
                //查询所有的学生放到list集合中,然后从list集合中删除学生
                //然后把文件内容清空,将新的list重新写入到文件中
                //根据学号找到学生
                foeachlist(findAll());
                System.out.println("请输入要删除的学号:");
                String stuNo = scanner.next();
                if (delStudent(findAll(), stuNo)) {
                    System.out.println("删除成功");
                    System.out.println("是否继续操作:1、增加学生 2、删除学生 3、修改学生 4、查询所有、0、退出系统");
                } else {
                    System.out.println("删除失败404");
                }


            } else if (next.equals("3")) {
                foeachlist(findAll());
                System.out.println("请输入您要修改学生信息的学号:");
                String stuNo = scanner.next();
                if (modify(findAll(), stuNo)) {
                    System.out.println("修改成功");
                    System.out.println("是否继续操作:1、增加学生 2、删除学生 3、修改学生 4、查询所有、0、退出系统");
                } else {
                    System.out.println("修改失败404");
                }

            } else if (next.equals("4")) {
                foeachlist(findAll());

            } else if (next.equals("0")) {
                System.exit(0);
            } else {
                System.out.println("输入有误,请重新输入您的操作:1、增加学生 2、删除学生 3、修改学生 4、查询所有、0、退出系统");
            }

        }
    }

    /**
     *添加学生
     * @param s
     */

    private static void addStudent(Student s) {
        File file = new File("D:\\学生信息库.txt");
        FileWriter write = null;
        try {
            write = new FileWriter(file, true);
            if (file.length() == 0) {
                //文件为空写一个表头
                write.write("学生学号     学生姓名     学生性别     学生年龄");
                write.write("\n");
            }
            write.write(s.getNo() + "     " + s.getName() + "     " + s.getSex() + "     " + s.getAge());
            write.write("\n");
            write.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (write != null) {
                try {
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
   /**
     *查找学生
     * @param
     */
    private static List<Student> findAll() {
        List<Student> list = new ArrayList<>();
        FileReader fr = null;
        BufferedReader reader = null;
        try {
            fr = new FileReader("D:\\学生信息库.txt");
            reader = new BufferedReader(fr);
            reader.readLine();
            String line = null;
            while ((line = reader.readLine()) != null) {
                line = line.trim();//去掉头尾空格
                String[] splits = line.split("     ");
                String no = splits[0].trim();
                String name = splits[1].trim();
                String sex = splits[2].trim();
                int age = Integer.parseInt(splits[3].trim());
                Student s = new Student(no, name, sex, age);
                list.add(s);
                //  System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    /**
     * 根据学号删除学生
     *
     * @param list1
     * @param stuNo1
     * @return
     */
    private static boolean delStudent(List<Student> list1, String stuNo1) {
        Iterator<Student> iterator = list1.iterator();
        while (iterator.hasNext()) {
            if ((iterator.next().getNo()).equals(stuNo1)) {
                iterator.remove();
            }
        }

        File file = new File("D:\\学生信息库.txt");
        if (file.exists()) {
            file.delete();
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            if (file.length() == 0) {
                //文件为空写一个表头
                writer.write("学生学号     学生姓名     学生性别     学生年龄");
                writer.write("\n");
            }
            for (Student s : list1
            ) {
                writer.write(s.getNo() + "     " + s.getName() + "     " + s.getSex() + "     " + s.getAge());
                writer.write("\n");
            }

            writer.flush();
            return true;

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 根据学号修改学生信息
     * @param list4
     * @param stuNo4
     * @return
     */
    public static boolean modify(List<Student> list4, String stuNo4) {

        Scanner sc = new Scanner(System.in);
        HashMap<String, Student> map = new HashMap<String, Student>();
        for (Student students : list4
        ) {
            map.put(students.getNo(), students);
        }

        Set<Map.Entry<String, Student>> entries = map.entrySet();
        for (Map.Entry<String, Student> s : entries
        ) {
            if (s.getKey().equals(stuNo4)) {
                System.out.println("请输入学生新的信息:");
                System.out.println("请姓名:");
                String name = sc.next();
                //学生性别
                System.out.println("性别:");
                String sex = sc.next();
                //学生年龄
                System.out.println("年龄:");
                int age = sc.nextInt();
                Student s2 = new Student(stuNo4, name, sex, age);
                s.setValue(s2);
            }
        }
        File file = new File("D:\\学生信息库.txt");
        FileWriter writer = null;
        if (file.exists()) {
            file.delete();
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            writer = new FileWriter(file);
            if (file.length() == 0) {
                //文件为空写一个表头
                writer.write("学生学号     学生姓名     学生性别     学生年龄");
                writer.write("\n");
            }
            for (Map.Entry<String, Student> s : entries
            ) {
                writer.write(s.getValue().getNo() + "     " + s.getValue().getName() + "     " + s.getValue().getSex() + "     " + s.getValue().getAge());
                writer.write("\n");
            }

            writer.flush();

            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;

    }

    /**
     * 集合遍历输出
     *
     * @param list2
     */
    private static void foeachlist(List<Student> list2) {
        for (Student t : list2
        ) {
            System.out.println(t.getNo() + "     " + t.getName() + "     " + t.getSex() + "     " + t.getAge());
        }
    }


}
/**
 * 学生对象
 *     学生学号   学生姓名  学生性别   学生年龄
 */
public class Student {
    //学生学号
    private String no;
    //学生姓名
    private  String name;
    //学生性别
    private String sex;
    //学生年龄
    private int age;

    public Student() {
    }

    public Student(String no, String name, String sex, int age) {
        this.no = no;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值