StudentManage学生管理系统

蓟和煦
2023-12-01

实现代码:

package com.hahaha;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentManage {
    public static void main(String[] args) {
        ArrayList<Student> array1 = 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);
            int input = sc.nextInt();
            switch (input) {
                case 1:
//                addStudent();
                    addStudent(array1);
                    break;
                case 2:
//                    System.out.println("2 删除学生");
                    deleteStudent(array1);
                    break;
                case 3:
//                    System.out.println("3 修改学生");
                    updateStudent(array1);
                    break;
                case 4:
//                    System.out.println("4 查看学生");
                    findAllStudent(array1);
                    break;
                case 5:
                    System.out.println("已退出,谢谢使用!");
//                    break;
                    System.exit(0);
                default:
                    System.out.println("输入有误,请重新输入!");
                    break;
            }
        }

    }

//***********************判断学号是否存在
public static boolean isUsed(ArrayList<Student> array, String sid) {
    boolean flag = false;
    for (int i = 0; i < array.size(); i++) {
        Student s = array.get(i);
        if (sid.equals(s.sid)) {
            flag = true;
            break;
        }
    }
    return flag;
}

//************************添加学生信息!
public static void addStudent(ArrayList<Student> array) {
    String sid = null;
    int age = 0;
    String name = null;

    //姓名
    Scanner sc = new Scanner(System.in);
    while (true) {
        System.out.println("请输入姓名:");
        String name1 = sc.nextLine();
        if (name1.length() > 2 && name1.length() < 5) {
            name = name1;
            break;
        } else {
            System.out.println("输入错误,请重新输入!");
        }

    }

//      学号
        while (true) {
            if (array.size() == 0) {
                System.out.println("请输入学号:");
                Scanner sc1 = new Scanner(System.in);
                String sid1 = sc1.nextLine();
                if (sid1.length() < 11) {
                    sid = sid1;
                    break;
                } else {
                    System.out.println("请输入小于十一位的数字!");
                }
            } else {
                System.out.println("请输入学号:");
                Scanner sc1 = new Scanner(System.in);
                String sid1 = sc1.nextLine();
                if (sid1.length() < 11 && isUsed(array, sid1) != true) {
                    sid = sid1;
                    break;
                } else {
                    System.out.println("请重新输入学号!(1.小于11位\t2.不得重复)");
                    continue;
                }


            }

        }
//      年龄
        while (true) {
            System.out.println("请输入年龄:");
            int age1 = sc.nextInt();
            if (age1 < 6 || age1 > 100) {
                System.out.println("请输入合法的年龄!");
            } else {
                age = age1;
                break;
            }
        }
//      为什么下面两句放在这里不能够输入??


        Student s = new Student();
        s.setAge(age);
        s.setName(name);
        s.setSid(sid);

        array.add(s);
        System.out.println("添加成功!");

    }

    //************************删除学生!
    public static void deleteStudent(ArrayList<Student> array) {
        if (array.size() == 0) {
            System.out.println("未查询到学生信息,请先添加学生信息!");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生学号:");
        String input = sc.nextLine();
        if (isUsed(array,input)!= true){
            System.out.println("您输入的学号不存在!");
            return;

        }
        int index = 0;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getSid().equals(input)) {
                index = i;
                break;
            }
        }
        array.remove(index);
        System.out.println("删除成功!");

    }

    //************************修改学生信息!
    public static void updateStudent(ArrayList<Student> array) {
        if (array.size() == 0) {
            System.out.println("未查询到学生信息,请先添加学生信息!");
            return;
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生学号:");
        String input = sc.nextLine();
        if (isUsed(array,input)!= true){
            System.out.println("您输入的学号不存在!");
            return;

        }
        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getSid().equals(input)) {
                index = i;
                System.out.println("1.姓名");
                System.out.println("2.年龄");
//                System.out.println("3.学号");
                System.out.println("3.全部修改");
                System.out.println("0.返回主页面");
                System.out.println("请选择你要修改的内容:");
                int s1 = sc.nextInt();
                if (s1 == 0) {
                    break;
                } else if (s1 == 1) {
                    System.out.println("请输入您要修改的姓名:");
                    Scanner scc = new Scanner(System.in);
                    String name = scc.nextLine();
                    s.setName(name);
                } else if (s1 == 2) {
                    System.out.println("请输入您要修改的年龄:");
                    int s2 = sc.nextInt();
                    s.setAge(s2);
                } else
                    if (s1 == 4) {
                        System.out.println("请输入学生姓名:");
                        Scanner scc = new Scanner(System.in);
                        String name = scc.nextLine();
//                    System.out.println("请输入学生学号:");
//                    String sid  =scc.nextLine();
                        System.out.println("请输入学生年龄:");
                        int age = sc.nextInt();
                    } else {
                        System.out.println("您输入的指令有误!将会返回主页面!");
                        break;
                    }
//                    if (s1 == 3) {
//                    System.out.println("请输入您要修改的学号:");
//                    Scanner scc = new Scanner(System.in);
//                    String s2 = scc.nextLine();
//                    s.setSid(s2);
                System.out.println("修改成功!");

                break;
            }
        }

    }

    //************************查询全部学生信息!
    public static void findAllStudent(ArrayList<Student> array) {
        if (array.size() == 0) {
            System.out.println("未查询到学生信息,请先添加学生信息再进行查看!");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("所有学生的信息如下:");
        System.out.println("姓名\t年龄\t学号");
        for (int i = 0; i < array.size(); i++) {
            Student s = new Student();
            s = array.get(i);
            s.show();
        }

    }
}

学生类

package com.hahaha;

public class Student {
    String name;
    int age;
    String sid;
    public Student(){

    }
    public Student(String name,int age,String sid){
        this.name = name;
        this.age = age;
        this.sid = sid;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public void setSid(String sid){
        this.sid = sid;
    }
    public String getSid(){
        return sid;
    }
    public int getAge(){
        return age;
    }
    public String getName(){
        return name;
    }
    public void show(){
        System.out.println(getName()+"\t"+getAge()+"\t"+getSid());
    }
}
 类似资料: