TIL

Java 팀 프로젝트 - 점수 관리

jinny8 2024. 8. 6. 23:50

이번에는 저번 수강생 관리에 이어서

점수 관리 부분에 대해 코드를 작성하였다.

 

그중에서 나는 수강생의 과목별 회차 점수 수정 파트를 맡았다.

// 수강생의 과목별 회차 점수 수정
private static void updateRoundScoreBySubject() {

    String studentID = sc.next(); // 관리할 수강생 고유 번호
    System.out.println("시험 점수를 수정합니다.");
    IStudent student = getStudentByStudentId(studentID);
    Map<String, ISubject> subject = student.getSubjects();

    if (student == null) {
        System.out.println("해당 ID의 수강생을 찾을 수 없습니다.");
        return;
    }

    if (subject == null) {
        System.out.println("해당 과목을 찾을 수 없습니다.");
        return;
    }

    // 기능 구현 (수정할 과목 및 회차, 점수)
    System.out.println("과목을 입력하세요: ");
    String subjectName = sc.next();

    System.out.println("회차를 입력하세요: ");
    int round = sc.nextInt();
    // 기능 구현
    System.out.println("수정할 점수를 입력하세요: ");
    int updateScore = sc.nextInt();
    student.getSubjects().get(subjectName).updateScore(round, updateScore);
    System.out.println("점수 수정 성공");

}

 

 

팀원들의 도움을 받아 어렵사리 코드를 완성했다.

그리고 실행해 보니

 

 

관리 항목을 선택하라고만 하고 아무것도 뜨지 않는 것이었다...

왜 아무것도 뜨지 않나 생각해봤더니

 

<기존>

String studentID = sc.next(); // 관리할 수강생 고유 번호
    System.out.println("시험 점수를 수정합니다.");

 

입력값을 받고 "시험 점수를 수정합니다." 를 뜨게 해서

처음에 아무것도 뜨지 않는 것이라 생각했다.

 

<수정>

System.out.println("시험 점수를 수정합니다.");
    System.out.println("학생 ID를 입력하세요.");
    String studentID = sc.next(); // 관리할 수강생 고유 번호
    IStudent student = getStudentByStudentId(studentID);
    Map<String, ISubject> subject = student.getSubjects();

 

<기존>

그리고 마지막 줄인

System.out.println("점수 수정 성공");이

성공하든 실패하든 뜨기에

성공하는 경우와 실패하는 경우를 따로 나누기로 정했다.

 

<수정>

if (updateScore >= 0 && updateScore <= 100) {
        System.out.println("점수 수정 성공");
    } else {
        System.out.println("점수 수정 실패");
    }

 

if문을 통해 0이상 100이하인 경우만 수정 성공

그 이외의 경우에는 수정 실패로 지정하였다.

 

코드를 수정하니

이렇게 원하는 출력 결과를 얻게 되었다.

 

<최종 코드>

// 수강생의 과목별 회차 점수 수정
private static void updateRoundScoreBySubject() {
    System.out.println("시험 점수를 수정합니다.");
    System.out.println("학생 ID를 입력하세요.");
    String studentID = sc.next(); // 관리할 수강생 고유 번호
    IStudent student = getStudentByStudentId(studentID);
    Map<String, ISubject> subject = student.getSubjects();

    if (student == null) {
        System.out.println("해당 ID의 수강생을 찾을 수 없습니다.");
        return;
    }

    if (subject == null) {
        System.out.println("해당 과목을 찾을 수 없습니다.");
        return;
    }

    // 기능 구현 (수정할 과목 및 회차, 점수)
    System.out.println("과목을 입력하세요: ");
    String subjectName = sc.next();

    System.out.println("회차를 입력하세요: ");
    int round = sc.nextInt();
    // 기능 구현
    System.out.println("수정할 점수를 입력하세요: ");
    int updateScore = sc.nextInt();
    student.getSubjects().get(subjectName).updateScore(round, updateScore);

    if (updateScore >= 0 && updateScore <= 100) {
        System.out.println("점수 수정 성공");
    } else {
        System.out.println("점수 수정 실패");
    }
}