学生管理系统(增、删、改、查) 刚学没多久,经供参考

​
/**
 * 学生管理系统(增、删、改、查)
 * @author 云清墨
 */
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;
    }

}

​

 主代码

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

/**
 * 学生管理系统(增、删、改、查)
 * @author 云清墨
 */

public class Student_management_system {
    public static void main(String[] args) {
        ArrayList list= new ArrayList<>();
        loop: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("6.退出学生管理系统");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();
            switch(choose){
                case 1 -> {System.out.println("----------添加学生信息----------");addStudent(list);}
                case 2 -> deleteStudent(list);
                case 3 -> UpdateStudent(list);
                case 4 -> queryStudent(list);
                case 5 -> printList(list);
                case 6 -> {System.out.println("----------已退出学生管理系统----------");break loop ;}
                default -> System.out.println("没有这个选项,输入错误");
            }
        }
    }
    public static void addStudent(ArrayList list){                 //添加学生信息
        Scanner sc = new Scanner(System.in);
        String id ;
        while (true) {
            System.out.println("请输入学生ID:");
            id = sc.next();
            boolean flag = contains(list, id);
            if (flag) {System.out.println("存在相同ID,请重试:");
            }else {break;}
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生家庭地址:");
        String address = sc.next();
        Student student = new Student(id,name,age,address);
        list.add(student);
        System.out.println("信息添加成功!" );
    }
    public static boolean contains(ArrayList list,String id){          //判断ID唯一
        for (Student student : list) {
            if (id.equals(student.getID())) {return true;}
        }
        return false;
    }
    public static int getIndex(ArrayList list,String id){                        //返回重复ID索引
        for (int i = 0; i < list.size(); i++) {
            if(id.equals(list.get(i).getID())) {return i;}
        }
        return -1;
    }
    public static  void deleteStudent(ArrayList list){                           //删除学生信息
        System.out.println("----------删除学生信息----------");
        Scanner sc = new Scanner(System.in);
        System.out.println("输入所需删除的学生信息的ID:");
        String id = sc.next();
        if(getIndex(list,id)>=0){
            list.remove(getIndex(list,id));
            System.out.println("ID为:"+id+"删除成功!");
        }else {System.out.println("ID不存在");}
    }
    public static void printList(ArrayList list){                                 //遍历学生信息
        System.out.println("----------遍历学生信息----------");
        System.out.println("ID"+'\t'+"姓名"+'\t'+"年龄"+'\t'+"家庭地址");
        for (Student s : list) {
            System.out.println(s.getID() + '\t' + s.getName() + '\t' + s.getAge() + '\t' + s.getAddress());
        }
    }
    public static void UpdateStudent(ArrayList list){                              //修改学生信息
        System.out.println("----------修改学生信息----------");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您所需要修改的学生信息的ID:");
        String id = sc.next();
        if(getIndex(list,id)==-1){System.out.println("所输入的ID:"+id+"不存在,请重试");return;
        }else{list.remove(getIndex(list,id));addStudent(list);}
        System.out.println("修改学生信息成功!");
    }
    public static void queryStudent(ArrayList list){
        System.out.println("----------查询学生信息----------");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您所需要查询的学生信息的ID:");
        String id = sc.next();
        if(getIndex(list,id)==-1){System.out.println("您所输入的ID"+id+"不存在,请重试");
        } else {System.out.println("ID"+'\t'+"姓名"+'\t'+"年龄"+'\t'+"家庭地址");
            System.out.println(list.get(getIndex(list,id)).getID() + '\t' + list.get(getIndex(list,id)).getID() +
                    '\t' + list.get(getIndex(list,id)).getID() + '\t' + list.get(getIndex(list,id)).getID());}
    }
}

可能有小bug

你可能感兴趣的:(代码,java)