Java方法学生管理系统(CRUD数组)

import java.util.Arrays;
import java.util.Scanner;
/**
 * 使用方法来改写数组的CRUD
 *
 * 打算采用四个数组来记录学生的信息,学生有编号,姓名,年龄,性别
 *
 * 学生编号和索引没有任何关系,用于唯一表示一条学生记录!
 *             int[] ids = {1,2,3,4}
 *             int[] ages = {19,17,19,20}
 *
 *             String[] names = {"张三","李四","王五","赵六"}
 *             String[] genders = {"男","男","女","女"}
 *
 * 四个数组,相同的索引,何在一起构成一个学生的信息
 *         1,19,张三,男
 *         2,17,李四,男
 *         3,19,王五,女
 *         4,20,赵六,女
 *
 *
 * @author Administrator
 *
 */
public class demo62 {
    // -- 写在方法外叫做成员,被static修饰,叫做类变量,也叫做静态成员变量
    static Scanner scan = new Scanner(System.in);
    static int[] ids = new int[10];
    static int[] ages = new int[10];
    static String[] names = new String[10];
    static String[] genders = new String[10];
    // -- 记录数组中元素的个数,和下一个元素的索引!!
    static int size = 0;
    public static void main(String[] args) {
        initData();
        boolean flag = true;
        while (flag) {
            showMenu();
            int choose = nextInt();
            switch (choose) {
                case 1:
                    // -- 添加
                    addStudent();
                    break;
                case 2:
                    // -- 指定位置添加
                    addStudentAtspecificPosition();
                    break;
                case 3:
                    // -- 修改
                    updateStudent();
                    break;
                case 4:
                    // -- 删除
                    deleteStudent();
                    break;
                case 5:
                    // -- 查找
                    findStudent();
                    break;
                case 6:
                    // -- 显示所有
                    listStudent();
                    break;
                case 7:
                    flag = false;
                    break;
                default:
                    break;
            }
        }
        scan.close();
    }
    /**
     * 显示菜单项
     */
    public static void showMenu() {
        System.out.println("====================");
        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("7.退出");
        System.out.println("====================\n");
    }
    /**
     * 封装了一个方法,专门用于获取整数 解决输入类型不匹配问题
     *
     * @return
     */
    public static int nextInt() {
        // -- 局部变量必须初始化后才可以使用!
        int value = 0;
        while (true) {
            if (scan.hasNextInt()) {
                value = scan.nextInt();
                break;
            } else {
                // -- 输入的不是整数,要把类型不匹配的给取出来
                scan.next();
                System.out.println("只能输入数字,请重新输入");
            }
        }
        return value;
    }
    /**
     * 顺序添加学生
     */
    public static void addStudent() {
        // -- 检查数组的容量,如果容量超过0.8 自动扩容
        checkCapacity();
        // -- 获取编号,姓名,年龄,性别四个值
        System.out.println("请输入学生编号");
        int id = nextInt();
        System.out.println("请输入学生姓名");
        String name = scan.next();
        System.out.println("请输入学生年龄");
        int age = nextInt();
        System.out.println("请输入学生性别");
        String gender = scan.next();
        // -- 把四个值分别放到数组中 千万记得 size的改变要放在最后!
        names[size] = name;
        ages[size] = age;
        genders[size] = gender;
        ids[size++] = id;
    }
    /**
     * 指定位置添加学生
     */
    public static void addStudentAtspecificPosition() {
        System.out.println("请输入要插入的位置");
        int position = 0;
        while (true) {
            position = nextInt();
            // -- 这个位置是索引啊 ,必须在[0,size-1]之间
            if (position < 0 || position > size - 1) {
                System.out.println("指定的位置不合法,位置必须在[0," + (size - 1) + "]之间");
                // -- 代码不需要继续向执行了,再次录入!!
                continue;
            }
            break;
        }
        // -- 位置已经拿到了,下面就是就是录入数据
        // -- 获取编号,姓名,年龄,性别四个值
        System.out.println("请输入学生编号");
        int id = nextInt();
        System.out.println("请输入学生姓名");
        String name = scan.next();
        System.out.println("请输入学生年龄");
        int age = nextInt();
        System.out.println("请输入学生性别");
        String gender = scan.next();
        // -- 把四个数据分别存储到相应的数组中,注意移位
        for (int i = size - 1; i >= position; i--) {
            ids[i + 1] = ids[i];
            ages[i + 1] = ages[i];
            genders[i + 1] = genders[i];
            names[i + 1] = names[i];
        }
        // System.err.println("移位后:" + Arrays.toString(names));
        // -- 开始赋值
        names[position] = name;
        genders[position] = gender;
        ages[position] = age;
        ids[position] = id;
        // -- 千万要记得 ,指定位置添加 那也是添加啊 ,添加完成后 size要改变的!
        size++;
    }
    /**
     * 修改学生
     */
    public static void updateStudent() {
        System.out.println("请输入学生编号");
        while (true) {
            // -- 修改的是已经存在的学生.这种修改往往是根据唯一表示.这里可以是name也可以是id
            // -- 如果是name 那么name的值必须唯一!(unique)
            // -- 目前有一个唯一标识 就是ID,
            // -- 输入学生的ID 检查学生是否存在
            int id = nextInt();
            // -- 根据ID在数组中查找学生记录,如果找到了,返回这条记录的索引!
            int index = checkStudentExists(id);
            // --
            if (index != -1) {
                // -- 代表存在,开始修改!
                System.out.println("请输入学生姓名");
                String name = scan.next();
                System.out.println("请输入学生年龄");
                int age = nextInt();
                System.out.println("请输入学生性别");
                String gender = scan.next();
                names[index] = name;
                ages[index] = age;
                genders[index] = gender;
                break;
            } else {
                // -- 代表不存在
                System.out.println("你输入的学生编号不存在,重新输入");
            }
        }
    }
    /**
     * 删除学生
     */
    public static void deleteStudent() {
        System.out.println("请输入学生编号");
        int id = 0;
        int index = 0;
        while (true) {
            id = nextInt();
            index = checkStudentExists(id);
            if (index == -1) {
                System.out.println("编号不存在,重新输入");
                continue;
            }
            break;
        }
        // -- 根据ID得到这条记录的Index
        for (int i = index; i < size; i++) {
            ids[i] = ids[i + 1];
            ages[i] = ages[i + 1];
            names[i] = names[i + 1];
            genders[i] = genders[i + 1];
        }
        // -- 删除之后size 要 -1
        size--;
    }
    /**
     * 查找学生
     */
    public static void findStudent() {
        // -- 条件查询 比如根据姓名来查
        System.out.println("请输入学生姓名");
        String name = scan.next();
        int[] indexArray = new int[size];
        // -- 为了避免0值在这里有歧义 int类型的默认值就是0 ,而所以也可以取值为0
        // -- 把indexArray中的所有值改为-1 那取到的0一定时索引,而不是默认值!
        for (int i = 0; i < indexArray.length; i++) {
            indexArray[i] = -1;
        }
        int index = 0;
        for (int i = 0; i < size; i++) {
            // -- 把名称匹配的索引保存下来
            if (names[i].equals(name)) {
                indexArray[index++] = i;
            }
        }
        //System.out.println("indexArray:" + Arrays.toString(indexArray));
        // -- 获取所有匹配的索引
        System.out.println("学生编号\t学生姓名\t学生年龄\t学生性别");
        for (int i = 0; i < indexArray.length ; i++) {
            if (indexArray[i] != -1) {
                //System.out.println(indexArray[i]);
                System.out.println(ids[indexArray[i]] + "\t" + names[indexArray[i]] + "\t" + ages[indexArray[i]] + "\t" + genders[indexArray[i]]);
            }
        }
    }
    /**
     * 显式所有学生
     */
    public static void listStudent() {
        System.out.println("学生编号\t学生姓名\t学生年龄\t学生性别");
        for (int i = 0; i < size; i++) {
            System.out.println(ids[i] + "\t" + names[i] + "\t" + ages[i] + "\t" + genders[i]);
        }
    }
    /**
     * 检查容量是否足够,不够扩容
     *
     * @return
     */
    public static void checkCapacity() {
        if ((double) size / ids.length >= 0.8) {
            ids = Arrays.copyOf(ids, (int) (ids.length * 1.5));
            genders = Arrays.copyOf(genders, (int) (genders.length * 1.5));
            ages = Arrays.copyOf(ages, (int) (ages.length * 1.5));
            names = Arrays.copyOf(names, (int) (names.length * 1.5));
        }
    }
    /**
     * 检查学生是否存在,不存在返回-1 存在返回相应的索引
     *
     * @return
     */
    public static int checkStudentExists(int id) {
        for (int i = 0; i < size; i++) {
            if (ids[i] == id) {
                return i;
            }
        }
        return -1;
    }
    /**
     * 初始化数据
     */
    public static void initData() {
        ages[size] = 17;
        names[size] = "张三";
        genders[size] = "男";
        ids[size++] = 1;
        ages[size] = 18;
        names[size] = "李四";
        genders[size] = "女";
        ids[size++] = 2;
        ages[size] = 17;
        names[size] = "王五";
        genders[size] = "男";
        ids[size++] = 3;
        ages[size] = 19;
        names[size] = "赵六";
        genders[size] = "女";
        ids[size++] = 4;
    }
    public static int getMaxId() {
        int max = 0;
        for (int i = 0; i < size; i++) {
            if (max < ids[i]) {
                max = ids[i];
            }
        }
        // -- ids[size] = getMaxId()+1
        return max;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值