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
4 changes: 4 additions & 0 deletions src/main/java/nextstep/courses/domain/Capacity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ private void validation(int capacity) {
public boolean isFull(int enrolledCount) {
return capacity <= enrolledCount;
}

public int getCapacity() {
return capacity;
}
}
34 changes: 19 additions & 15 deletions src/main/java/nextstep/courses/domain/Enrollment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,56 @@
import java.util.Objects;

public class Enrollment {
private final Long id;
private final Long studentId;
private final Long sessionId;
private final Money money;

public Enrollment(Long studentId, Long sessionId, int money) {
this(studentId, sessionId, new Money(money));
public Enrollment(Long studentId, Long sessionId) {
this(1L, studentId, sessionId);
}

public Enrollment(Long studentId, Long sessionId, Money money) {
public Enrollment(Long id, Long studentId, Long sessionId) {
this.id = id;
this.studentId = studentId;
this.sessionId = sessionId;
this.money = money;
}

public Money getMoney() {
return money;
}

public boolean sameSession(Long id) {
return this.sessionId.equals(id);
}

public void validateBelongsTo(Session session) {
if (!sameSession(session.getId())) {
public void validateBelongsTo(Long sessionId) {
if (!sameSession(sessionId)) {
throw new IllegalArgumentException("신청하고자 하는 강의가 아닙니다.");
}
}

public Long getStudentId() {
return studentId;
}

public Long getSessionId() {
return sessionId;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Enrollment that = (Enrollment) o;
return Objects.equals(studentId, that.studentId) && Objects.equals(sessionId, that.sessionId) && Objects.equals(money, that.money);
return Objects.equals(id, that.id) && Objects.equals(studentId, that.studentId) && Objects.equals(sessionId, that.sessionId);
}

@Override
public int hashCode() {
return Objects.hash(studentId, sessionId, money);
return Objects.hash(id, studentId, sessionId);
}

@Override
public String toString() {
return "Enrollment{" +
"studentId=" + studentId +
"id=" + id +
", studentId=" + studentId +
", sessionId=" + sessionId +
", money=" + money +
'}';
}
}
2 changes: 1 addition & 1 deletion src/main/java/nextstep/courses/domain/EnrollmentRule.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nextstep.courses.domain;

public interface EnrollmentRule {
Copy link
Contributor

Choose a reason for hiding this comment

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

인터페이스에 너무 많은 메서드를 제공하고 있음.
이름 또한 EnrollmentRule이라 수강 신청 규칙을 판단하는 메서드만 가질 것으로 판단되는데 규칙 외의 메서드도 가지고 있는 것으로 판단됨
인터페이스의 메서드를 최소화하기 위한 설계 개선을 시도해 본다.

void validate(int money, int enrolledCount);
void validate(Money money, int enrolledCount);

void validateMoney(Money money);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package nextstep.courses.domain;

public class FreeEnrollmentRule implements EnrollmentRule {

public FreeEnrollmentRule() {

}

@Override
public void validate(int money, int enrolledCount) {
public void validate(Money money, int enrolledCount) {
// 무료 강의는 아무 제한 없음
}

Expand Down
61 changes: 56 additions & 5 deletions src/main/java/nextstep/courses/domain/ImageFile.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
package nextstep.courses.domain;

import java.util.Objects;

public class ImageFile {
private static final long MAX_SIZE = 1024 * 1024; // 1MB
private static final int MIN_WIDTH_PX = 300;
private static final int MIN_HEIGHT_PX = 200;
private static final int WIDTH_RATIO = 3;
private static final int HEIGHT_RATIO = 2;

private long size;
private ImageType imageType;
private int width;
private int height;
private Long id;
private final long size;
private final ImageType imageType;
private final int width;
private final int height;


public ImageFile(long size) {
this(size, "png", 300, 200);
this(1L, size, "png", MIN_WIDTH_PX , MIN_HEIGHT_PX);
}

public ImageFile(long size, String imageType, int width, int height) {
this(1L, size, imageType, width, height);
}

public ImageFile(Long id, long size, String imageType, int width, int height) {
validateSize(size);
validateDimensions(width, height);

ImageType type = ImageType.getType(imageType);
validateImageType(type);

this.id = id;
this.size = size;
this.imageType = type;
this.width = width;
this.height = height;
}

public long getSize() {
return this.size;
}

public ImageType getImageType() {
return this.imageType;
}

public int getWidth() {
return this.width;
}

public int getHeight() {
return this.height;
}

private void validateImageType(ImageType type) {
if (type == ImageType.UNKNOWN) {
throw new IllegalArgumentException("지원하지 않는 이미지 타입입니다. (허용 형식: gif, jpg/jpeg, png, svg)");
Expand Down Expand Up @@ -61,4 +85,31 @@ private void validateSize(long size) {
throw new IllegalArgumentException("이미지 파일 크기는 1MB 이하여야 합니다. (현재: " + size + ")");
}
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
ImageFile imageFile = (ImageFile) o;
return size == imageFile.size && width == imageFile.width && height == imageFile.height && Objects.equals(id, imageFile.id) && imageType == imageFile.imageType;
}

@Override
public int hashCode() {
return Objects.hash(id, size, imageType, width, height);
}

@Override
public String toString() {
return "ImageFile{" +
"id=" + id +
", size=" + size +
", imageType=" + imageType +
", width=" + width +
", height=" + height +
'}';
}

public Long getImageId() {
return this.id;
}
}
6 changes: 4 additions & 2 deletions src/main/java/nextstep/courses/domain/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.util.Objects;

public class Money {
private int money;
public static final Money ZERO = new Money(0);

private final int money;

public Money(int money) {
validation(money);
Expand All @@ -21,7 +23,7 @@ public boolean matches(int money) {
}

public boolean matches(Money money) {
return this.money == money.getMoney();
return matches(money.getMoney());
}

public int getMoney() {
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/nextstep/courses/domain/PaidEnrollmentRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ public class PaidEnrollmentRule implements EnrollmentRule {
private final Money price;
private final Capacity capacity;

PaidEnrollmentRule(int price, int capacity) {
public PaidEnrollmentRule(int price, int capacity) {
this(new Money(price), new Capacity(capacity));
}

PaidEnrollmentRule(Money price, Capacity capacity) {
public PaidEnrollmentRule(Money price, Capacity capacity) {
this.price = price;
this.capacity = capacity;
}

@Override
public void validate(int money, int enrolledCount) {
validateMoney(new Money(money));
public void validate(Money money, int enrolledCount) {
validateMoney(money);
validateCapacity(enrolledCount);
}

Expand All @@ -37,4 +37,12 @@ public void validateCapacity(int enrolledCount) {
public SessionType getType() {
return SessionType.PAID;
}

public int getPrice() {
return this.price.getMoney();
}

public int getCapacity() {
return this.capacity.getCapacity();
}
}
90 changes: 84 additions & 6 deletions src/main/java/nextstep/courses/domain/Session.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package nextstep.courses.domain;

import java.time.LocalDateTime;
import java.util.Objects;

public class Session {
Copy link
Contributor

Choose a reason for hiding this comment

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

Service Layer를 담당하는 Service 클래스도 추가해 보는 것은 어떨까?
도메인 객체로 로직을 구현한 만큼 Service Layer가 로직을 담당하지 않는지?
Service 클래스가 Repository와 도메인 객체를 어떻게 연결하는지 확인해 보면 어떨까?
이번 기회에 Service Layer의 역할은 무엇인지 고민해 보면 어떨까?

private final Long id;
private final ImageFile imageFile;
Expand All @@ -8,8 +11,16 @@ public class Session {
private final EnrollmentRule enrollmentRule;
private final Enrollments enrollments;

public Session(Long id, ImageFile imageFile, SessionPeriod period, SessionStatus sessionStatus, EnrollmentRule enrollmentRule) {
this(id, imageFile, period, sessionStatus, enrollmentRule, new Enrollments());
public Session(Long id,ImageFile imageFile, LocalDateTime startTime, LocalDateTime endTime, String sessionStatus, Integer price, Integer capacity) {
this(id, imageFile, new SessionPeriod(startTime, endTime), SessionStatus.valueOf(sessionStatus), allocateEnrollmentRule(price, capacity), new Enrollments());
}

public Session(ImageFile imageFile, SessionPeriod period, SessionStatus sessionStatus, EnrollmentRule enrollmentRule) {
this(null, imageFile, period, sessionStatus, enrollmentRule, new Enrollments());
}

public Session(ImageFile imageFile, SessionPeriod period, SessionStatus sessionStatus, EnrollmentRule enrollmentRule, Enrollments enrollments) {
this(null, imageFile, period, sessionStatus, enrollmentRule, enrollments);
}

public Session(Long id, ImageFile imageFile, SessionPeriod period, SessionStatus sessionStatus, EnrollmentRule enrollmentRule, Enrollments enrollments) {
Expand All @@ -25,13 +36,12 @@ public int countEnrollments() {
return enrollments.countEnrollments();
}

public void enroll(Enrollment enrollment) {
public void enroll(Enrollment enrollment, Money money) {
validationRecruiting();

enrollment.validateBelongsTo(this);
enrollment.validateBelongsTo(getId());

enrollmentRule.validateMoney(enrollment.getMoney());
enrollmentRule.validateCapacity(enrollments.countEnrollments());
enrollmentRule.validate(money, countEnrollments());

enrollments.enroll(enrollment);
}
Expand All @@ -40,9 +50,77 @@ public Long getId() {
return id;
}

public Long getImageId() {
return this.imageFile.getImageId();
}

public String getSessionStatus() {
return this.sessionStatus.toString();
}

public Integer getPrice() {
if (enrollmentRule.getType().equals(SessionType.PAID)) {
return ((PaidEnrollmentRule) this.enrollmentRule).getPrice();
}

return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

null보다 무료 강의의 기본을 0원으로 반환하는 것은 어떨까?

}

public Integer getCapacity() {
if (enrollmentRule.getType().equals(SessionType.PAID)) {
return ((PaidEnrollmentRule) this.enrollmentRule).getCapacity();
}

return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

null보다 무료 강의의 Integer 최대 값으로 반환하거나 무료 강의도 capacity 값을 가지는 것은 어떨까?

}

public LocalDateTime getStartTime() {
return this.period.getStartTime();
}

public LocalDateTime getEndTime() {
return this.period.getEndTime();
}

public SessionPeriod getPeriod() {
return this.period;
}

private static EnrollmentRule allocateEnrollmentRule(Integer price, Integer capacity) {
if (price != null) {
return new PaidEnrollmentRule(price, capacity);
}

return new FreeEnrollmentRule();
}

private void validationRecruiting() {
if (!sessionStatus.enableRecruiting()) {
throw new IllegalArgumentException("모집중인 강의만 수강 신청할 수 있습니다.");
}
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Session session = (Session) o;
return Objects.equals(id, session.id) && Objects.equals(imageFile, session.imageFile) && Objects.equals(period, session.period) && sessionStatus == session.sessionStatus && Objects.equals(enrollmentRule, session.enrollmentRule) && Objects.equals(enrollments, session.enrollments);
}

@Override
public int hashCode() {
return Objects.hash(id, imageFile, period, sessionStatus, enrollmentRule, enrollments);
}

@Override
public String toString() {
return "Session{" +
"id=" + id +
", imageFile=" + imageFile +
", period=" + period +
", sessionStatus=" + sessionStatus +
", enrollmentRule=" + enrollmentRule +
", enrollments=" + enrollments +
'}';
}
}
Loading