简易学生管理系统

要求:
1:实现学生的添加、修改、查询以及删除的功能
2:将学生信息存储在文件当中
3:查询的结果按照学生的年龄进行排序
基本思路:
1:创建的第一个类是学生基本属性类,包含学生的基本信息(学号、姓名、性别、年龄)
2:对学生进行管理的类,对学生进行增删改查,排序。
用到的主要知识:
1:IO流
2:比较器(Comparator)
3:对象序列化(serializable)

//学生信息类
public class Student implements Serializable, Comparator<Student>{
    private String num;
    private String name;
    private String sex;
    private int age;

    public Student() {
    }

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

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    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 [num=" + num + ", name=" + name + ", sex=" + sex
                + ", age=" + age + "]";
    }

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }

}

//管理学生的类,主要是对学生进行增删改查,排序以及将装学生的集
//合序列化和反序列化

public class MangerStudent {
    private ArrayList<Student> list = new ArrayList<Student>();

    public ArrayList<Student> getList() {
        return list;
    }

    public void setList(ArrayList<Student> list) {
        this.list = list;
    }

    Student student = null;
    Scanner scanner = new Scanner(System.in);

    // 添加学生
    public void add() {
        System.out.println("请输入学生学号,姓名,性别,年龄格式是(学号/姓名/性别/年龄);");
        String string = scanner.next();
        String[] strings = string.split("/");
        Integer integer = new Integer(strings[3]);
        student = new Student(strings[0], strings[1], strings[2], integer);
        list.add(student);
        // 调用对象序列化方法
        write1();
    }

    // 查询全体学生
    public void findAll() {
        // 调用反序列化方法
        list = read1();
        // 将学生按照年龄进行排序
        Collections.sort(list, new Student());
        for (Student student : list) {
            System.out.println(student);
        }
    }

    // 删除学生(根据学号进行删除)
    public void delete() {
        // 调用反序列化方法
        list = read1();
        String num = null;
        System.out.println("请输入需要删除的学生的学号:");
        num = scanner.next();
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getNum().equals(num)) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            list.remove(index);
        } else {
            System.out.println("需要删除的学生不存在");
        }
        // 调用序列化方法
        write1();
    }

    // 修改学生信息
    public void modify() {
        // 调用反序列化方法
        list = read1();
        int index = findOne();
        String num = null;
        String name = null;
        String sex = null;
        int age = 0;
        if (index == -1) {
            System.out.println("需要修改的学生不存在");
            return;
        }
        System.out.println("请输入需要修改的选项1、学号 2、姓名 3、性别 4、年龄");
        int choice = 0;
        choice = scanner.nextInt();
        switch (choice) {
        case 1:
            System.out.println("请输入新的学号:");
            num = scanner.next();
            list.get(index).setNum(num);
            break;
        case 2:
            System.out.println("请输入新的姓名:");
            name = scanner.next();
            list.get(index).setName(name);
            break;
        case 3:
            System.out.println("请输入新的性别:");
            sex = scanner.next();
            list.get(index).setSex(sex);
            break;
        case 4:
            System.out.println("请输入新的年龄:");
            age = scanner.nextInt();
            list.get(index).setAge(age);
            break;
        }
        // 调用序列化方法
        write1();
    }

    // 根据学号查找学生
    public int findOne() {
        // 调用反序列化方法
        list = read1();
        String num = null;
        System.out.println("请输入学生学号:");
        num = scanner.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getNum().equals(num)) {
                return i;
            }
        }
        return -1;
    }

    File file = new File("src/record.txt");

    // 完成对象的序列化
    public void write1() {
        try {
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(list);
            // //因为是字节输入流。因此可以不用使用flush()来刷新
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 反对象序列化
    public ArrayList<Student> read1() {
        // 反对象序列化
        try {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Object obj = ois.readObject();
            list = (ArrayList) obj;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

}

//执行类

public class TestMain {
    MangerStudent ms = new MangerStudent();
    Scanner scanner = new Scanner(System.in);
    public void start(){
        int choice = 0;
        System.out.println("1、添加学生\n"
                + "2、修改学生信息\n"
                + "3、查询全体学生\n"
                + "4、删除学生\n"
                + "5、退出!");
        System.out.println("请输入选项:");
        choice = scanner.nextInt();
        switch (choice) {
        case 1:
            ms.add();
            start();
            break;
        case 2:
            ms.modify();
            start();
            break;
        case 3:
            ms.findAll();
            start();
            break;
        case 4:
            ms.delete();
            start();
            break;
        case 5:
            return;
        }
    }

    public static void main(String[] args) {
        new TestMain().start();
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值