Skip to content

Commit

Permalink
[DEV-54] 편입 로직 수정 및 리팩토링 (#297)
Browse files Browse the repository at this point in the history
* fix: 편입 전공의 인정학점이 제대로 반영되도록 수정

* refactor: 편입 인정 학점 업데이트 코드 리팩토링

* test: 수정에 따른 테스트 코드 수정과
user의 TransferCredit 정보 업데이트에 관한 테스트 코드 추가

* test: 주전공 선택 상세 졸업 결과 계산 테스트코드 작성

* chore: 사용하지 않는 임포트문 제거

* test: 편입생 졸업학점 테스트

* refactor: 편입 로직 리팩토링
메소드 분리, 접근제한자 수정

* refactor: 불필요한 리팩토링 제거

* test:편입학생의 졸업요건 테스트코드 수정
  • Loading branch information
tiemo0708 authored Dec 31, 2024
1 parent 8ddb5b0 commit 35c7bf8
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@ private boolean checkCompleted() {
isCompleted = totalCredits <= takenCredits && isSatisfiedMandatory;
return isCompleted;
}
public void addTakenCredits(int credits) {
this.takenCredits += credits;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ private static int calculateTakenCredit(TakenLectureInventory takenLectureInvent
.getCredit())
.sum();

int transferFreeElectiveCredit = 0;
int transferFreeElectiveCredit = calculateTransferFreeElectiveCredit(user);

// 편입생일 경우 추가 학점을 계산
if (user.getStudentCategory() == StudentCategory.TRANSFER) {
transferFreeElectiveCredit = user.getTransferCredit().getFreeElective();
}
return remainCreditByDetailGraduationResult + remainCreditByTakenLectures
+ leftNormalCultureCredit+transferFreeElectiveCredit;
}
private static int calculateTransferFreeElectiveCredit(User user) {
if (user.getStudentCategory() == StudentCategory.TRANSFER) {
return user.getTransferCredit().getFreeElective();
}
return 0;
}

public void checkCompleted() {
this.isCompleted = takenCredit >= totalCredit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void addUpTakenCredit(User user) {
double freeElectiveCredits = freeElectiveGraduationResult.getTakenCredit();
double normalCultureCredits = normalCultureGraduationResult.getTakenCredit();

if (user.getStudentCategory() == StudentCategory.TRANSFER) {
if (isTransferStudent(user)) {
this.takenCredit += chapelResult.getTakenChapelCredit();
}
this.takenCredit += freeElectiveCredits + normalCultureCredits;
Expand All @@ -103,7 +103,7 @@ private void handleLeftTakenNormaCulture(
TakenLectureInventory takenLectureInventory, GraduationRequirement graduationRequirement, User user
) {
int acknowledgedCredit = 0;
if (user.getStudentCategory() == StudentCategory.TRANSFER) {
if (isTransferStudent(user)) {
acknowledgedCredit = user.getTransferCredit().getNormalCulture();
}
this.normalCultureGraduationResult = NormalCultureGraduationResult.create(
Expand All @@ -115,6 +115,9 @@ private void handleLeftTakenNormaCulture(

normalCultureGraduationResult.checkCompleted();
}
private boolean isTransferStudent(User user) {
return user.getStudentCategory() == StudentCategory.TRANSFER;
}

private void handleLeftTakenFreeElective(
TakenLectureInventory takenLectureInventory, GraduationRequirement graduationRequirement, User user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public class BusinessBasicAcademicalGraduationManager implements BasicAcademical
private static final String MANAGEMENT_INFORMATION = "경영정보학과";
private static final String INTERNATIONAL_TRADE = "국제통상학과";
private static final Set<Lecture> businessBefore20 = Set.of(
Lecture.of("KMD02114", "미시경제학원론", 3, 0, null),
Lecture.of("KMD02107", "경상통계학", 3, 0, null)
Lecture.of("KMD02114", "미시경제학원론", 3, 0, null),
Lecture.of("KMD02107", "경상통계학", 3, 0, null)
);
private static final Set<Lecture> internationBefore20 = Set.of(
Lecture.of("KMD02114", "미시경제학원론", 3, 0, null),
Lecture.of("KMD02115", "거시경제학원론", 3, 0, null)
Lecture.of("KMD02114", "미시경제학원론", 3, 0, null),
Lecture.of("KMD02115", "거시경제학원론", 3, 0, null)
);

@Override
Expand All @@ -39,9 +39,9 @@ public boolean isSatisfied(String major) {

@Override
public DetailGraduationResult createDetailGraduationResult(User user,
TakenLectureInventory takenLectureInventory,
Set<BasicAcademicalCultureLecture> graduationLectures,
int basicAcademicalCredit) {
TakenLectureInventory takenLectureInventory,
Set<BasicAcademicalCultureLecture> graduationLectures,
int basicAcademicalCredit) {

if (user.getStudentCategory() == StudentCategory.TRANSFER) {
return DetailGraduationResult.createNonCategorizedGraduationResult(
Expand All @@ -54,29 +54,29 @@ public DetailGraduationResult createDetailGraduationResult(User user,
Set<TakenLecture> finishedTakenLecture = new HashSet<>();
Set<Lecture> taken = new HashSet<>();
Set<Lecture> finalBasicAcademicalLectures = resetBasicAcademicalLectureSet(
basicAcademicalLectures,
user);
basicAcademicalLectures,
user);

takenLectureInventory.getTakenLectures()
.stream()
.filter(
takenLecture -> finalBasicAcademicalLectures.contains(takenLecture.getLecture()))
.forEach(takenLecture -> {
finishedTakenLecture.add(takenLecture);
taken.add(takenLecture.getLecture());
});
.stream()
.filter(
takenLecture -> finalBasicAcademicalLectures.contains(takenLecture.getLecture()))
.forEach(takenLecture -> {
finishedTakenLecture.add(takenLecture);
taken.add(takenLecture.getLecture());
});
takenLectureInventory.handleFinishedTakenLectures(finishedTakenLecture);

DetailCategoryResult detailCategoryResult = DetailCategoryResult.create(
"학문기초교양", true, basicAcademicalCredit);
"학문기초교양", true, basicAcademicalCredit);
detailCategoryResult.calculate(taken, basicAcademicalLectures);

return DetailGraduationResult.createNonCategorizedGraduationResult(basicAcademicalCredit,
List.of(detailCategoryResult));
List.of(detailCategoryResult));
}

private Set<Lecture> resetBasicAcademicalLectureSet(Set<Lecture> basicAcademicalLectures,
User user) {
User user) {
if (!user.checkBeforeEntryYear(TWENTY)) {
return basicAcademicalLectures;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.List;
import java.util.Set;

import com.plzgraduate.myongjigraduatebe.user.domain.model.StudentCategory;
import com.plzgraduate.myongjigraduatebe.user.domain.model.User;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -25,7 +27,7 @@ public class ElectiveMajorManager {

public DetailCategoryResult createDetailCategoryResult(
TakenLectureInventory takenLectureInventory,
Set<Lecture> electiveLectures, int electiveMajorTotalCredit
Set<Lecture> electiveLectures, int electiveMajorTotalCredit, User user
) {
Set<Lecture> takenElective = new HashSet<>();
Set<TakenLecture> finishedTakenLecture = new HashSet<>();
Expand All @@ -39,12 +41,19 @@ public DetailCategoryResult createDetailCategoryResult(
DetailCategoryResult electiveMajorResult = DetailCategoryResult.create(ELECTIVE_MAJOR_NAME,
true, electiveMajorTotalCredit
);
processTransferStudent(user, electiveMajorResult);

excludePracticeLectureForHaveToLecture(electiveLectures);
electiveMajorResult.calculate(takenElective, electiveLectures);
takenLectureInventory.handleFinishedTakenLectures(finishedTakenLecture);
return electiveMajorResult;
}

private void processTransferStudent(User user, DetailCategoryResult electiveMajorResult) {
if (user.getStudentCategory() == StudentCategory.TRANSFER) {
int transferCredit = user.getTransferCredit().getMajorLecture();
electiveMajorResult.addTakenCredits(transferCredit);
}
}
private void excludePracticeLectureForHaveToLecture(Set<Lecture> electiveLectures) {
electiveLectures.removeIf(lecture -> PRACTICE_LECTURE_CODES.contains(lecture.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.plzgraduate.myongjigraduatebe.lecture.domain.model.MajorLecture;
import com.plzgraduate.myongjigraduatebe.takenlecture.domain.model.TakenLecture;
import com.plzgraduate.myongjigraduatebe.takenlecture.domain.model.TakenLectureInventory;
import com.plzgraduate.myongjigraduatebe.user.domain.model.StudentCategory;
import com.plzgraduate.myongjigraduatebe.user.domain.model.User;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -45,11 +44,8 @@ public DetailGraduationResult createDetailGraduationResult(User user, MajorType

int electiveMajorTotalCredit =
graduationResultTotalCredit - mandantoryDetailCategoryResult.getTotalCredits();
if(user.getStudentCategory() == StudentCategory.TRANSFER){
electiveMajorTotalCredit+=user.getTransferCredit().getMajorLecture();
}
DetailCategoryResult electiveDetailCategoryResult = electiveMajorManager.createDetailCategoryResult(
takenLectureInventory, electiveLectures, electiveMajorTotalCredit);
takenLectureInventory, electiveLectures, electiveMajorTotalCredit, user);

return DetailGraduationResult.createNonCategorizedGraduationResult(
graduationResultTotalCredit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public User updateUser(UpdateStudentInformationCommand updateStudentInformationC
updateStudentInformationCommand.getSubMajor(),
updateStudentInformationCommand.getAssociatedMajor(),
updateStudentInformationCommand.getStudentCategory(),
updateStudentInformationCommand.getTransferCredit(),
updateStudentInformationCommand.getTotalCredit(),
updateStudentInformationCommand.getTakenCredit(),
updateStudentInformationCommand.isGraduate()
);
user.updateTransferCredit(updateStudentInformationCommand.getTransferCredit());
updateUserPort.updateUser(user);
return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,21 @@ public String toString() {
public void updateStudentInformation(
String name, String major, String dualMajor,
String subMajor, String associatedMajor,
StudentCategory studentCategory, int totalCredit, double takenCredit, boolean graduate
StudentCategory studentCategory, TransferCredit transferCredit, int totalCredit, double takenCredit, boolean graduate
) {
this.name = name;
this.primaryMajor = major;
this.dualMajor = dualMajor;
this.subMajor = subMajor;
this.associatedMajor = associatedMajor;
this.studentCategory = studentCategory;
updateTransferCredit(transferCredit);
this.totalCredit = totalCredit;
this.takenCredit = takenCredit;
this.graduated = graduate;
}

public void updateTransferCredit(TransferCredit transferCredit) {
private void updateTransferCredit(TransferCredit transferCredit) {
if (transferCredit != null) {
this.transferCredit = transferCredit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class UserFixture {
StudentCategory.NORMAL);
}
public static User 경제학과_20학번_편입() {
return createUser("mj1003", "1234", EnglishLevel.FREE, "최편입", "60191666", 19, "경제학과",
return createUser("mj1003", "1234", EnglishLevel.FREE, "최편입", "60191666", 20, "경제학과",
null,
StudentCategory.NORMAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,57 @@ void calculateSingleDetailGraduationForTransferPrimaryMandatory() {
.extracting("graduationCategory", "isCompleted", "totalCredit", "takenCredit")
.contains(PRIMARY_MANDATORY_MAJOR, false, 6, 3.0);
}
@DisplayName("편입생 주전공선택 졸업결과를 계산한다.")
@Test
void calculateSingleDetailGraduationForTransferPrimaryElective() {
// given
User user = User.builder()
.id(1L)
.primaryMajor("응용소프트웨어전공")
.entryYear(19)
.studentCategory(StudentCategory.TRANSFER)
.transferCredit(TransferCredit.from("0/15/0/0")) // 15 전공 선택 학점
.build();

HashSet<MajorLecture> graduationMajorLectures = new HashSet<>(
Set.of(
// 전공 필수
MajorLecture.of(Lecture.builder()
.id("HEC01211")
.credit(3)
.build(), "응용소프트웨어전공", 1, 16, 23),
// 전공 선택
MajorLecture.of(Lecture.builder()
.id("HEC01305")
.credit(3)
.build(), "응용소프트웨어전공", 0, 16, 23)
)
);
given(findMajorPort.findMajor(user.getPrimaryMajor())).willReturn(graduationMajorLectures);

HashSet<TakenLecture> takenLectures = new HashSet<>(
Set.of(
TakenLecture.builder()
.lecture(Lecture.builder()
.id("HEC01305") // 전공 선택
.credit(3)
.build())
.build()
)
);
TakenLectureInventory takenLectureInventory = TakenLectureInventory.from(takenLectures);

GraduationRequirement graduationRequirement = GraduationRequirement.builder()
.primaryMajorCredit(70) // 전공 학점 총합
.build();

// when
DetailGraduationResult detailPrimaryElectiveMajorGraduationResult = calculateMajorGraduationService.calculateSingleDetailGraduation(
user, PRIMARY_ELECTIVE_MAJOR, takenLectureInventory, graduationRequirement);

// then
assertThat(detailPrimaryElectiveMajorGraduationResult)
.extracting("graduationCategory", "isCompleted", "totalCredit", "takenCredit")
.contains(PRIMARY_ELECTIVE_MAJOR, 18.0); // 15 (편입 학점) + 3 (수강 학점)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.plzgraduate.myongjigraduatebe.user.application.service.update;

import static org.mockito.BDDMockito.then;
import static org.testng.AssertJUnit.assertEquals;

import com.plzgraduate.myongjigraduatebe.user.application.port.UpdateUserPort;
import com.plzgraduate.myongjigraduatebe.user.application.usecase.update.UpdateStudentInformationCommand;
import com.plzgraduate.myongjigraduatebe.user.domain.model.StudentCategory;
import com.plzgraduate.myongjigraduatebe.user.domain.model.TransferCredit;
import com.plzgraduate.myongjigraduatebe.user.domain.model.User;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -42,4 +44,32 @@ void updateUser() {
then(updateUserPort).should()
.updateUser(user);
}
@DisplayName("User의 TransferCredit 정보를 수정한다.")
@Test
void updateUserTransferCredit() {
// given
User user = User.builder()
.transferCredit(TransferCredit.empty())
.build();

TransferCredit newTransferCredit = new TransferCredit(3, 6, 9, 2);

UpdateStudentInformationCommand command = UpdateStudentInformationCommand.builder()
.user(user)
.name("정지환")
.major("응용소프트웨어학과")
.studentCategory(StudentCategory.TRANSFER)
.transferCredit(newTransferCredit)
.build();

// when
updateStudentInformationService.updateUser(command);

// then
then(updateUserPort).should()
.updateUser(user);

// Assert TransferCredit is updated
assertEquals(newTransferCredit, user.getTransferCredit());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.plzgraduate.myongjigraduatebe.graduation.domain.model.DefaultGraduationRequirementType;
import com.plzgraduate.myongjigraduatebe.graduation.domain.model.GraduationRequirement;
import java.util.NoSuchElementException;

import com.plzgraduate.myongjigraduatebe.graduation.domain.model.TransferGraduationRequirementType;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -91,4 +93,26 @@ void convertToProfitGraduationRequirementWithSubMajorUser() {
assertThat(graduationRequirement.getFreeElectiveCredit()).isZero();

}
@DisplayName("편입학생의 졸업요건을 결정한다.")
@Test()
void convertToProfitGraduationRequirementWithTransferStudent() {
//given
User transferStudent = UserFixture.경제학과_20학번_편입();
College socialScience = College.SOCIAL_SCIENCE;
DefaultGraduationRequirementType defaultGraduationRequirementType = DefaultGraduationRequirementType.determineGraduationRequirement(
socialScience, transferStudent);

//when
GraduationRequirement graduationRequirement = defaultGraduationRequirementType.convertToProfitGraduationRequirement(
transferStudent);

// then
TransferGraduationRequirementType transferRequirement = TransferGraduationRequirementType.findByCollegeName(socialScience.getName());

assertThat(graduationRequirement.getTotalCredit()).isEqualTo(defaultGraduationRequirementType.getTotalCredit());
assertThat(graduationRequirement.getPrimaryMajorCredit()).isEqualTo(defaultGraduationRequirementType.getMajorLectureCredit());
assertThat(transferRequirement.getCombinedCultureCredit()).isEqualTo(51);
assertThat(transferRequirement.getChristianCredit()).isEqualTo(2);
assertThat(graduationRequirement.getFreeElectiveCredit()).isEqualTo(defaultGraduationRequirementType.getFreeElectiveLectureCredit());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void create() {
@Test
void updateStudentInformation() {
//given //when
user.updateStudentInformation("테스터2", "경영학과", null, null, null, StudentCategory.CHANGE_MAJOR, 134,
user.updateStudentInformation("테스터2", "경영학과", null, null, null, StudentCategory.CHANGE_MAJOR, null,134,
120.5, true);
//then
assertThat(user)
Expand Down

0 comments on commit 35c7bf8

Please sign in to comment.