java 简单的学生信息管理系统(控制台版)

程序简介

此程序主要是为了演示管理学生信息,用于展示信息,为初级教学,这个教学呢,主要是让我门对程序开发有一些逻辑上的认识,对于初学者来讲可以着重去看看。

如遇到不解可咨询,Email [email protected]

程序开始

1.定义一个学生实体类

package Student;

/**
 * @Author xgp
 * @Date 2022/7/22 10:04
 * @PackageName:Student
 * @ClassName: StudentEntity
 * @Description: TODO
 * @Version 1.0
 */
public class StudentEntity {

    private String id;
    private String name;
    private String age;
    private String phone;

    @Override
    public String toString() {
        return "StudentEntity{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    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 String getAge() {
        return age;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

2.定义一个装载学生信息容器,这里我们用的是LIST

    static List list = new ArrayList();

3.定义学生信息添加方法,同时在添加方法中,增加对相同学号信息去重。

 public static void add() {
        System.out.println("------------添加信息------------");
        StudentEntity studentEntity = new StudentEntity();
        System.out.print("学号:");
        while (true) {
            Scanner scanner = new Scanner(System.in);
            String id = scanner.next();
            long count = list.stream().filter(item -> item.getId().equals(id)).count();
            if (count > 0) {
                System.out.print("学号不能重复,请重新输入:");
            } else {
                studentEntity.setId(id);
                break;
            }
        }
        System.out.print("姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        System.out.print("年龄:");

        while(true){
            scanner = new Scanner(System.in);
            String age = scanner.next();
            try {
                int i = Integer.parseInt(age);
                studentEntity.setAge(age);
                break;
            }catch (Exception e){
                System.out.print("年龄非数字,请重新输入:");
            }
        }
        System.out.print("手机:");
        scanner = new Scanner(System.in);
        String phone = scanner.next();
        studentEntity.setName(name);
        studentEntity.setPhone(phone);
        System.out.println(studentEntity);
        list.add(studentEntity);
        System.out.println("------------添加成功------------");
    }

4.定义修改学生信息方法,此方法用户输入学号,通过对学号的查询,获取对应的学生信息,保证学生信息存在,不存在的情况下,要求重新输入学号进行查找,同时添加可退出功能,当输入-1时,退出编辑界面

 public static void edit() {
        System.out.println("------------编辑信息------------");
        System.out.print("输入要编辑的学号(-1退出):");
        boolean flag = true;
        while (flag) {
            Scanner scanner = new Scanner(System.in);
            String editId = scanner.next();
            if ("-1".equals(editId)) {
                System.out.println("------------操作取消------------");
                break;
            }
            StudentEntity studentEntity = new StudentEntity();
            int index = -1;
            for (int i = 0; i < list.size(); i++) {
                StudentEntity item = list.get(i);
                studentEntity.setId(item.getId());
                studentEntity.setName(item.getName());
                studentEntity.setAge(item.getAge());
                studentEntity.setPhone(item.getPhone());
                if (editId.equals(studentEntity.getId())) {
                    index = i;
                    break;
                }
            }
            if (index == -1) {
                System.out.println("输入学号不存在,请重新输入(-1退出):");
            } else {
                System.out.print("姓名(" + studentEntity.getName() + "):");
                scanner = new Scanner(System.in);
                String name = scanner.nextLine();
                if (name.length() > 0) {
                    studentEntity.setName(name);
                }
                System.out.print("年龄(" + studentEntity.getAge() + "):");
                scanner = new Scanner(System.in);
                String age = scanner.nextLine();
                if (age.length() > 0) {
                    studentEntity.setAge(age);
                }
                System.out.print("手机号(" + studentEntity.getPhone() + "):");
                scanner = new Scanner(System.in);
                String phone = scanner.nextLine();
                if (phone.length() > 0) {
                    studentEntity.setPhone(phone);
                }
                System.out.print("确认是否编辑(Y/N):");
                while (true) {
                    scanner = new Scanner(System.in);
                    String status = scanner.next();
                    if ("N".equals(status)) {
                        System.out.println("------------操作取消------------");
                        flag = false;
                        break;
                    }else if("Y".equals(status)) {
                        System.out.println("------------修改成功------------");
                        list.set(index,studentEntity);
                        flag = false;
                        break;
                    }else {
                        System.out.print("输入错误,请重新输入,确认是否编辑(Y/N):");
                    }
                }
            }
        }

    }

5. 学生信息删除功能,获取用户输入学号信息,同时监听是否输入为-1,如输入-1则退出当前操作,否则验证当前输入学号是否存在,存在则删除,不存在则通知用户重新输入或者退出,同时添加确认条件,当输入Y是,确认删除,输入N时,退出删除。

 public static void del() {
        System.out.println("------------删除信息------------");
        System.out.print("输入要删除的学号(-1退出):");
        boolean flag = true;
        while (flag) {
            Scanner scanner = new Scanner(System.in);
            String delId = scanner.next();
            if ("-1".equals(delId)) {
                System.out.println("------------操作取消------------");
                break;
            }
            int index = -1;
            for (int i = 0; i < list.size(); i++) {
               StudentEntity item = list.get(i);
                if(delId.equals(item.getId())){
                    index = i;
                    break;
                }
            }
            if(index == -1){
                System.out.print("输入学号不存在,请重新输入要删除的学号(-1退出):");
            }else {
                System.out.print("确认是否编辑(Y/N):");
                while (true) {
                    scanner = new Scanner(System.in);
                    String status = scanner.next();
                    if ("N".equals(status)) {
                        System.out.println("------------操作取消------------");
                        flag = false;
                        break;
                    }else if("Y".equals(status)) {
                        System.out.println("------------删除成功------------");
                        list.remove(index);
                        flag = false;
                        break;
                    }else {
                        System.out.print("输入错误,请重新输入,确认是否编辑(Y/N):");
                    }
                }
            }
        }
    }

6.学生信息查询方法,遍历数据进行呈现,保证数据呈现格式化 ,让显示有规律

 public static void list() {
        System.out.println("------------学生列表------------");
        System.out.print("序号\t\t\t学号\t\t\t姓名\t\t\t年龄\t\t\t手机");
        System.out.println();
        if (list.size() == 0) {
            System.out.println("------------暂时未有学生信息,请添加------------");
        }
        for (int i = 0; i < list.size(); i++) {
            StudentEntity item = list.get(i);
            System.out.printf("%-12s", (i + 1));
            System.out.printf("%-12s", item.getId());
            System.out.printf("%-12s", item.getName());
            System.out.printf("%-12s", item.getAge());
            System.out.printf("%-12s", item.getPhone());
            System.out.println();
        }
    }

7.学生年龄分布,获取到各个年龄段的学生个数。

public static void ageDistributed() {
        System.out.println("------------学生年龄分布------------");
        System.out.print("序号\t\t0-20\t20-40\t40-60\t60以上");
        System.out.println();
        if (list.size() == 0) {
            System.out.println("------------暂时未有学生信息,请添加------------");
        }else {
          long one = list.stream().filter(item ->
                          (Integer.parseInt(item.getAge()) >= 0 &&
                                  Integer.parseInt(item.getAge()) < 20)).count();
            long two = list.stream().filter(item ->
                    (Integer.parseInt(item.getAge()) >= 20 &&
                            Integer.parseInt(item.getAge()) < 40)).count();
            long three = list.stream().filter(item ->
                    (Integer.parseInt(item.getAge()) >= 40 &&
                            Integer.parseInt(item.getAge()) < 60)).count();
            long four = list.stream().filter(item ->
                    (Integer.parseInt(item.getAge()) >= 60 )).count();
            System.out.printf("%-8s", ( 1));
            System.out.printf("%-8s", one);
            System.out.printf("%-8s", two);
            System.out.printf("%-8s", three);
            System.out.printf("%-8s", four);
            System.out.println();
        }
    }

8.通过学生姓名查询学生信息

 public static void listByName() {
        System.out.println("------------学生查询------------");
        System.out.print("输入要查询学生的姓名(-1退出):");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        if("-1".equals(name)){
            System.out.println("------------操作取消------------");
            return;
        }
        List entities = list.stream().filter(item -> item.getName().equals(name)).collect(Collectors.toList());
        getByName(name, entities);
    }

9.通过学生学号查询学生信息

  public static void listById() {
        System.out.println("------------学生查询------------");
        System.out.print("输入要查询学生的姓名(-1退出):");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();
        if("-1".equals(id)){
            System.out.println("------------操作取消------------");
            return;
        }
        List entities = list.stream().filter(item -> item.getId().equals(id)).collect(Collectors.toList());
        getByName(id, entities);
    }

10.主方法,进入方法

  public static void main(String[] args) {
        Boolean flag = true;
        while (flag) {
            System.out.println("------------学生信息管理系统------------");
            System.out.println();
            System.out.println("\t\t\t1.添加学生");
            System.out.println("\t\t\t2.修改学生");
            System.out.println("\t\t\t3.删除学生");
            System.out.println("\t\t\t4.学生列表");
            System.out.println("\t\t\t5.退出");
            System.out.println("\t\t\t6.学生年龄分布");
            System.out.println("\t\t\t7.学生姓名查找");
            System.out.println("\t\t\t8.学生学号查找");
            System.out.print("\t\t请选择(1-8):");
            Scanner scanner = new Scanner(System.in);
            String status = scanner.next();
            switch (status) {
                case "1":
                    add();
                    break;
                case "2":
                    edit();
                    break;
                case "3":
                    del();
                    break;
                case "4":
                    list();
                    break;
                case "6":
                    ageDistributed();
                    break;
                case "7":
                    listByName();
                    break;
                case "8":
                    listById();
                    break;
                case "5":
                    System.out.println("------------系统已退出------------");
                    flag = false;
                    break;
                default:
                    System.out.println("输入参数错误,请输入1 - 5");
            }
        }
    }

你可能感兴趣的:(java,java,servlet,开发语言)