Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/nextstep/courses/domain/Course.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package nextstep.courses.domain;

import nextstep.courses.domain.session.Session;
import nextstep.courses.domain.session.Sessions;

import java.time.LocalDateTime;

public class Course {
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/nextstep/courses/domain/CoverImage.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/nextstep/courses/domain/Enrollment.java

This file was deleted.

48 changes: 0 additions & 48 deletions src/main/java/nextstep/courses/domain/Session.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nextstep.courses.domain;
package nextstep.courses.domain.enrollment;

public class Capacity {

Expand All @@ -23,4 +23,8 @@ public void validateAvailable() {
public void increase() {
this.current++;
}

public int value() {
return max;
}
}
36 changes: 36 additions & 0 deletions src/main/java/nextstep/courses/domain/enrollment/Enrollment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nextstep.courses.domain.enrollment;

import java.time.LocalDateTime;

public class Enrollment {
private final Long sessionId;
private final Long userId;
private final LocalDateTime enrollmentDate;

public Enrollment(Long sessionId, Long userId) {
this(sessionId, userId, LocalDateTime.now());
}

public Enrollment(Long sessionId, Long userId, LocalDateTime enrollmentDate) {
this.sessionId = sessionId;
this.userId = userId;
this.enrollmentDate = enrollmentDate;
}

public boolean isSameUser(Long userId) {
return this.userId.equals(userId);
}

public Long getSessionId() {
return sessionId;
}


public Long getUserId() {
return userId;
}

public LocalDateTime getEnrollmentDate() {
return enrollmentDate;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package nextstep.courses.domain;
package nextstep.courses.domain.enrollment;

import nextstep.payments.domain.Payment;

public interface EnrollmentPolicy {
PolicyType type();
Long price();
void validateEnrollment(Payment payment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nextstep.courses.domain.enrollment;

import static nextstep.courses.domain.enrollment.PolicyType.FREE;
import static nextstep.courses.domain.enrollment.PolicyType.PAID;

public class EnrollmentPolicyFactory {
public static EnrollmentPolicy create(String name, Long price) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnrollmentPolicy 생성을 PolicyType이 담당하기 보다 별도의 EnrollmentPolicyFactory와 같은 객체를 추가하는 것은 어떨까?

PolicyType type = PolicyType.valueOf(name);

if (type == FREE) {
return new FreeEnrollmentPolicy();
}

if (type == PAID) {
return new PaidEnrollmentPolicy(new Money(price));
}

throw new IllegalArgumentException();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nextstep.courses.domain;
package nextstep.courses.domain.enrollment;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,7 +25,7 @@ public void validateEnroll(Long userId) {
capacity.validateAvailable();
}

public Enrollment add(Long sessionId, Long userId) {
public Enrollment enroll(Long sessionId, Long userId) {
validateEnroll(userId);

Enrollment enrollment = new Enrollment(sessionId, userId);
Expand All @@ -39,6 +39,10 @@ private boolean EnrolledCheck(Long userId) {
return enrollments.stream()
.anyMatch(enrollment -> enrollment.isSameUser(userId));
}

public int getCapacity() {
return capacity.value();
}
}


Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package nextstep.courses.domain;
package nextstep.courses.domain.enrollment;

import nextstep.payments.domain.Payment;

public class FreeEnrollmentPolicy implements EnrollmentPolicy{
@Override
public PolicyType type() {
return PolicyType.FREE;
}

@Override
public Long price() {
return null;
}

@Override
public void validateEnrollment(Payment payment) {
// 무료강의는 검증하지 않는다
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nextstep.courses.domain;
package nextstep.courses.domain.enrollment;

import java.util.Objects;

Expand All @@ -13,6 +13,10 @@ public boolean isEqualTo(Money other) {
return this.equals(other);
}

public long value() {
return amount;
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nextstep.courses.domain;
package nextstep.courses.domain.enrollment;

import nextstep.payments.domain.Payment;

Expand All @@ -10,6 +10,16 @@ public PaidEnrollmentPolicy(Money money) {
this.money = money;
}

@Override
public PolicyType type() {
return PolicyType.PAID;
}

@Override
public Long price() {
return money.value();
}

@Override
public void validateEnrollment(Payment payment) {
validatePayment(payment);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nextstep.courses.domain.enrollment;

public enum PolicyType {
FREE,
PAID
}
112 changes: 112 additions & 0 deletions src/main/java/nextstep/courses/domain/session/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package nextstep.courses.domain.session;

import nextstep.courses.domain.enrollment.Enrollment;
import nextstep.courses.domain.enrollment.EnrollmentPolicy;
import nextstep.courses.domain.enrollment.Enrollments;
import nextstep.courses.domain.session.cover.CoverImage;
import nextstep.payments.domain.Payment;

import java.time.LocalDateTime;

public class Session {
private final long id;
private final long courseId;
private final SessionDuration sessionDuration;
private final CoverImage coverImage;
private final EnrollmentPolicy enrollmentPolicy;
private final SessionState sessionState;
private final Enrollments enrollments;

public Session(long id
, long courseId
, LocalDateTime startDate
, LocalDateTime endDate
, int size
, String fileName
, int width
, int height
, EnrollmentPolicy enrollmentPolicy
, SessionState sessionState
, Enrollments enrollments) {

this(id, courseId, new SessionDuration(startDate, endDate), new CoverImage(size, fileName, width, height)
, enrollmentPolicy, sessionState, enrollments);
}

public Session(long id, long courseId, SessionDuration sessionDuration, CoverImage coverImage
, EnrollmentPolicy enrollmentPolicy, SessionState sessionState, Enrollments enrollments) {
this.id = id;
this.courseId = courseId;
this.sessionDuration = sessionDuration;
this.coverImage = coverImage;
this.enrollmentPolicy = enrollmentPolicy;
this.sessionState = sessionState;
this.enrollments = enrollments;
}

public Enrollment enroll(Long userId, Payment payment) {
sessionState.validateEnroll();
enrollmentPolicy.validateEnrollment(payment);
return enrollments.enroll(this.id, userId);
}

public long getId() {
return id;
}

public long getCourseId() {
return courseId;
}

public LocalDateTime getStartDate() {
return sessionDuration.getStartDate();
}

public LocalDateTime getEndDate() {
return sessionDuration.getEndDate();
}

public int getCoverImageSize() {
return coverImage.getImageSize();
}

public String getCoverImageName() {
return coverImage.getImageName();
}

public int getCoverImageWidth() {
return coverImage.getCoverImageWidth();
}

public int getCoverImageHeight() {
return coverImage.getCoverImageHeight();
}

public String getPolicyType() {
return enrollmentPolicy.type().name();
}

public long getPrice() {
return enrollmentPolicy.price();
}

public String getState() {
return sessionState.name();
}

public int getCapacity() {
return enrollments.getCapacity();
}

@Override
public String toString() {
return "Session{" +
"id=" + id +
", sessionDuration=" + sessionDuration +
", coverImage=" + coverImage +
", enrollmentPolicy=" + enrollmentPolicy +
", sessionState=" + sessionState +
", enrollments=" + enrollments +
'}';
}
}
Loading