学生信息管理系统

Student.java

package com.ly;

public class Student {
    private String id;
    
    private String name;
    
    private String age;
    
    private String 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 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;
    }
    
    public Student() {
        
    }

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

}

StudentManager.java

package com.ly;

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

public class StudentManager {
    public static void main(String[] args) {
        addMainPage();
        return;
    }
    
    public static void addMainPage() {
        ArrayList<Student> arr = 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 sc = new Scanner(System.in);
            String inputStr = sc.nextLine();
            switch (inputStr) {
            case "1": // 1.查看所有学生信息
                getAllStudent(arr);
                break;
            case "2": // 2.添加学生
                addStudent(arr);
                break;
            case "3": // 3.删除学生
                removeStudent(arr);
                break;
            case "4": // 4.修改学生信息
                modifyStudent(arr);
                break;
            case "5": // 5.退出
            default:
                System.out.println("谢谢使用!");
                System.exit(0);
                break;
            }
        }
    }
    
    public static void getAllStudent(ArrayList<Student> arr) {
        if (0 == arr.size()) {
            System.out.println("没有学生信息,请返回主界面重新选择");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t住址");
        for (int i = 0; i < arr.size(); i++) {
            Student stu = arr.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
        }
        return;
    }
    
    public static void addStudent(ArrayList<Student> arr) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要添加的学生学号");
        String strId = sc.nextLine();
        
        if (0 != arr.size()) {
            while (true) {
                int i = 0;
                for (; i < arr.size(); i++) {
                    Student stu = arr.get(i);
                    if (strId.equals(stu.getId())) {
                        System.out.println("该学号已被占用,请重新输入需要添加的学生学号");
                        strId = sc.nextLine();
                        break;
                    }
                }
                if (i == arr.size()) {
                    break;
                }
            }
        }
        
        System.out.println("请输入学生姓名"); String strName = sc.nextLine();
        System.out.println("请输入学生年龄"); String strAge = sc.nextLine();
        System.out.println("请输入学生住址"); String strAddress = sc.nextLine();
        //sc.close();
        Student newStu = new Student(strId, strName, strAge, strAddress);
        arr.add(newStu);
        System.out.println("添加学生成功");
        return;
    }
    
    public static void removeStudent(ArrayList<Student> arr) {
        if (0 == arr.size()) {
            System.out.println("学生信息管理系统中没有学生信息,请重新输入");
            return;
        }
        
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要删除的学生学号");
        String strId = sc.nextLine();

        int i = 0;
        while (true) {
            i = 0;
            for (; i < arr.size(); i++) {
                if (true == strId.equals(arr.get(i).getId())) {
                    break;
                }
            }
            if (i == arr.size()) {
                System.out.println("未查找到该学生学号,请重新输入需要删除的学生学号");
                strId = sc.nextLine();
            } else {
                break;
            }
        }
        //sc.close();
        arr.remove(i);
        System.out.println("删除学生成功");
        return;
    }
    
    public static void modifyStudent(ArrayList<Student> arr) {
        if (0 == arr.size()) {
            System.out.println("学生信息系统中没有学生信息,请重新选择");
            return;
        }
        
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入需要修改的学生学号");
        String strId = sc.nextLine();
        int i = 0;
        while (true) {
            i = 0;
            for (; i < arr.size(); i++) {
                if (true == strId.equals(arr.get(i).getId())) {
                    break;
                }
            }
            if (i == arr.size()) {
                System.out.println("未查找到该学生学号,请重新输入需要修改的学生学号");
                strId = sc.nextLine();
            } else {
                break;
            }
        }
        
        System.out.println("请输入新学生学号"); String newId = sc.nextLine();
        System.out.println("请输入新学生姓名"); String newName = sc.nextLine();
        System.out.println("请输入新学生年龄"); String newAge = sc.nextLine();
        System.out.println("请输入新学生住址"); String newAddress = sc.nextLine();
        //sc.close();
        Student stu = new Student(newId, newName, newAge, newAddress);
        arr.set(i, stu);
        System.out.println("修改学生成功");
        return;
    }
    
}

推荐阅读更多精彩内容