首页 > 其他分享 >学生管理系统

学生管理系统

时间:2022-09-27 21:00:53浏览次数:54  
标签:String 管理系统 list System 学生 public id out

学生管理系统

Student.java

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;
    }
}

StudentSystem.java

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

public class StudentSystem {
    public static void main(String[] args) {
        boolean flag1=true;

        ArrayList<Student> list= new ArrayList<>();

        txt();
        Scanner scanner=new Scanner(System.in);
        while (flag1){
            String choose=scanner.next();
            switch (choose){
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("已退出");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入错误");
            }

            while(true){
                System.out.print("是否继续?Y/N:");
                String yn=scanner.next();
                if (yn.equalsIgnoreCase("n")){
                    System.out.println("已退出");
                    System.exit(0);
                }else if (yn.equalsIgnoreCase("y")){
                    txt();
                    break;
                }else {
                    System.out.println("输入错误");
                }
            }


        }


        scanner.close();
    }

    public static void txt(){
        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.print("请输入您的选择:");
    }

    public static void addStudent(ArrayList<Student> list){
        Scanner scanner=new Scanner(System.in);
        Student s=new Student();
        while (true){
            System.out.print("请输入id:");
            String id=scanner.next();
            boolean flag2=contains(list,id);
            if (flag2){
                System.out.println("id已存在,请重录。");
            }else {
                s.setId(id);
                break;
            }
        }

        System.out.print("请输入姓名:");
        String name=scanner.next();
        s.setName(name);
        System.out.print("请输入年龄:");
        int age=scanner.nextInt();
        s.setAge(age);
        System.out.print("请输入地址:");
        String address=scanner.next();
        s.setAddress(address);

        list.add(s);
        System.out.println("添加成功");
    }

    public static void deleteStudent(ArrayList<Student> list){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入要删除的学生id:");
        String id=scanner.next();
        int index = get_index(list, id);
        if (index>=0){
            list.remove(index);
            System.out.println("id为"+id+"的学生信息删除成功");
        }else {
            System.out.println("id不存在,删除失败");
        }
    }

    public static void updateStudent(ArrayList<Student> list){
        Scanner scanner=new Scanner(System.in);
        System.out.print("请输入要修改的学生id:");
        String id=scanner.next();
        int index = get_index(list, id);
        if (index ==-1){
            System.out.println("id不存在,修改失败");
        }else {
            Student stu= list.get(index);
            System.out.print("请输入要修改的学生姓名:");
            String newName=scanner.next();
            stu.setName(newName);
            System.out.print("请输入要修改的学生年龄:");
            int newAge=scanner.nextInt();
            stu.setAge(newAge);
            System.out.print("请输入要修改的学生地址:");
            String newAddress=scanner.next();
            stu.setName(newAddress);
            System.out.println("修改成功");
        }
    }

    public static void queryStudent(ArrayList<Student> list){
        if (list.size()==0){
            System.out.println("当前无学生信息,请添加后再查询。");
            return;//结束方法
        }
        //打印表头信息
        System.out.println("id\t姓名\t年龄\t地址");
        for (int i = 0; i < list.size(); i++) {
           Student stu= list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }

    //判断id是否存在
    public static boolean contains(ArrayList<Student> list,String id){
//        for (int i = 0; i < list.size(); i++) {
//            Student stu=list.get(i);
//            if (stu.getId().equals(id)){
//                return true;
//            }
//        }
//        return false;
        return get_index(list,id)>=0;//提高代码复用性
    }


    //根据id获取索引
    public static int get_index(ArrayList<Student> list,String id){
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            //得到每一个学生对象
            Student stu=list.get(i);
            //集合中的学生id与查询的id比较
            if (stu.getId().equals(id)){
                //如果相同,返回索引
                return i;
            }
        }
        //如果不同,就说明查询的id不存在,返回-1
        return -1;
    }
}

标签:String,管理系统,list,System,学生,public,id,out
From: https://www.cnblogs.com/ben10044/p/16735964.html