새로운 방법 : 두 학생이 일치하는지 확인하려고 할 때, 그리고
public class Student {
private int physics;
private int english;
private int chemistry;
public Student(int physics, int english, int chemistry) {
this.physics = physics;
this.english = english;
this.chemistry = chemistry;
}
public Set<Integer> marks() {
return new HashSet<Integer>(Arrays.asList(physics, english, chemistry));
}
}
, 당신이보고 할 필요가있다 StudentMatcher
이처럼 마크의 자신이 개 각각의 세트, 해체 여부 : 여기
public class StudentMatcher {
public boolean matches(Student student1, Student student2) {
Set<Integer> studentMarks1 = student1.marks();
Set<Integer> studentMarks2 = student2.marks();
return haveIntersection(studentMarks1, studentMarks2);
}
private boolean haveIntersection(Set<Integer> studentMarks1, Set<Integer> studentMarks2) {
return studentMarks1.removeAll(studentMarks2);
}
}
을 그리고 그것을 확인하는 단위 테스트는 작품이다
public class StudentMatcherTest {
@Test
public void matches() {
StudentMatcher matcher = new StudentMatcher();
Student student1 = new Student(34, 45, 66);
Student student2 = new Student(99, 55, 34);
Student student3 = new Student(11, 22, 33);
assertTrue("Should match", matcher.matches(student1, student2));
assertFalse("Should not match", matcher.matches(student1, student3));
}
}
더 나아질 수있는 방법이 많이 있지만, 게시 한 코드보다 코드가 더 복잡하다고 가정합니다. 따라서 더 나은 길을 열어두기에 충분합니다.
어떤 과목이 어떤 과목보다 세분성이 떨어지기 때문에 다른 데이터 구조와 함께 세트의 성적을 저장해야합니다. –
하지만 불행히도 나는 학생 대상을 변경할 수 없습니다. 병 안에 있어요. – crazyTechie
학생 클래스를 변경할 수 없다면, 하나씩 차례로 마크를 추가하십시오 :'aMarks.add (a.getPhysics)','aMarks.add (a.getEnglish) '... –