Java—student information management system (simple, detailed)

Article directory

  • 1. Main interface display
  • 2. Students
  • 3. System Function Method
    • 3.1 main() method
    • 3.2 Add student information
    • 3.3 Delete student information
    • 3.4 Modify student information
    • 3.5 View all student information
  • 4. Complete code
    • 4.1 Student .Java
    • 4.2 StudentManger.Java


  Preface: This case uses the ArrayList collection in the Java language to store data during implementation. I divided the entire project into two parts: Student.Java and StudentManger.Java, and implemented a simple student information management system. The article first explains the code in parts, and finally attaches the complete project code.

Insert image description here


1. Main interface display

Insert image description here

2. Students

 We first create the Student class and constructor:

  • Member variables: student number (sid), name (name), age (age), place of residence (address).
  • Construction method: no-parameter construction and construction with four parameters.
  • Member methods: Each member variable corresponds to a get/set method.
public class Student {
    
    
    //学号
    private String sid;
    //姓名
    private String name;
    //年龄
    private String age;
    //地址
    private String address;

    public String getSid() {
    
    
        return sid;
    }

    public void setSid(String sid) {
    
    
        this.sid = sid;
    }

    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 getAddress() {
    
    
        return address;
    }

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

3. System Function Method

3.1 main() method

 Idea:

  • Use output statements to complete writing the main interface.
  • Use Scanner to enter data using the keyboard.
  • Use switch statements to complete the selection of operations.
  • Use Loop Finish to return to the main interface again.
    public static void main(String[] args){
    
    
        //创建集合对象,用于存储学生数据
        ArrayList<Student> array=new ArrayList<Student>();

        //用循环完成再次回到主界面
        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实现键盘录入数据
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();

            //用switch语句完成操作选择
            switch (line) {
    
    
                case "1":
                    addStudent(array);
                    break;
                case "2":
                    deleteStudent(array);
                    break;
                case "3":
                    updateStudent(array);
                    break;
                case "4":
                    finalAllStudent(array);
                    break;
                case "5":
                    System.out.println("已退出,谢谢使用");
                    System.exit(0); //退出虚拟机
            }
        }

    }

3.2 Add student information

 Idea:

  • Display prompt information to prompt what information to enter.
  • Use the keyboard to enter the data required by the student object.
  • Create a student object and assign the keyboard input data to the member variables of the student object.
  • Add the student object to the collection (save).
  • Give a successful addition prompt.

Notice: The best name in student information is 3 characters, and the best age is 2 digits. Such as: Zhao Sisi 20.

    public static void addStudent(ArrayList<Student> array){
    
    
        Scanner sc=new Scanner(System.in);
        String sid;
        while(true) {
    
    
            System.out.println("请输入学生的学号:");
            sid = sc.nextLine();

            boolean flag = isUsed(array, sid);
            if (flag) {
    
    
                System.out.println("你输入的学号已被占用,请重新输入:");
            }
            else{
    
    
                break;
            }
        }
        System.out.println("请输入学生的姓名:");
        String name=sc.nextLine();
        System.out.println("请输入学生的年龄:");
        String age=sc.nextLine();
        System.out.println("请输入学生的居住地:");
        String address=sc.nextLine();

        //创建学生对象,将键盘录入的数据赋值给学生对象的成员变量
        Student s=new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        //将学生对象添加到集合中
        array.add(s);

        //给出添加成功提示
        System.out.println("添加学生成功");
    }

3.3 Delete student information

 Idea:

  • Display prompt message.
  • Use the keyboard to enter the student number to be deleted.
  • Traverse the collection and delete the corresponding student object from the collection.
  • Gives a prompt of successful deletion.
    public static void deleteStudent(ArrayList<Student> array){
    
    
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号:");
        String sid=sc.nextLine();

        //遍历集合将对应的学生对象从集合中删除
        for(int i=0; i< array.size(); i++){
    
    
            Student s=array.get(i);
            if(s.getSid().equals(sid)){
    
    
                array.remove(i);
                break;
            }
        }
        System.out.println("删除该学生信息成功");
    }

3.4 Modify student information

 Idea:

  • Display prompt message.
  • Use the keyboard to enter the student number to be modified.
  • Use the keyboard to enter the student information to be modified.
  • Traverse the collection to modify the corresponding student information.
  • Give a successful modification prompt.
    public static void updateStudent(ArrayList<Student> array){
    
    
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要修改的学生的学号:");
        String sid=sc.nextLine();

        //键盘录入要修改的学生信息
        System.out.println("请输入学生的新姓名:");
        String name=sc.nextLine();
        System.out.println("请输入学生的新年龄:");
        String age=sc.nextLine();
        System.out.println("请输入学生的新居住地:");
        String address=sc.nextLine();

        //创建学生对象
        Student s=new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        //遍历集合修改对应的学生信息
        for(int i=0; i<array.size(); i++){
    
    
            Student student=array.get(i);
            if(student.getSid().equals(sid)){
    
    
                array.set(i,s);  //把修改后的student s赋值给i的位置
                break;
            }
        }

        //给出修改成功提示
        System.out.println("修改学生信息成功");
    }
}

3.5 View all student information

 Idea:

  • Display header information.
  • Take out the data in the collection and display the student information in the corresponding format, and add "years" to the age display.
    public static void finalAllStudent(ArrayList<Student> array){
    
    
        //判断集合是否有数据,如果没有显示提示信息
        if(array.size()==0){
    
    
            System.out.println("无信息,请先添加信息再查询");
            //为了让程序不再往下执行
            return;
        }

        //显示表头信息
        System.out.println("学号\t\t\t姓名\t\t\t年龄\t\t居住地");
        for(int i=0; i<array.size(); i++){
    
    
            Student s=array.get(i);
            System.out.println(s.getSid()+"\t\t"+s.getName()+"\t\t"+s.getAge()+"岁\t"+s.getAddress());
        }
    }

4. Complete code

4.1 Student .Java

public class Student {
    
    
    //学号
    private String sid;
    //姓名
    private String name;
    //年龄
    private String age;
    //地址
    private String address;

    public String getSid() {
    
    
        return sid;
    }

    public void setSid(String sid) {
    
    
        this.sid = sid;
    }

    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 getAddress() {
    
    
        return address;
    }

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

4.2 StudentManger.Java

package mypackage;

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

public class StudentManger {
    
    
    public static void main(String[] args){
    
    
        //创建集合对象,用于存储学生数据
        ArrayList<Student> array=new ArrayList<Student>();

        //用循环完成再次回到主界面
        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实现键盘录入数据
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();

            //用switch语句完成操作选择
            switch (line) {
    
    
                case "1":
                    addStudent(array);
                    break;
                case "2":
                    deleteStudent(array);
                    break;
                case "3":
                    updateStudent(array);
                    break;
                case "4":
                    finalAllStudent(array);
                    break;
                case "5":
                    System.out.println("已退出,谢谢使用");
                    System.exit(0); //退出虚拟机
            }
        }

    }

    //定义添加学生信息的方法
    public static void addStudent(ArrayList<Student> array){
    
    
        Scanner sc=new Scanner(System.in);
        String sid;
        while(true) {
    
    
            System.out.println("请输入学生的学号:");
            sid = sc.nextLine();

            boolean flag = isUsed(array, sid);
            if (flag) {
    
    
                System.out.println("你输入的学号已被占用,请重新输入:");
            }
            else{
    
    
                break;
            }
        }
        System.out.println("请输入学生的姓名:");
        String name=sc.nextLine();
        System.out.println("请输入学生的年龄:");
        String age=sc.nextLine();
        System.out.println("请输入学生的居住地:");
        String address=sc.nextLine();

        //创建学生对象,将键盘录入的数据赋值给学生对象的成员变量
        Student s=new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        //将学生对象添加到集合中
        array.add(s);

        //给出添加成功提示
        System.out.println("添加学生成功");
    }

    //定义判断学号是否被占用方法
    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(s.getSid().equals(sid)){
    
    
                flag=true;
                break;
            }
        }
        return  flag;
    }

    //定义查看所有学生信息的方法
    public static void finalAllStudent(ArrayList<Student> array){
    
    
        //判断集合是否有数据,如果没有显示提示信息
        if(array.size()==0){
    
    
            System.out.println("无信息,请先添加信息再查询");
            //为了让程序不再往下执行
            return;
        }

        //显示表头信息
        System.out.println("学号\t\t\t姓名\t\t\t年龄\t\t居住地");
        for(int i=0; i<array.size(); i++){
    
    
            Student s=array.get(i);
            System.out.println(s.getSid()+"\t\t"+s.getName()+"\t\t"+s.getAge()+"岁\t"+s.getAddress());
        }
    }

    //定义删除学生信息的方法
    public static void deleteStudent(ArrayList<Student> array){
    
    
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号:");
        String sid=sc.nextLine();

        //遍历集合将对应的学生对象从集合中删除
        for(int i=0; i< array.size(); i++){
    
    
            Student s=array.get(i);
            if(s.getSid().equals(sid)){
    
    
                array.remove(i);
                break;
            }
        }
        System.out.println("删除该学生信息成功");
    }

    //定义修改学生信息的方法
    public static void updateStudent(ArrayList<Student> array){
    
    
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你要修改的学生的学号:");
        String sid=sc.nextLine();

        //键盘录入要修改的学生信息
        System.out.println("请输入学生的新姓名:");
        String name=sc.nextLine();
        System.out.println("请输入学生的新年龄:");
        String age=sc.nextLine();
        System.out.println("请输入学生的新居住地:");
        String address=sc.nextLine();

        //创建学生对象
        Student s=new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        //遍历集合修改对应的学生信息
        for(int i=0; i<array.size(); i++){
    
    
            Student student=array.get(i);
            if(student.getSid().equals(sid)){
    
    
                array.set(i,s);  //把修改后的student s赋值给i的位置
                break;
            }
        }

        //给出修改成功提示
        System.out.println("修改学生信息成功");
    }
}

Guess you like

Origin blog.csdn.net/m0_62881487/article/details/132455144