利用Java集合类ArrayList制作简易学生管理系统

java.util.ArrayList是大小可变的数组的实现,存储在内的数据称为元素。此类提供一些方法来操作内部存储的元素。ArrayList中可不断添加元素,其大小也自动增长

 ManageDynamicArray.java

package cn.chenwei.student;

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

/*
@Author:陈小哥cw
@CreateTime:2019-03-05 15:20
@Description:利用ArrayList集合制作简易学生管理系统
*/
public class ManageDynamicArray {
    static ArrayList<Student> arr = new ArrayList<Student>();

    public static void main(String[] args) {
        menu();
    }

    //主方法
    public static void menu() {
        System.out.println("--------------学生选课管理系统-----------");
        System.out.println("1.查询全部学生信息\n2.根据学生编号查询\n3.添加学生\n4.修改学生信息\n5.删除学生\n6.退出");
        Scanner sc = new Scanner(System.in);
        switch (sc.nextInt()) {
            case 1:
                findAll();
                break;
            case 2:
                findId();
                break;
            case 3:
                add();
                break;
            case 4:
                change();
                break;
            case 5:
                delete();
                break;
            case 6:
                System.out.println("欢迎下次使用本系统!");
                break;
            default:
                System.out.println("输入格式不规范!");
                menu();
                break;
        }
    }

    //查询全部学生信息
    public static void findAll() {
        if (arr.size() != 0) {
            for (int i = 0; i < arr.size(); i++) {
                System.out.println("学号为:" + arr.get(i).getId() + "的学生信息为:\n--------------------\n"
                        + "姓名:" + arr.get(i).getName() + "\n年龄为:" + arr.get(i).getAge() + "\n性别为:"
                        + arr.get(i).getSex() + "\n--------------------\n\n");
            }
        } else {
            System.out.println("本系统暂时无学生信息!");

        }
        menu();
    }

    //根据学生id查询学生信息
    public static void findId() {
        int i;
        if (arr.size() != 0) {
            int j = 0;
            System.out.println("请输入学生id!");
            Scanner sc = new Scanner(System.in);
            int id = sc.nextInt();
            for (i = 0; i < arr.size(); i++) {
                if (arr.get(i).getId() == id) {
                    j++;
                    System.out.println("学号为:" + arr.get(i).getId() + "的学生信息为:\n--------------------\n"
                            + "姓名:" + arr.get(i).getName() + "\n年龄为:" + arr.get(i).getAge() + "\n性别为:"
                            + arr.get(i).getSex() + "\n--------------------\n\n");
                    break;
                }
            }
            if (j == 0) {
                System.out.println("本系统暂时无该id学生信息!");
            }
        } else {
            System.out.println("本系统暂时无学生信息!");
        }
        menu();

    }

    //添加学生信息
    public static void add() {
        int id;
        String name;
        int age;
        String sex;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生id(整数):");
        id = sc.nextInt();
        System.out.println("请输入学生姓名:");
        name = sc.next();
        System.out.println("请输入学生年龄(整数):");
        age = sc.nextInt();
        System.out.println("请输入学生性别:");
        sex = sc.next();
        arr.add(new Student(id, name, age, sex));
        menu();
    }

    //修改学生信息
    public static void change() {
        if (arr.size() != 0) {
            int j = 0;
            System.out.println("请输入学生id!");
            Scanner sc = new Scanner(System.in);
            int id = sc.nextInt();
            for (int i = 0; i < arr.size(); i++) {
                if (arr.get(i).getId() == id) {
                    String name;
                    int age;
                    String sex;
                    System.out.println("请输入修改后学生姓名:");
                    name = sc.next();
                    System.out.println("请输入修改后学生年龄:");
                    age = sc.nextInt();
                    System.out.println("请输入修改后学生性别:");
                    sex = sc.next();
                    arr.set(i, new Student(id, name, age, sex));
                    j++;
                    break;
                }
            }
            if (j == 0) {
                System.out.println("本系统暂无该id学生信息!");
            }
        } else {
            System.out.println("本系统暂时无学生信息!");
        }
        menu();
    }

    //根据id删除学生信息
    public static void delete() {
        if (arr.size() != 0) {
            int j = 0;
            System.out.println("请输入学生id!");
            Scanner sc = new Scanner(System.in);
            int id = sc.nextInt();
            for (int i = 0; i < arr.size(); i++) {
                if (arr.get(i).getId() == id) {
                    int oid = arr.get(i).getId();
                    arr.remove(i);
                    System.out.println("学号为:" + oid + "的学生信息已删除成功!");
                    menu();
                    break;
                }
                j++;
            }
            if (j == 0) {
                System.out.println("本系统暂无该id学生信息!");
            }
        } else {
            System.out.println("本系统暂时无学生信息!");
        }
        menu();
    }
}

Student.java

package cn.chenwei.student;

public class Student {
    private int id;
    private String name;
    private int age;
    private String sex;


    public Student() {

    }

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

    public int getId() {
        return id;
    }

    public void setId(int 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 getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

 

posted @ 2019-03-05 15:24  陈小哥cw  阅读(444)  评论(0编辑  收藏  举报