package com.sf._02_list.demo3;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentManagerSystem {


    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        // 搭建学生管理系统初始界面
        while (true){
            System.out.println("----------------------欢迎来到四福学生管理系统---------------------");
            System.out.println("1: 添加学生");
            System.out.println("2: 删除学生");
            System.out.println("3: 修改学生");
            System.out.println("4: 查询学生");
            System.out.println("5: 退出");
            // 通过键盘录入输入用户选择
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的选择:");
            int inputNumber = scanner.nextInt();
            if(inputNumber == 1){
                addStudent(list);
            }else if(inputNumber  == 2){
                System.out.println("请输入要删除的学生id");
                long sid = scanner.nextLong();
                removeStudent(sid,list);
            }else if(inputNumber == 3){
                System.out.println("请输入要修改的学生id");
                long sid = scanner.nextLong();
                updateStudent(sid,list);
            }else if(inputNumber == 4){
                queryStudents(list);
            }else if(inputNumber == 5){
                break;
            }else{
                System.out.println("请输入1-5");
            }

        }
    }

    private static void queryStudents(List<Student> list) {
        System.out.println("查询操作");
        System.out.println("学生id    学生姓名   学生年龄   学生地址");
        for (Student student : list) {
            System.out.println(student.getSid() +"         " +student.getName()+"       "+student.getAge() +"     "+ student.getAddress());
        }
    }

    private static void updateStudent(Long sid,List<Student> list) {
        // 1 判断要更改sid 是否存在
        // 如果flag = true 表示要修改id 数据是存在可以修改, 如果flag=false 不存在不允许修改
        boolean flag = isStudentId(sid, list);

        if(flag){
            // 修改名称  修改年龄  修改 地址
            // 一般修改信息的时候学号是不会变好
            Student updateStudent = new Student();
            updateStudent.setSid(sid);
            Scanner scanner = new Scanner(System.in);

            System.out.println("请录入要修改name");
            String name = scanner.next();
            updateStudent.setName(name);

            System.out.println("请录入要修改age");
            int age = scanner.nextInt();
            updateStudent.setAge(age);

            System.out.println("请录入要修改地址");
            String address = scanner.next();
            updateStudent.setAddress(address);

            // 替换对应索引位置 student
            // 找到索引位置
            // {student1,student2 ,student3}
            int index = findStudentIndex(sid,list);
            list.set(index,updateStudent);
        }else{
            System.out.println("要修改数据不存在");
        }
    }

    /**
     * 找student 锁在的索引信息
     */
    private static int findStudentIndex(Long sid, List<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(sid == student.getSid()){
                return i;
            }
        }
        return -1;
    }

    private static void removeStudent(Long id ,List<Student> students) {
        // 判断id 是否存在
        boolean flag = isStudentId(id, students);
        // flag = true 表存在  false 表示不存在
        if(flag){
            int studentIndex = findStudentIndex(id, students);
            students.remove(studentIndex);
            System.out.println("删除成功");
        }else{
            System.out.println("删除学生不存在");
        }
    }

    private static void addStudent(List<Student> list) {
        Scanner scanner = new Scanner(System.in);
        Student student = new Student();
        while (true){
            System.out.println("请录入学生id");
            long sid = scanner.nextLong();
            boolean flag = isStudentId(sid,list);
            if(!flag){
                student.setSid(sid);
                break;
            }else{
                System.out.println("id 已经存在请重新输入");
            }
        }
        System.out.println("请输入学生姓名");
        String name = scanner.next();
        student.setName(name);

        System.out.println("请输入学生年龄");
        int age = scanner.nextInt();
        student.setAge(age);

        System.out.println("请输入学生地址");
        String address = scanner.next();
        student.setAddress(address);
        // 把学生信息添加到集合中
        list.add(student);
//        System.out.println(list);
    }

    /**
     * 判断学生id 是否存在
     * @param sid
     * @param list
     * @return
     */
    private static boolean isStudentId(long sid, List<Student> list) {
        boolean flag = false;
        for (Student student : list) {
            if(student.getSid() == sid){
                flag = true;
            }
        }
        return flag;
    }
}
