14.text--学生类

发布时间 2023-03-29 10:00:02作者: 小黑确实不黑

学生类

定义一个长度为3的数组,数组存储1-3名学生对象作为初始数据,学生对象的学号,姓名各不相同
学生的属性:学号、姓名、年龄
要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
要求2:添加完毕之后,遍历所有学生信息
要求3:通过ID删除学生信息
如果存在,则删除,如果不存在,则提示删除失败
要求4:删除完毕之后,遍历所有学生信息
要求5:查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁

因为要求复杂,所以分成了三个测试类,分别完成各个要求

1.JavaBean:

//学生对象
public class Student {
    private String id;
    private String name;
    private int age;

    public Student() {
    }

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

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

2.Test1

//要求1、要求2
public class StudentTest01 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        Student s1 = new Student("heima001","周健",22);
        Student s2 = new Student("heima002","杨伟楠",24);
        Student s3 = new Student("heima003","彭成思",24);

        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;

        //要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断
        Student s4 = new Student("heima002","丁郎平",21);

        //唯一性判断,调用方法实现
        boolean flag = contains(s4.getId(), arr);
        if(flag){
            //id存在于数组中 --- 已存在 --- 不用添加
            System.out.println("当前id重复,请修改id后再进行添加");
        }else{
            //id不存在于数组中 --- 不存在 --- 就可以把学生对象添加到数组中
            //获取数组元素个数,调用方法实现
            int count = getCount(arr);
            //根据数组元素个数,满了则无法添加只能创建新数组,未满则直接添加
            if(count == arr.length){
                //1.数组已经存满 --- 只能创建一个新数组 --- 调用方法实现
                Student[] newArr = createNewArr(arr);
                newArr[count] = s4;

                //要求2:添加完毕之后,遍历所有学生信息,调用方法实现
                print(newArr);
            }else{
                //2.数组没有存满 --- 直接添加,此时的count相当于数组中,存在空值位置的索引
                arr[count] = s4;

                //要求2:添加完毕之后,遍历所有学生信息,调用方法实现
                print(arr);
            }
        }




    }

    //返回值选布尔值,因为是比较是否存在的方法,存在true or 不存在false
    public static boolean contains(String id,Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student student = arr[i];
            if(student != null && id == student.getId()){
                    return true;
            }
        }
        return false;
    }

    //定义一个方法判断数组中已经存入几个元素
    public static int getCount(Student[] arr){
        //定义一个计数器用来统计
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
                count++;
            }
        }
        return count;
    }

    //定义一个方法,创建新数组,长度 = 老数组的长度+1,把老数组的元素拷贝到新数组中
    public static Student[] createNewArr(Student[] arr){
        Student[] newArr = new Student[arr.length + 1];
        //拷贝老数组
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    //定义一个方法,遍历数组并打印
    public static void print(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student s = arr[i];
            if(s != null){
                System.out.println(s.getId() + " " + s.getName() + " " + s.getAge());
            }
        }
    }
}

3.Test2

//要求3、要求4
public class StudentTest02 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        Student s1 = new Student("heima001","周健",22);
        Student s2 = new Student("heima002","杨伟楠",24);
        Student s3 = new Student("heima003","彭成思",24);

        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;

        //要求3:通过ID删除学生信息
        //如果存在,则删除,如果不存在,则提示删除失败

        //要找到id在数组中对应的索引
        int index = getIndex("heima002",arr);

        //进行判断是否存在 index?0
        if(index >= 0){
            //存在,删除信息
            arr[index] = null;
            //要求4:删除完毕之后,遍历所有学生信息
            print(arr);
        }else{
            //不存在,删除失败
            System.out.println("当前id不存在,删除失败");
        }


    }

    //定义一个方法,找到id在数组中的索引
    public static int getIndex(String id,Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                String sid = stu.getId();
                if(id == sid){
                    return i;
                }
            }
        }
        return -1;
    }

    //定义一个方法,遍历数组并打印
    public static void print(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student s = arr[i];
            if(s != null){
                System.out.println(s.getId() + " " + s.getName() + " " + s.getAge());
            }
        }
    }
}

4.Test3

//要求5:查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁
public class StudentTest03 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        Student s1 = new Student("heima001","周健",22);
        Student s2 = new Student("heima002","杨伟楠",24);
        Student s3 = new Student("heima003","彭成思",24);

        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;

        //获取id对应的数组索引,调用方法实现
        int index = getIndex("heima002",arr);

        //判断索引,决定是否执行操作
        if(index >= 0){
            //存在,则按要求对年龄+1
            Student stu = arr[index];
            int newAge = stu.getAge() + 1;
            stu.setAge(newAge);

            print(arr);
        }else{
            //不存在,则提示错误
            System.out.println("当前id不存在,修改失败");
        }
    }

    //定义一个方法,获取id对应的数组索引
    public static int getIndex(String id,Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                String sid = stu.getId();
                if(id == sid){
                    return i;
                }
            }
        }
        return -1;
    }

    //定义一个方法,遍历数组并打印
    public static void print(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student s = arr[i];
            if(s != null){
                System.out.println(s.getId() + " " + s.getName() + " " + s.getAge());
            }
        }
    }
}