-
Notifications
You must be signed in to change notification settings - Fork 308
Step2 리뷰요청 드립니다. #819
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
base: qwer920414-ctrl
Are you sure you want to change the base?
Step2 리뷰요청 드립니다. #819
Changes from all commits
e900a4a
8271411
cdcc5a9
20eb67f
2ae44f9
31bccd9
7131ea4
00c2257
3612e77
b9e6e97
770df4d
7dd2e8c
bfc3ef3
64113eb
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,18 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| public class Capacity { | ||
|
|
||
| private final int max; | ||
| private int current; | ||
|
|
||
| public Capacity(int max, int current) { | ||
| this.max = max; | ||
| this.current = current; | ||
| } | ||
|
|
||
| public void validateAvailable() { | ||
| if (current > max) { | ||
| throw new IllegalStateException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| public class CoverImage { | ||
| private ImageSize imageSize; | ||
| private ImageType imageType; | ||
| private ImageDimension imageDimension; | ||
|
|
||
| public CoverImage(int size, String fileName, int width, int height) { | ||
| this(new ImageSize(size), ImageType.fromFileName(fileName), new ImageDimension(width, height)); | ||
| } | ||
|
|
||
| private CoverImage(ImageSize imageSize, ImageType imageType, ImageDimension imageDimension) { | ||
| this.imageSize = imageSize; | ||
| this.imageType = imageType; | ||
| this.imageDimension = imageDimension; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| 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 = sessionId; | ||
| this.userId = userId; | ||
| this.enrollmentDate = LocalDateTime.now(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import nextstep.payments.domain.Payment; | ||
|
|
||
| public interface EnrollmentPolicy { | ||
|
Contributor
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. 👍 |
||
| void validateEnrollment(Payment payment); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import nextstep.payments.domain.Payment; | ||
|
|
||
| public class FreeEnrollmentPolicy implements EnrollmentPolicy{ | ||
| @Override | ||
| public void validateEnrollment(Payment payment) { | ||
| // 무료강의는 검증하지 않는다 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| public class ImageDimension { | ||
|
|
||
| public static final int HEIGHT_RATIO = 2; | ||
| public static final int WIDTH_RATIO = 3; | ||
| public static final int MIN_WIDTH = 300; | ||
| public static final int MIN_HEIGHT = 200; | ||
| private int width; | ||
| private int height; | ||
|
|
||
| public ImageDimension(int width, int height) { | ||
| validateMinimumSize(width, height); | ||
| validateRatio(width, height); | ||
| this.width = width; | ||
| this.height = height; | ||
| } | ||
|
|
||
| private static void validateMinimumSize(int width, int height) { | ||
| if (width < MIN_WIDTH || height < MIN_HEIGHT) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| private static void validateRatio(int width, int height) { | ||
| if (!(HEIGHT_RATIO * width == WIDTH_RATIO * height)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class ImageSize { | ||
|
|
||
| public static final int MAX_SIZE = 1_048_576; | ||
| private int imageSize; | ||
|
|
||
| public ImageSize(int imageSize) { | ||
| if (imageSize > MAX_SIZE) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| this.imageSize = imageSize; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object object) { | ||
| if (this == object) return true; | ||
| if (object == null || getClass() != object.getClass()) return false; | ||
| ImageSize imageSize1 = (ImageSize) object; | ||
| return imageSize == imageSize1.imageSize; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(imageSize); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public enum ImageType { | ||
|
|
||
| GIF("gif"), | ||
| JPG("jpg", "jpeg"), | ||
| PNG("png"), | ||
| SVG("svg"); | ||
|
|
||
| private final String[] extensions; | ||
|
|
||
| ImageType(String... extensions) { | ||
| this.extensions = extensions; | ||
| } | ||
|
|
||
| private boolean matches(String extension) { | ||
| return Arrays.stream(extensions) | ||
| .anyMatch(ext -> ext.equalsIgnoreCase(extension)); | ||
| } | ||
|
|
||
| private static ImageType from(String extension) { | ||
| return Arrays.stream(values()) | ||
| .filter(type -> type.matches(extension)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException()); | ||
| } | ||
|
|
||
| public static ImageType fromFileName(String fileName) { | ||
| String extension = extractExtension(fileName); | ||
| return from(extension); | ||
| } | ||
|
|
||
| private static String extractExtension(String fileName) { | ||
| int index = fileName.lastIndexOf("."); | ||
| if (index == -1 || index == fileName.length() - 1) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| return fileName.substring(index + 1); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class Money { | ||
| private final long amount; | ||
|
|
||
| public Money(long amount) { | ||
| this.amount = amount; | ||
| } | ||
|
|
||
| public boolean isEqualTo(Money other) { | ||
| return this.equals(other); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object object) { | ||
| if (this == object) return true; | ||
| if (object == null || getClass() != object.getClass()) return false; | ||
| Money money = (Money) object; | ||
| return amount == money.amount; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(amount); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import nextstep.payments.domain.Payment; | ||
|
|
||
| public class PaidEnrollmentPolicy implements EnrollmentPolicy { | ||
|
|
||
| private final Money money; | ||
| private final Capacity capacity; | ||
|
|
||
| public PaidEnrollmentPolicy(Money money, Capacity capacity) { | ||
| this.money = money; | ||
| this.capacity = capacity; | ||
| } | ||
|
|
||
| @Override | ||
| public void validateEnrollment(Payment payment) { | ||
| capacity.validateAvailable(); | ||
| validatePayment(payment); | ||
| } | ||
|
|
||
| private void validatePayment(Payment payment) { | ||
| if (!payment.isSameAmount(money)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import nextstep.payments.domain.Payment; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public class Session { | ||
| private final long id; | ||
| private final SessionDuration sessionDuration; | ||
| private final CoverImage coverImage; | ||
| private final EnrollmentPolicy enrollmentPolicy; | ||
| private final SessionState sessionState; | ||
|
|
||
| public Session(long id | ||
| , LocalDateTime startDate | ||
| , LocalDateTime endDate | ||
| , int size | ||
| , String fileName | ||
| , int width | ||
| , int height | ||
| , EnrollmentPolicy enrollmentPolicy | ||
| , SessionState sessionState) { | ||
| this(id, new SessionDuration(startDate, endDate), new CoverImage(size, fileName, width, height) | ||
| , enrollmentPolicy, sessionState); | ||
|
Comment on lines
+23
to
+24
Contributor
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 Session(long id, SessionDuration sessionDuration, CoverImage coverImage | ||
| , EnrollmentPolicy enrollmentPolicy, SessionState sessionState) { | ||
| this.id = id; | ||
| this.sessionDuration = sessionDuration; | ||
| this.coverImage = coverImage; | ||
| this.enrollmentPolicy = enrollmentPolicy; | ||
| this.sessionState = sessionState; | ||
| } | ||
|
|
||
| public Enrollment enroll(Long userId, Payment payment) { | ||
|
Contributor
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. 👍 |
||
| sessionState.validateEnroll(); | ||
| enrollmentPolicy.validateEnrollment(payment); | ||
| return new Enrollment(this.id, userId); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| public class SessionDuration { | ||
| private LocalDateTime startDate; | ||
| private LocalDateTime endDate; | ||
|
|
||
| public SessionDuration(LocalDateTime startDate, LocalDateTime endDate) { | ||
| validateDate(startDate, endDate); | ||
| this.startDate = startDate; | ||
| this.endDate = endDate; | ||
| } | ||
|
|
||
| private void validateDate(LocalDateTime startDate, LocalDateTime endDate) { | ||
| if (!startDate.isBefore(endDate)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| public enum SessionState { | ||
| READY, | ||
| OPEN, | ||
| CLOSED; | ||
|
|
||
| public void validateEnroll() { | ||
| if (this != OPEN) { | ||
| throw new IllegalStateException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package nextstep.courses.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Sessions { | ||
| private final List<Session> sessions; | ||
|
|
||
| public Sessions() { | ||
| this(new ArrayList<>()); | ||
| } | ||
|
|
||
| public Sessions(List<Session> sessions) { | ||
| this.sessions = new ArrayList<>(sessions); | ||
| } | ||
|
|
||
| public void add(Session session) { | ||
| this.sessions.add(session); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍