Skip to content

Commit

Permalink
Merge pull request #288 from Myongji-Graduate/feature/DEV-47-voluntee…
Browse files Browse the repository at this point in the history
…r_credit_free_elective

[DEV-47] 봉사학점의 이수학점이 자유선택으로 반영되도록 수정
  • Loading branch information
tiemo0708 authored Nov 30, 2024
2 parents 9d12cf2 + 5379ebe commit 2339a42
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,70 @@

import com.plzgraduate.myongjigraduatebe.takenlecture.domain.model.TakenLecture;
import com.plzgraduate.myongjigraduatebe.takenlecture.domain.model.TakenLectureInventory;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import lombok.Builder;
import lombok.Getter;

@Getter
public class NormalCultureGraduationResult {

private final String categoryName;
private final int totalCredit;
private boolean isCompleted;
private int takenCredit;
private static final String VOLUNTEER_CREDIT_CODE = "KMA02198";

@Builder
private NormalCultureGraduationResult(String categoryName, boolean isCompleted, int totalCredit,
int takenCredit) {
this.categoryName = categoryName;
this.isCompleted = isCompleted;
this.totalCredit = totalCredit;
this.takenCredit = takenCredit;
}
private final String categoryName;
private final int totalCredit;
private boolean isCompleted;
private int takenCredit;

public static NormalCultureGraduationResult create(int totalCredit,
TakenLectureInventory takenLectureInventory,
List<DetailGraduationResult> detailGraduationResults) {
return NormalCultureGraduationResult.builder()
.categoryName(NORMAL_CULTURE.getName())
.isCompleted(false)
.totalCredit(totalCredit)
.takenCredit(calculateTakenCredit(takenLectureInventory, detailGraduationResults))
.build();
}
@Builder
private NormalCultureGraduationResult(String categoryName, boolean isCompleted, int totalCredit,
int takenCredit) {
this.categoryName = categoryName;
this.isCompleted = isCompleted;
this.totalCredit = totalCredit;
this.takenCredit = takenCredit;
}

private static int calculateTakenCredit(TakenLectureInventory takenLectureInventory,
List<DetailGraduationResult> detailGraduationResults) {
int remainCreditByDetailGraduationResult = detailGraduationResults.stream()
.mapToInt(DetailGraduationResult::getNormalLeftCredit)
.sum();
public static NormalCultureGraduationResult create(int totalCredit,
TakenLectureInventory takenLectureInventory,
List<DetailGraduationResult> detailGraduationResults) {
return NormalCultureGraduationResult.builder()
.categoryName(NORMAL_CULTURE.getName())
.isCompleted(false)
.totalCredit(totalCredit)
.takenCredit(calculateTakenCredit(takenLectureInventory, detailGraduationResults))
.build();
}

Set<TakenLecture> remainTakenNormalCultures = takenLectureInventory.getCultureLectures();
int remainCreditByTakenLectures = remainTakenNormalCultures.stream()
.mapToInt(takenLecture -> takenLecture.getLecture()
.getCredit())
.sum();
private static int calculateTakenCredit(TakenLectureInventory takenLectureInventory,
List<DetailGraduationResult> detailGraduationResults) {
int remainCreditByDetailGraduationResult = detailGraduationResults.stream()
.mapToInt(DetailGraduationResult::getNormalLeftCredit)
.sum();
Set<TakenLecture> remainTakenNormalCultures = takenLectureInventory.getCultureLectures().stream()
.filter(takenLecture -> !takenLecture.getLecture().getId().equals(VOLUNTEER_CREDIT_CODE))
.collect(Collectors.toSet());
int remainCreditByTakenLectures = remainTakenNormalCultures.stream()
.mapToInt(takenLecture -> takenLecture.getLecture().getCredit())
.sum();
takenLectureInventory.handleFinishedTakenLectures(remainTakenNormalCultures);
return remainCreditByDetailGraduationResult + remainCreditByTakenLectures;
}

takenLectureInventory.handleFinishedTakenLectures(remainTakenNormalCultures);
return remainCreditByDetailGraduationResult + remainCreditByTakenLectures;
}

public void checkCompleted() {
this.isCompleted = takenCredit >= totalCredit;
}
public void checkCompleted() {
this.isCompleted = takenCredit >= totalCredit;
}

public int getLeftCredit() {
if (totalCredit >= takenCredit) {
return 0;
}
int leftCredit = takenCredit - totalCredit;
this.takenCredit = totalCredit;
return leftCredit;
}
public int getLeftCredit() {
if (totalCredit >= takenCredit) {
return 0;
}
int leftCredit = takenCredit - totalCredit;
this.takenCredit = totalCredit;
return leftCredit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,56 @@ void createFreeElectiveGraduationResult() {
remainCreditByTakenLectures + freeElectiveLeftCredit + leftNormalCultureCredit);
}

@DisplayName("봉사학점을 한 번 들었을 때 자유선택 졸업 결과를 생성한다.")
@Test
void createFreeElectiveGraduationResult_withOneVolunteerCredit() {
//given
User user = UserFixture.경영학과_19학번_ENG34();
Set<TakenLecture> takenLectures = new HashSet<>((Set.of(
TakenLecture.of(user, mockLectureMap.get("KMA02198"), 2021, Semester.FIRST) // 봉사학점
)));
TakenLectureInventory takenLectureInventory = TakenLectureInventory.from(takenLectures);

int totalFreeElectiveCredit = 7;
int leftNormalCultureCredit = 0;

//when
FreeElectiveGraduationResult freeElectiveGraduationResult = FreeElectiveGraduationResult.create(
totalFreeElectiveCredit,
takenLectureInventory,
List.of(),
leftNormalCultureCredit);

//then
assertThat(freeElectiveGraduationResult)
.extracting("categoryName", "takenCredit")
.contains(FREE_ELECTIVE.getName(), 1); // 봉사학점 1학점 반영
}

@DisplayName("봉사학점을 두 번 들었을 때 자유선택 졸업 결과를 생성한다.")
@Test
void createFreeElectiveGraduationResult_withTwoVolunteerCredits() {
//given
User user = UserFixture.경영학과_19학번_ENG34();
Set<TakenLecture> takenLectures = new HashSet<>((Set.of(
TakenLecture.of(user, mockLectureMap.get("KMA02198"), 2021, Semester.FIRST), // 봉사학점 1
TakenLecture.of(user, mockLectureMap.get("KMA02198"), 2022, Semester.SECOND) // 봉사학점 2
)));
TakenLectureInventory takenLectureInventory = TakenLectureInventory.from(takenLectures);

int totalFreeElectiveCredit = 7;
int leftNormalCultureCredit = 0;

//when
FreeElectiveGraduationResult freeElectiveGraduationResult = FreeElectiveGraduationResult.create(
totalFreeElectiveCredit,
takenLectureInventory,
List.of(),
leftNormalCultureCredit);

//then
assertThat(freeElectiveGraduationResult)
.extracting("categoryName", "takenCredit")
.contains(FREE_ELECTIVE.getName(), 2); // 봉사학점 2학점 반영
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,27 @@ void createNormalCultureGraduationResult() {
.contains(NORMAL_CULTURE.getName(),
remainTakenNormalCultureCredit + remainCreditByDetailGraduationResult);
}
@DisplayName("봉사학점은 일반교양 학점에 포함되지 않는다.")
@Test
void volunteerCreditIsNotIncludedInNormalCulture() {
//given
User user = UserFixture.경영학과_19학번_ENG34();
Set<TakenLecture> takenLectures = new HashSet<>((Set.of(
TakenLecture.of(user, mockLectureMap.get("KMA00101"), 2019, Semester.FIRST),
TakenLecture.of(user, mockLectureMap.get("KMA02102"), 2019, Semester.FIRST),
TakenLecture.of(user, mockLectureMap.get("KMA02198"), 2021, Semester.FIRST) // 봉사학점
)));
TakenLectureInventory takenLectureInventory = TakenLectureInventory.from(takenLectures);

//when
NormalCultureGraduationResult normalCultureGraduationResult = NormalCultureGraduationResult.create(
9,
takenLectureInventory,
List.of());

//then
assertThat(normalCultureGraduationResult)
.extracting("takenCredit")
.isEqualTo(4); // 봉사학점(1학점)은 제외되고, 나머지 일반교양 학점만 포함
}
}
1 change: 1 addition & 0 deletions src/test/resources/lecture.csv
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ KMA02135, 우주생명마음, 3, 0, KMA02119
KMA02136, SW프로그래밍입문, 3, 0, null
KMA02138, 인공지능의세계, 3, 0, null
KMA02139, 4차산업혁명의이해, 3, 0, null
KMA02198, 봉사학점, 1, 0, null
KMB02127, 한국문화와문자생활, 3, 0, null
KMB02119, 영어와영미문화, 3, 1, null
KMB02120, 아랍의언어와문화, 3, 0, null
Expand Down

0 comments on commit 2339a42

Please sign in to comment.