Skip to content

Commit 8ffdca6

Browse files
author
heewonham
committed
refactor : (review) period 객체 추출
1 parent a4837f4 commit 8ffdca6

File tree

6 files changed

+95
-53
lines changed

6 files changed

+95
-53
lines changed

src/main/java/nextstep/courses/domain/FreeSession.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import nextstep.payments.domain.Payment;
44

5-
import java.time.LocalDate;
6-
75
public class FreeSession extends Session {
86

9-
public FreeSession(Long id, String name, LocalDate startDate, LocalDate endDate, Image coverImage, LectureStatus status) {
10-
super(id, name, startDate, endDate, coverImage, status);
7+
public FreeSession(Long id, String name, Period period, Image coverImage, SessionStatus status) {
8+
super(id, name, period, coverImage, status);
119
}
1210

1311
@Override

src/main/java/nextstep/courses/domain/PaidSession.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
import nextstep.payments.domain.Payment;
44

5-
import java.time.LocalDate;
6-
75
public class PaidSession extends Session {
86
private Capacity maxCapacity;
97
private TuitionFee tuitionFee;
108

11-
public PaidSession(Long id, String name, LocalDate startDate, LocalDate endDate, Image coverImage,
12-
LectureStatus status, int maxCapacity, Long tuitionFee) {
13-
super(id, name, startDate, endDate, coverImage, status);
9+
public PaidSession(Long id, String name, Period period, Image coverImage,
10+
SessionStatus status, int maxCapacity, int tuitionFee) {
11+
super(id, name, period, coverImage, status);
1412
this.maxCapacity = new Capacity(maxCapacity);
1513
this.tuitionFee = new TuitionFee(tuitionFee);
1614
}
1715

1816
@Override
1917
protected void validateRegistration(Long studentId, Payment payment) {
20-
if (registeredStudents.size() >= maxCapacity) {
18+
if (registeredStudents.size() >= maxCapacity.getValue()) {
2119
throw new IllegalStateException("수강 인원을 초과하였습니다.");
2220
}
2321
if (payment == null) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package nextstep.courses.domain;
2+
3+
import java.time.LocalDate;
4+
5+
public class Period {
6+
private final LocalDate startDate;
7+
private final LocalDate endDate;
8+
9+
public Period(LocalDate startDate, LocalDate endDate) {
10+
if (endDate.isBefore(startDate) || endDate.isEqual(startDate)) {
11+
throw new IllegalArgumentException("종료일은 시작일보다 이후여야 합니다.");
12+
}
13+
this.startDate = startDate;
14+
this.endDate = endDate;
15+
}
16+
17+
public LocalDate getStartDate() {
18+
return startDate;
19+
}
20+
21+
public LocalDate getEndDate() {
22+
return endDate;
23+
}
24+
}

src/main/java/nextstep/courses/domain/Session.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22

33
import nextstep.payments.domain.Payment;
44

5-
import java.time.LocalDate;
65
import java.util.HashSet;
76
import java.util.Set;
87
public abstract class Session {
98
protected Long id;
109
protected String name;
11-
protected LocalDate startDate;
12-
protected LocalDate endDate;
10+
protected Period period;
1311
protected Image coverImage;
14-
protected LectureStatus status;
12+
protected SessionStatus status;
1513
protected Set<Long> registeredStudents = new HashSet<>();
16-
public Session(Long id, String name, LocalDate startDate, LocalDate endDate, Image coverImage, LectureStatus status) {
14+
15+
public Session(Long id, String name, Period period, Image coverImage, SessionStatus status) {
1716
this.id = id;
1817
this.name = name;
19-
this.startDate = startDate;
20-
this.endDate = endDate;
18+
this.period = period;
2119
this.coverImage = coverImage;
2220
this.status = status;
2321
}
2422

2523
public void register(Long studentId, Payment payment) {
26-
if (status != LectureStatus.RECRUITING) {
24+
if (status != SessionStatus.RECRUITING) {
2725
throw new IllegalStateException("수강 신청은 모집중일 때만 가능합니다.");
2826
}
2927

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package nextstep.qna.domain;
2+
3+
import nextstep.courses.domain.Period;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.time.LocalDate;
8+
9+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
10+
11+
class PeriodTest {
12+
@Test
13+
@DisplayName("종료일이 시작일보다 이르거나 같으면 예외가 발생한다")
14+
void invalidPeriodThrowsException() {
15+
LocalDate startDate = LocalDate.of(2025, 4, 10);
16+
LocalDate endDateSame = LocalDate.of(2025, 4, 10);
17+
LocalDate endDateBefore = LocalDate.of(2025, 4, 9);
18+
19+
assertThatThrownBy(() -> new Period(startDate, endDateSame))
20+
.isInstanceOf(IllegalArgumentException.class)
21+
.hasMessage("종료일은 시작일보다 이후여야 합니다.");
22+
23+
assertThatThrownBy(() -> new Period(startDate, endDateBefore))
24+
.isInstanceOf(IllegalArgumentException.class)
25+
.hasMessage("종료일은 시작일보다 이후여야 합니다.");
26+
}
27+
}
Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package nextstep.qna.domain;
22

3-
import nextstep.courses.domain.FreeSession;
4-
import nextstep.courses.domain.LectureStatus;
5-
import nextstep.courses.domain.PaidSession;
6-
import nextstep.courses.domain.Session;
3+
import nextstep.courses.domain.*;
74
import nextstep.payments.domain.Payment;
85
import org.junit.jupiter.api.DisplayName;
96
import org.junit.jupiter.api.Test;
@@ -20,10 +17,9 @@ void notRegisterSession() {
2017
Session session = new FreeSession(
2118
1L,
2219
"자바지기",
23-
LocalDate.now(),
24-
LocalDate.now(),
20+
new Period(LocalDate.now(), LocalDate.now().plusDays(1)),
2521
null,
26-
LectureStatus.CLOSED
22+
SessionStatus.CLOSED
2723
);
2824
assertThatThrownBy(() -> {
2925
session.register(1L, new Payment());
@@ -35,10 +31,9 @@ void registerSession() {
3531
Session session = new FreeSession(
3632
1L,
3733
"자바지기",
38-
LocalDate.now(),
39-
LocalDate.now(),
34+
new Period(LocalDate.now(), LocalDate.now().plusDays(1)),
4035
null,
41-
LectureStatus.RECRUITING
36+
SessionStatus.RECRUITING
4237
);
4338
session.register(1L, new Payment());
4439
assertThat(session.isRegistered(1L)).isTrue();
@@ -50,19 +45,24 @@ void paidSession() {
5045
Session session = new PaidSession(
5146
1L,
5247
"자바지기",
53-
LocalDate.now(),
54-
LocalDate.now(),
48+
new Period(LocalDate.now(), LocalDate.now().plusDays(1)),
5549
null,
56-
LectureStatus.RECRUITING,
57-
0,
58-
10000L
50+
SessionStatus.RECRUITING,
51+
1,
52+
10000
5953
);
6054
assertThatThrownBy(() -> {
6155
session.register(11L, new Payment(
6256
"1",
6357
1L,
6458
11L,
65-
10000L
59+
10000
60+
));
61+
session.register(11L, new Payment(
62+
"1",
63+
1L,
64+
11L,
65+
10000
6666
));
6767
}).isInstanceOf(IllegalStateException.class).hasMessage("수강 인원을 초과하였습니다.");
6868
}
@@ -73,12 +73,11 @@ void paidSessionWithoutPayment() {
7373
Session session = new PaidSession(
7474
1L,
7575
"자바지기",
76-
LocalDate.now(),
77-
LocalDate.now(),
76+
new Period(LocalDate.now(), LocalDate.now().plusDays(1)),
7877
null,
79-
LectureStatus.RECRUITING,
78+
SessionStatus.RECRUITING,
8079
10,
81-
10000L
80+
10000
8281
);
8382
assertThatThrownBy(() -> {
8483
session.register(11L, null);
@@ -91,19 +90,18 @@ void paidSessionWithDifferentPayment() {
9190
Session session = new PaidSession(
9291
1L,
9392
"자바지기",
94-
LocalDate.now(),
95-
LocalDate.now(),
93+
new Period(LocalDate.now(), LocalDate.now().plusDays(1)),
9694
null,
97-
LectureStatus.RECRUITING,
95+
SessionStatus.RECRUITING,
9896
10,
99-
10000L
97+
10000
10098
);
10199
assertThatThrownBy(() -> {
102100
session.register(11L, new Payment(
103101
"1",
104102
2L,
105103
11L,
106-
10000L
104+
10000
107105
));
108106
}).isInstanceOf(IllegalArgumentException.class).hasMessage("결제한 강의와 일치하지 않습니다.");
109107
}
@@ -114,19 +112,18 @@ void paidSessionWithDifferentStudent() {
114112
Session session = new PaidSession(
115113
1L,
116114
"자바지기",
117-
LocalDate.now(),
118-
LocalDate.now(),
115+
new Period(LocalDate.now(), LocalDate.now().plusDays(1)),
119116
null,
120-
LectureStatus.RECRUITING,
117+
SessionStatus.RECRUITING,
121118
10,
122-
10000L
119+
10000
123120
);
124121
assertThatThrownBy(() -> {
125122
session.register(11L, new Payment(
126123
"1",
127124
1L,
128125
12L,
129-
10000L
126+
10000
130127
));
131128
}).isInstanceOf(IllegalArgumentException.class).hasMessage("결제한 사용자와 일치하지 않습니다.");
132129
}
@@ -137,20 +134,20 @@ void paidSessionWithDifferentTuitionFee() {
137134
Session session = new PaidSession(
138135
1L,
139136
"자바지기",
140-
LocalDate.now(),
141-
LocalDate.now(),
137+
new Period(LocalDate.now(), LocalDate.now().plusDays(1)),
142138
null,
143-
LectureStatus.RECRUITING,
139+
SessionStatus.RECRUITING,
144140
10,
145-
10000L
141+
10000
146142
);
147143
assertThatThrownBy(() -> {
148144
session.register(11L, new Payment(
149145
"1",
150146
1L,
151147
11L,
152-
20000L
148+
20000
153149
));
154150
}).isInstanceOf(IllegalArgumentException.class).hasMessage("결제 금액과 일치하지 않습니다.");
155151
}
152+
156153
}

0 commit comments

Comments
 (0)