package _02_复杂对象练习;

import java.util.Scanner;

public class StudentTest2 {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(1L,"zhangsan",18);
        students[1] = new Student(2L,"lisi",18);
        students[2] = new Student(3L,"wangwu",18);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请录入要查找学号");
        long studentNumber = scanner.nextLong();
        // 判断查找id 在数组当中是否存在
        boolean flag = contains(studentNumber, students);
        if(flag){
            // 找到id 锁在位置
            for (int i = 0; i < students.length; i++) {
                Student students1 = students[i];
                if(students1.getId() == studentNumber){
                    // 如果相同说明这个学生就是我们要找的血行
                    // 让学生年龄+1
                    students1.setAge(students1.getAge() + 1);
                    System.out.println(students1);
                }
            }
        }else{
            System.out.println("查找id 不存在");
        }
    }


    private static boolean contains(Long id, Student[] students) {
        /**
         * NullPointerException ： 空指针异常
         * 原因： 使用null 去调用了属性或者是方法
         */
        for (Student student : students) {
            // 判断当前student 不为哦那个做id 比较， 因为数组当中如果没存满，可能为空null
            if(student!=null){
                if(student.getId() == id){
                    return true;
                }
            }
        }
        return false;
    }
}
