java 배우기/TIL

팀 프로젝트 추가 사항(멤버ID로 멤버 정보 조회) 구현하기

checkuu 2024. 8. 6. 21:16
public static void inquireStudentInfo(List<Student> studentStore) {
        System.out.println("\n수강생 목록을 조회합니다...");
        String studentId = getStudentId(); // 학생 id 입력
        System.out.println();
        Student studentInfo = null;
        // studentStore 리스트에서 학생 정보 받아서 목록 출력 + 순서대로 번호 부여
        for (int i = 0; i < studentStore.size(); i++) {
            if (studentId.equals(studentStore.get(i).getStudentId())) { //입력받은 id가 스튜던트 스토어에 같은값이 있다면 진행
                studentInfo = studentStore.get(i); //진행 오류 확인용
                System.out.println("고유번호: "); //과목 이름 받아오는 메서드가 세로로 나열해서
                //비슷하게 맞추기 위한 세로 정렬
                System.out.println(" " + studentStore.get(i).getStudentId());
                System.out.println("이름: ");
                System.out.println(" " + studentStore.get(i).getStudentName());
                System.out.println("상태: ");
                System.out.println(" " + studentStore.get(i).getStatus());
                System.out.println("과목: ");
                Helper.getSubjectNameListByStudentId(studentId); //해당학생이 선택한 과목 리스트 불러오기
                break;
            }
        }
        if (studentInfo == null) {
            System.out.println("해당 학생을 찾을 수 없습니다.");
            return; //오류시 끝내기
        }
        System.out.println("\n수강생 목록 조회 성공!");
    }

위와 같은 식으로 studentId를 입력받은값을 studentId에 담아주고 for 문을 돌면서 studentStore에 저장된 같은 값을 찾는다.

찾은위치의 저장된 이름 , 상태 , 과목을 불러온다.

studentInfo에 null을 선언하고 for문이 제대로 돌면 다른값이 들어가게 설정해준다.

그리고 for문 밑에 studentInfo 값이 null 이라면 for문이 제대로 돌지 않은것이므로 오류라고 나타내주고 다시 되돌아가는 코드를 짰다.

그리고 메인에 있던 모든 메서드들을 각각 클래스로 나누고 한 pakage에 담아서 분리 시켜줬다.

 

오늘 추가 사항하면서 for문에 대해 조금더 가까워진 느낌이다.