Skip to content

Commit

Permalink
Chapter 05 후반의 리팩토링 과정을 따라하라 #5
Browse files Browse the repository at this point in the history
Chapter 05 후반의 리팩토링 과정을 따라하라
  • Loading branch information
johngrib authored Mar 21, 2020
2 parents 0c94c3f + a311f44 commit 05ef7c6
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/johngrib/objects/_05_refactoring/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.johngrib.objects._05_refactoring;

public class Customer {
private String name;
private String id;

public Customer(String name, String id) {
this.name = name;
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.johngrib.objects._05_refactoring;

import lombok.Getter;
import lombok.Setter;

import java.time.DayOfWeek;
import java.time.LocalTime;

public class DiscountCondition {
@Getter
@Setter
private DiscountConditionType type;
@Getter
@Setter
private int sequence;
@Getter
@Setter
private DayOfWeek dayOfWeek;
@Getter
@Setter
private LocalTime startTime;
@Getter
@Setter
private LocalTime endTime;

public boolean isDiscountable(Screening screening) {
if (type == DiscountConditionType.PERIOD) {
return isSatisfiedByPeriod(screening);
}
return isSatisfiedBySequence(screening);
}

private boolean isSatisfiedByPeriod(Screening screening) {
return screening.getWhenScreened().getDayOfWeek().equals(dayOfWeek)
&& startTime.isBefore(screening.getWhenScreened().toLocalTime())
&& endTime.isAfter(screening.getWhenScreened().toLocalTime());
}

private boolean isSatisfiedBySequence(Screening screening) {
return sequence == screening.getSequence();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.johngrib.objects._05_refactoring;

public enum DiscountConditionType {
SEQUENCE, // 순번 조건
PERIOD // 기간 조건
}
34 changes: 34 additions & 0 deletions src/main/java/com/johngrib/objects/_05_refactoring/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.johngrib.objects._05_refactoring;

import com.johngrib.objects._02_movie.Money;
import lombok.Getter;
import lombok.Setter;

import java.time.Duration;
import java.util.Collections;
import java.util.List;

public class Movie {
private String title;
private Duration runningTime;
@Getter
@Setter
private Money fee;

@Setter
private List<DiscountCondition> discountConditions;

public List<DiscountCondition> getDiscountConditions() {
return Collections.unmodifiableList(discountConditions);
}

@Getter
@Setter
private MovieType movieType;
@Getter
@Setter
private Money discountAmount;
@Getter
@Setter
private double discountPercent;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.johngrib.objects._05_refactoring;

public enum MovieType {
AMOUNT_DISCOUNT, // 금액 할인 정책
PERCENT_DISCOUNT, // 비율 할인 정책
NONE_DISCOUNT // 미적용
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.johngrib.objects._05_refactoring;

import com.johngrib.objects._02_movie.Money;
import lombok.Getter;
import lombok.Setter;

public class Reservation {
@Getter
@Setter
private Customer customer;
@Getter
@Setter
private Screening screening;
@Getter
@Setter
private Money fee;
@Getter
@Setter
private int audienceCount;

public Reservation(Customer customer, Screening screening, Money fee, int audienceCount) {
this.customer = customer;
this.screening = screening;
this.fee = fee;
this.audienceCount = audienceCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.johngrib.objects._05_refactoring;

import com.johngrib.objects._02_movie.Money;

public class ReservationAgency {
public Reservation reserve(Screening screening, Customer customer, int audienceCount) {
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 -> condition.isDiscountable(screening));
}

private Money calculateFee(Screening screening, boolean discountable, int audienceCount) {
if (discountable) {
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);
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/johngrib/objects/_05_refactoring/Screening.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.johngrib.objects._05_refactoring;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

public class Screening {
@Getter
@Setter
private Movie movie;
@Getter
@Setter
private int sequence;
@Getter
@Setter
private LocalDateTime whenScreened;
}

0 comments on commit 05ef7c6

Please sign in to comment.