java实现简易学生管理系统

最近在学java,看的黑马程序员的视频, 刚学到一个项目,学生管理系统,自己跟着用代码实现了一下。下面是我的代码。

javabean类
Student.java

public class Student {
    private String id;
    private String name;
    private int age;
    private String address;

    public Student() {
    }

    public Student(String id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

测试类
Main.java

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

	    while (true) {
            System.out.println("------------欢迎来到学生管理系统-----------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选择");

            Scanner sc = new Scanner(System.in);
            String input = sc.next();

            switch (input) {
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> modifyStudent(list);
                case "4" -> searchStudent(list);
                case "5" -> {
                    System.out.println("程序退出");
                    System.exit(0);
                }
                default -> System.out.println("输入错误,请重新输入");
            }

        }
    }

    public static void addStudent(ArrayList<Student> list) {
        System.out.println("添加学生");
        Scanner sc = new Scanner(System.in);
        Student student = new Student();

        String id;
        while (true) {
            System.out.println("请输入学生id");
            id = sc.next();
            if (!isOnlyId(list, id)) {
                System.out.println("id重复,请重新输入");
            } else {
                student.setId(id);
                break;
            }
        }

        System.out.println("请输入学生姓名");
        String name = sc.next();
        student.setName(name);

        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        student.setAge(age);

        System.out.println("请输入学生家庭住址");
        String address = sc.next();
        student.setAddress(address);

        list.add(student);
        System.out.println("添加成功");
    }

    public static void deleteStudent(ArrayList<Student> list) {
        System.out.println("删除学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生id");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                list.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("id不存在,删除失败");
    }

    public static void modifyStudent(ArrayList<Student> list) {
        System.out.println("修改学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        String id = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                System.out.println("请输入学生姓名");
                String name = sc.next();
                list.get(i).setName(name);

                System.out.println("请输入学生年龄");
                int age = sc.nextInt();
                list.get(i).setAge(age);

                System.out.println("请输入学生家庭住址");
                String address = sc.next();
                list.get(i).setAddress(address);
                System.out.println("学生信息修改成功");
                return;
            }
        }
        System.out.println("id不存在,修改失败");
    }

    public static void searchStudent(ArrayList<Student> list) {
        System.out.println("查询学生");
        if (list.isEmpty()) {
            System.out.println("当前无学生信息,请添加后再查询");
            return;
        }
        System.out.println("id\t" + "姓名\t" + "年龄\t" + "家庭住址");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId() + '\t' + list.get(i).getName() + '\t' + list.get(i).getAge() + '\t' + list.get(i).getAddress());
        }
    }

    public static boolean isOnlyId(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return false;
            }
        }
        return true;
    }
}




效果图
在这里插入图片描述
在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值