Java-学生管理系统(二维数组)

class StudentManagementSystem {
    public static String[][] stu = new String[5][5];
    public static int point = 0;

    public static void main(String[] args) {
        add("小红", "女", "18", "1804", "001");
        add("小黄", "女", "18", "1804", "002");
        add("小绿", "女", "18", "1804", "003");
        add("小兰", "女", "18", "1804", "004");
        update("1804", "003", 1, "小蓝");
        delete("1804", "003");
        System.out.println("姓名\t性别\t年龄\t班级\t学号");
        print();
    }

    // 添加
    public static boolean add(String name, String sex, String age, String classId, String id) {
        // 判断合法性
        if (!MyUtils.isOk(name, sex, age, classId, id)) {
            System.out.println("学生信息不合法");
            return false;
        }
        // 判断是否存在
        if (query(classId, id) != -1) {
            System.out.println("此学生已存在");
            return false;
        }
        // 扩容
        if (point == stu.length) {
            addCapacity();
        }
        // 添加
        stu[point][0] = name;
        stu[point][1] = sex;
        stu[point][2] = age;
        stu[point][3] = classId;
        stu[point][4] = id;
        point++;
        System.out.println("添加成功");
        return true;
    }

    // 删除学生
    public static boolean delete(String classId, String Id) {
        // 判断合法性
        if (!MyUtils.isClassId(classId) || !MyUtils.isId(Id)) {
            System.out.println("学生信息不合法");
            return false;
        }
        // 判断是否存在
        int index = query(classId, Id);
        if (index == -1) {
            System.out.println("此学生不存在");
            return false;
        }
        // 删除
        for (int i = index; i < point - 1; i++) {
            for (int j = 0; j < 5; j++) {
                stu[index][j] = stu[index + 1][j];
            }
        }
        point--;
        System.out.println("删除成功");
        return true;
    }

    // 修改学生
    public static boolean update(String classId, String Id, int type, String value) {
        // 判断合法性
        if (!MyUtils.isClassId(classId) || !MyUtils.isId(Id)) {
            System.out.println("学生信息不合法");
            return false;
        }
        // 判断是否存在
        int index = query(classId, Id);
        if (index == -1) {
            System.out.println("此学生不存在");
            return false;
        }
        // 修改
        switch (type) {
        case Type.NAME:
            // 判断合法性
            if (!MyUtils.isName(value)) {
                return false;
            }
            // 修改
            stu[index][0] = value;

            break;

        case Type.SEX:
            // 判断合法性
            if (!MyUtils.isSex(value)) {
                return false;
            }
            // 修改
            stu[index][1] = value;
            break;
        case Type.AGE:
            // 判断合法性
            if (!MyUtils.isAge(value)) {
                return false;
            }
            // 修改
            stu[index][2] = value;
            break;
        case Type.CLASS_ID:
            // 判断合法性
            if (!MyUtils.isClassId(value)) {
                return false;
            }
            if (query(value, Id) != -1) {
                return false;
            }
            // 修改
            stu[index][3] = value;
            break;
        case Type.ID:
            // 判断合法性
            if (!MyUtils.isId(value)) {
                return false;
            }
            // 修改
            stu[index][4] = value;
            break;
        }
        System.out.println("修改成功");
        return true;
    }

    // 查询
    public static int query(String classId, String id) {
        for (int i = 0; i < point; i++) {
            if (stu[i][3].equals(classId) && stu[i][4].equals(id)) {
                return i;
            }
        }
        return -1;
    }

    public static void addCapacity() {
        String[][] newStu = new String[stu.length * 2][5];
        for (int i = 0; i < point; i++) {
            newStu[i][1] = stu[i][1];
            newStu[i][2] = stu[i][2];
            newStu[i][3] = stu[i][3];
            newStu[i][4] = stu[i][4];
            newStu[i][5] = stu[i][5];
        }
        stu = newStu;
    }

    // 打印
    public static void print() {
        for (int i = 0; i < point; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(stu[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

// 判断合法性的工具类
class MyUtils {
    public static boolean isOk(String name, String sex, String age, String classId, String id) {
        if (!isName(name) || !isSex(sex) || !isAge(age) || !isClassId(classId) || !isId(id)) {
            return false;
        }
        return true;
    }

    public static boolean isName(String name) {
        if (name.length() < 2 || name.length() > 8) {
            return false;
        }
        return true;
    }

    public static boolean isSex(String sex) {
        if (!sex.equals("男") && !sex.equals("女")) {
            return false;
        }
        return true;
    }

    public static boolean isAge(String age) {
        int i = Integer.parseInt(age);
        if (i < 6 || i > 60) {
            return false;
        }
        return true;
    }

    public static boolean isClassId(String classId) {
        if (classId.length() != 4) {
            return false;
        }
        return true;
    }

    public static boolean isId(String id) {
        if (id.length() != 3) {
            return false;
        }
        return true;
    }
}

// 学生信息常量
class Type {
    public static final int NAME = 1;
    public static final int SEX = 2;
    public static final int AGE = 3;
    public static final int CLASS_ID = 4;
    public static final int ID = 5;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值