더북(TheBook)

(여기부터는 여러분이 책의 코드를 그대로 따라오면서 실습한다고 가정합니다. 지금처럼 명시적으로 이야기하지는 않을 것입니다. 새로운 코드 조각을 본다면 그대로 따라해 보기 바랍니다.)

빠르게 다시 적어 봅니다. 프로파일을 생성한 후에는 질문(상여를 받았나요?)을 만듭니다. 다음 3줄은 Criterion 객체를 만들어 답변과 그 가중치를 저장합니다. 반대로 criteriaAnswer 변수는 같은 질문과 기대하는 정답(Bool.TRUE)을 포함합니다. 마지막으로 Criterion 객체를 Criteria 객체에 추가합니다.

(혹시 몰라 이야기하자면, Bool 클래스는 0과 1 값을 갖는 열거자(enum)의 래퍼 클래스입니다. 지금 테스트하는 코드가 좋은 코드라고는 주장하지 않겠습니다.)

matches() 메서드에서 for 반복문을 돌면서 answers 해시 맵(HashMap)에서 각 Criterion 객체에 대응하는 Answer 객체를 가져옵니다(Profile.java의 29행). 이는 사전에 Profile 객체에 적절한 Answer 객체를 먼저 넣어 놓아야 한다는 것을 의미합니다.

iloveyouboss_07/test/iloveyouboss/ProfileTest.java

@Test
public void test() {
    Profile profile = new Profile("Bull Hockey, Inc.");
    Question question = new BooleanQuestion(1, "Got bonuses?");
    Answer profileAnswer = new Answer(question, Bool.FALSE);
    profile.add(profileAnswer);
    Criteria criteria = new Criteria();
    Answer criteriaAnswer = new Answer(question, Bool.TRUE);
    Criterion criterion = new Criterion(criteriaAnswer, 
                                        Weight.MustMatch);
    criteria.add(criterion);
}

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.