Skip to content
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

Chapter 05 후반의 리팩토링 과정을 따라하라 #5

Merged
merged 3 commits into from
Mar 21, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ReservationAgency를 응집도 높은 메소드들로 분해하라
  • Loading branch information
johngrib committed Mar 21, 2020
commit 35a31261ef0cedb01e59dcd16bf014962f353d43
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,69 @@

import com.johngrib.objects._02_movie.Money;

import java.time.LocalTime;

public class ReservationAgency {
/** Movie에 설정된 DiscountCondition 목록을 차례대로 탐색하며 영화의 할인 여부를 판단한다. */
public Reservation reserve(Screening screening, Customer customer, int audienceCount) {
Movie movie = screening.getMovie();

boolean discountable = false; // 할인 여부

// 할인 가능 여부를 확인하는 for 문
for (DiscountCondition condition : movie.getDiscountConditions()) {
if (condition.getType() == DiscountConditionType.PERIOD) {
// 기간 조건이면
LocalTime whenScreened = screening.getWhenScreened().toLocalTime();

discountable = screening.getWhenScreened().getDayOfWeek().equals(condition.getDayOfWeek())
&& condition.getStartTime().compareTo(whenScreened) <= 0
&& condition.getEndTime().compareTo(whenScreened) >= 0;
} else {
// 순번 조건이면
discountable = condition.getSequence() == screening.getSequence();
}
if (discountable) {
break;
}
boolean discountable = checkDiscountable(screening);
Money fee = calculateFee(screening, discountable, audienceCount);
return createReservation(screening, customer, audienceCount, fee);
}

private boolean checkDiscountable(Screening screening) {
return screening.getMovie().getDiscountConditions().stream()
.anyMatch(condition -> isDiscountable(condition, screening));
}

private boolean isDiscountable(DiscountCondition condition, Screening screening) {
if (condition.getType() == DiscountConditionType.PERIOD) {
return isSatisfiedByPeriod(condition, screening);
}
return isSatisfiedBySequence(condition, screening);
}

// discountable 변수의 값을 체크하고 적절한 할인 정책에 따라 예매 요금을 계산하는 if 문
Money fee;
private boolean isSatisfiedByPeriod(DiscountCondition condition, Screening screening) {
return screening.getWhenScreened().getDayOfWeek().equals(condition.getDayOfWeek())
&& condition.getStartTime().isBefore(screening.getWhenScreened().toLocalTime())
&& condition.getEndTime().isAfter(screening.getWhenScreened().toLocalTime());
}

private boolean isSatisfiedBySequence(DiscountCondition condition, Screening screening) {
return condition.getSequence() == screening.getSequence();
}

private Money calculateFee(Screening screening, boolean discountable, int audienceCount) {
if (discountable) {
Money discountAmount = Money.ZERO;
switch (movie.getMovieType()) {
case AMOUNT_DISCOUNT:
// 금액 할인 정책인 경우
discountAmount = movie.getDiscountAmount();
break;
case PERCENT_DISCOUNT:
// 비율 할인 정책인 경우
discountAmount = movie.getFee().times(movie.getDiscountPercent());
break;
case NONE_DISCOUNT:
// 할인 정책이 적용되지 않은 경우
discountAmount = Money.ZERO;
break;
}
fee = movie.getFee().minus(discountAmount).times(audienceCount);
} else {
fee = movie.getFee();
return screening.getMovie().getFee()
.minus(calculateDiscountedFee(screening.getMovie()))
.times(audienceCount);
}
return screening.getMovie().getFee();
}

private Money calculateDiscountedFee(Movie movie) {
switch (movie.getMovieType()) {
case AMOUNT_DISCOUNT:
return calculateAmountDiscountedFee(movie);
case PERCENT_DISCOUNT:
return calculatePercentDiscountedFee(movie);
case NONE_DISCOUNT:
return calculateNoneDiscountedFee(movie);
}
throw new IllegalArgumentException();
}

private Money calculateAmountDiscountedFee(Movie movie) {
return movie.getDiscountAmount();
}

private Money calculatePercentDiscountedFee(Movie movie) {
return movie.getFee().times(movie.getDiscountPercent());
}

private Money calculateNoneDiscountedFee(Movie movie) {
return movie.getFee();
}

private Reservation createReservation(Screening screening, Customer customer, int audienceCount, Money fee) {
return new Reservation(customer, screening, fee, audienceCount);
}
}