Skip to content

Commit

Permalink
연장한 대출의 반납 일자 변경하는 책임을 Period로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
shine-17 committed Jan 20, 2025
1 parent 7a89eb5 commit 23ec6b3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
26 changes: 15 additions & 11 deletions src/main/java/com/study/bookcafe/domain/borrow/Borrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,9 @@ public boolean haveExtendableCount() {
* 대출을 연장한다.
*/
public void extend() {
// 연장 가능한 횟수가 남아있지 않으므로 불가
if (!haveExtendableCount()) return;

// 도서에 예약이 있으므로 불가
if (this.getBook().haveReservation()) return;
if (!canExtend()) return;

// 대출 연장이 가능한 날짜가 아니므로 불가
if (!isPassHalfofPeriod()) return;

LocalDate from = this.getPeriod().getFrom();
LocalDate extendedTo = this.getPeriod().getTo().plusWeeks(1);
Period extendedPeriod = new Period(from, extendedTo);
Period extendedPeriod = this.getPeriod().getExtended(this.getMember().getLevel());

extendPeriod(extendedPeriod);
increaseExtendCount();
Expand All @@ -91,4 +82,17 @@ public boolean isPassHalfofPeriod() {

return now.isEqual(targetDate) || now.isAfter(targetDate);
}

private boolean canExtend() {
// 연장 가능한 횟수가 남아있지 않으므로 불가
if (!haveExtendableCount()) return false;

// 도서에 예약이 있으므로 불가
if (this.getBook().haveReservation()) return false;

// 대출 연장이 가능한 날짜가 아니므로 불가
if (!isPassHalfofPeriod()) return false;

return true;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/study/bookcafe/domain/borrow/Period.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public static Period of(@NonNull LocalDate from, Level level) {
return new Period(from, level);
}

public Period getExtended(Level level) {

This comment has been minimized.

Copy link
@f-lab-tomo

f-lab-tomo Jan 20, 2025

Collaborator

Period 는 좀 더 범용적인 개념이고 Level 은 꽤 도메인 개념입니다.

직접 협력하는 방법도 좋고, 혹은 Period 는 더 범용적이도록 배려할 수도 있습니다. 어떠세요?

return new Period(from, this.to.plusWeeks(level.getExtendPeriod()));

This comment has been minimized.

Copy link
@f-lab-tomo

f-lab-tomo Jan 20, 2025

Collaborator

오 불변성 유지 좋습니다. 메서드 명 말인데 get 을 붙이는 건 나쁘진 않지만 좀 더 편한 문체가 되도록 한번만 다듬어볼까요?

비슷하게 날짜나 불변을 다루는 다른 객체들을 참고해보는 것도 좋습니다

}

}
14 changes: 9 additions & 5 deletions src/main/java/com/study/bookcafe/domain/member/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@

public enum Level {

LIBRARIAN(2, 10,null, 1, 1), // 사서 회원
WORM(1, 5,null, 1, 1), // 책벌레 회원
BASIC(0, 3, WORM, 1, 1); // 일반 회원
LIBRARIAN(2, 10,null, 1, 2, 1), // 사서 회원
WORM(1, 5,null, 1, 1, 1), // 책벌레 회원
BASIC(0, 3, WORM, 1, 1, 1); // 일반 회원

private final int value; // 등급 값
private final int maximumBorrowCount; // 최대 대출 권수
private final Level next; // 다음 등급
private final int maximumExtendCount; // 최대 대출 연장 횟수

@Getter
private final int borrowPeriod; // 대출 기간 (week)
@Getter
private final int borrowPeriod; // 대출 연장 기간
private final int extendPeriod; // 연장 기간 (week)

Level(int value, int maximumBorrowCount, Level next, int maximumExtendCount, int borrowPeriod) {
Level(int value, int maximumBorrowCount, Level next, int maximumExtendCount, int borrowPeriod, int extendPeriod) {
this.value = value;
this.maximumBorrowCount = maximumBorrowCount;
this.next = next;
this.maximumExtendCount = maximumExtendCount;
this.borrowPeriod = borrowPeriod;
this.extendPeriod = extendPeriod;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/study/bookcafe/borrow/PeriodTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.study.bookcafe.borrow;

import com.study.bookcafe.domain.borrow.Period;
import com.study.bookcafe.domain.member.Level;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -29,4 +30,23 @@ public void ofExceptionTest() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new Period(date2, date1));
Assertions.assertThrows(NullPointerException.class, () -> new Period(null, null));
}

@Test
@DisplayName("회원 등급에 따라 대출 연장 기간을 확인한다,")
public void checkBorrowExtensionsPeriodByMemberShipLevel() {
LocalDate borrowDate = LocalDate.now();

Period basicExtendedPeriod = Period.of(borrowDate, Level.BASIC).getExtended(Level.BASIC);
Period wormExtendedPeriod = Period.of(borrowDate, Level.WORM).getExtended(Level.WORM);
Period librarianExtendedPeriod = Period.of(borrowDate, Level.LIBRARIAN).getExtended(Level.LIBRARIAN);

Period newBasicPeriod = new Period(borrowDate, borrowDate.plusWeeks(Level.BASIC.getBorrowPeriod()).plusWeeks(Level.BASIC.getExtendPeriod()));
Period newWormPeriod = new Period(borrowDate, borrowDate.plusWeeks(Level.WORM.getBorrowPeriod()).plusWeeks(Level.WORM.getExtendPeriod()));
Period newLibrarianPeriod = new Period(borrowDate, borrowDate.plusWeeks(Level.LIBRARIAN.getBorrowPeriod()).plusWeeks(Level.LIBRARIAN.getExtendPeriod()));

assertThat(basicExtendedPeriod).isEqualTo(newBasicPeriod);
assertThat(wormExtendedPeriod).isEqualTo(newWormPeriod);
assertThat(librarianExtendedPeriod).isEqualTo(newLibrarianPeriod);

}
}

0 comments on commit 23ec6b3

Please sign in to comment.