-
Notifications
You must be signed in to change notification settings - Fork 300
step3 #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
step3 #386
Changes from all commits
821c7bf
53740e3
b83b6ca
cb25131
40b28a5
e6385aa
48a48db
e6c0821
386422c
51a9602
7028a0b
c3a906e
be7549b
766f65b
f1362b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package nextstep.courses.domain; | ||
|
||
import nextstep.courses.exception.BusinessInvalidValueException; | ||
|
||
public class Capacity { | ||
private final int capacity; | ||
|
||
public Capacity(int capacity) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수강 인원이 1명 이상일 때 의미가 있기 때문에 1이상의 값인지에 대한 검증 로직도 필요하지 않을까? |
||
this.capacity = capacity; | ||
} | ||
|
||
public void validateCapacity(int count) { | ||
if (count >= capacity) { | ||
throw new BusinessInvalidValueException("최대수강인원을 초과했습니다."); | ||
} | ||
} | ||
|
||
public int capacity() { | ||
return capacity; | ||
} | ||
} | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package nextstep.courses.domain; | ||
|
||
import nextstep.courses.exception.BusinessInvalidValueException; | ||
|
||
public class Price { | ||
private final long price; | ||
|
||
public Price(long price) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 강의료도 1000원 이상과 같이 본인만의 정책을 만들고 유효성 처리하도록 구현하는 것은 어떨까? |
||
this.price = price; | ||
} | ||
|
||
public void validatePrice(long price) { | ||
if (this.price != price) { | ||
throw new BusinessInvalidValueException("강의 가격이 변동되었습니다."); | ||
} | ||
} | ||
|
||
public long price() { | ||
return price; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
package nextstep.courses.domain; | ||
|
||
import nextstep.courses.utils.BaseEntity; | ||
import nextstep.users.domain.NsUser; | ||
|
||
public class Registration { | ||
import java.time.LocalDateTime; | ||
|
||
public Registration() { | ||
public class Registration extends BaseEntity { | ||
private Long id = null; | ||
private final NsUser user; | ||
private final Session session; | ||
private final Long amount; | ||
|
||
public Registration(Long id, NsUser user, Session session, Long amount, LocalDateTime createdAt, LocalDateTime updatedAt) { | ||
this.id = id; | ||
this.user = user; | ||
this.session = session; | ||
this.amount = amount; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
} | ||
|
||
public Registration(NsUser user, Session session, Long amount) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 부생성자가 주생성자 앞에 오는 것이 관례임 |
||
this.user = user; | ||
this.session = session; | ||
this.amount = amount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 부생성자는 주생성자를 호출해 필드의 값을 초기화하는 것을 추천 |
||
} | ||
|
||
public void register(NsUser user, Session session, Long amount) { | ||
public static Registration register(NsUser user, Session session, Long amount) { | ||
Registration registration = new Registration(user, session, amount); | ||
session.enroll(user, amount); | ||
return registration; | ||
} | ||
|
||
public Long id() { | ||
return id; | ||
} | ||
|
||
public NsUser nsUser() { | ||
return user; | ||
} | ||
|
||
public Session session() { | ||
return session; | ||
} | ||
|
||
public Long amount() { | ||
return amount; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package nextstep.courses.domain; | ||
|
||
import nextstep.courses.utils.BaseEntity; | ||
|
||
public class Semester extends BaseEntity { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 객체는 Course를 가지는 것 이외에 아무 역할도 담당하고 있지 않다. |
||
private final Course course; | ||
|
||
public Semester(Course course) { | ||
this.course = course; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,40 +2,117 @@ | |
|
||
import nextstep.courses.enums.SessionStatus; | ||
import nextstep.courses.exception.BusinessInvalidValueException; | ||
import nextstep.courses.utils.BaseEntity; | ||
import nextstep.users.domain.NsUser; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public abstract class Session { | ||
final Long id; | ||
final Course course; | ||
final SessionCover sessionCover; | ||
final Period period; | ||
SessionStatus status = SessionStatus.PREPARE; | ||
final List<NsUser> participants = new ArrayList<>(); | ||
public class Session extends BaseEntity { | ||
public static long FREE_PRICE = 0L; | ||
public static int MAX_CAPACITY = Integer.MAX_VALUE; | ||
private final Long id; | ||
private Course course; | ||
private SessionCover sessionCover; | ||
private Period period; | ||
private Capacity capacity; | ||
private Price price; | ||
|
||
public Session(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionCover sessionCover, Course course) { | ||
private SessionStatus status = SessionStatus.PREPARE; | ||
private List<NsUser> participants; | ||
|
||
public Session(Long id) { | ||
this.id = id; | ||
} | ||
|
||
private Session(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionCover sessionCover, Course course, Capacity capacity, Price price, List<NsUser> participants) { | ||
this.id = id; | ||
this.period = new Period(beginDt, endDt); | ||
this.sessionCover = sessionCover; | ||
this.course = course; | ||
this.capacity = capacity; | ||
this.price = price; | ||
this.participants = participants; | ||
} | ||
|
||
public Session(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionStatus status, Capacity capacity, Price price, Course course, SessionCover sessionCover, LocalDateTime createdAt, LocalDateTime updatedAt) { | ||
this.id = id; | ||
this.period = new Period(beginDt, endDt); | ||
this.status = status; | ||
this.sessionCover = sessionCover; | ||
this.course = course; | ||
this.capacity = capacity; | ||
this.price = price; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
} | ||
|
||
public abstract void enroll(NsUser participant, Long amount); | ||
public static Session fromSessionForFree(Session session, SessionCover sessionCover, Course course, List<NsUser> participants) { | ||
return Session.ofFree(session.id, session.period.getBeginDt(), session.period.getEndDt(), sessionCover, course, participants); | ||
} | ||
|
||
public static Session fromSessionForPaid(Session session, SessionCover sessionCover, Course course, List<NsUser> participants) { | ||
return Session.ofPaid(session.id, session.period.getBeginDt(), session.period.getEndDt(), sessionCover, course, session.price.price(), session.capacity.capacity(), participants); | ||
} | ||
|
||
public static Session ofFree(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionCover sessionCover, Course course, List<NsUser> participants) { | ||
return new Session(id, beginDt, endDt, sessionCover, course, new Capacity(MAX_CAPACITY), new Price(FREE_PRICE), participants); | ||
} | ||
|
||
public static Session ofPaid(Long id, LocalDateTime beginDt, LocalDateTime endDt, SessionCover sessionCover, Course course, Long price, Integer capacity, List<NsUser> participants) { | ||
return new Session(id, beginDt, endDt, sessionCover, course, new Capacity(capacity), new Price(price), participants); | ||
} | ||
|
||
public static Session fromSession(Session session, SessionCover sessionCover, Course course, List<NsUser> participants) { | ||
if (session.price.equals(0L)) { | ||
return fromSessionForFree(session, sessionCover, course, participants); | ||
} | ||
return fromSessionForPaid(session, sessionCover, course, participants); | ||
} | ||
|
||
public void startEnrollment() { | ||
this.status = SessionStatus.ENROLL; | ||
} | ||
|
||
public void validateStatus() { | ||
public void enroll(NsUser participant, Long amount) { | ||
Hyesooo marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메서드의 로직을 Enrollment와 같은 도메인 객체를 추가해 로직을 분리해 보는 것은 어떨까? |
||
capacity.validateCapacity(participants.size()); | ||
price.validatePrice(amount); | ||
validateStatus(); | ||
this.participants.add(participant); | ||
} | ||
|
||
|
||
private void validateStatus() { | ||
if (SessionStatus.ENROLL != this.status) { | ||
throw new BusinessInvalidValueException("수강신청 가능한 상태가 아닙니다."); | ||
} | ||
} | ||
|
||
public Price price() { | ||
return price; | ||
} | ||
|
||
public Capacity capacity() { | ||
return capacity; | ||
} | ||
|
||
public Long id() { | ||
return id; | ||
} | ||
|
||
public Period period() { | ||
return period; | ||
} | ||
|
||
public Course course() { | ||
return course; | ||
} | ||
|
||
public SessionCover sessionCover() { | ||
return sessionCover; | ||
} | ||
|
||
public String status() { | ||
return this.status.name(); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.