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
Show file tree
Hide file tree
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
Next Next commit
실습 대상인 4장 코드를 가져오라
4장 초반의 리팩토링 전 코드를 가져왔습니다.
  • Loading branch information
johngrib committed Mar 21, 2020
commit f7ca49f6168efd1de4f55cba07e8ce03f875636f
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,25 @@
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;
}
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,56 @@
package com.johngrib.objects._05_refactoring;

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;
}
}

// discountable 변수의 값을 체크하고 적절한 할인 정책에 따라 예매 요금을 계산하는 if 문
Money fee;
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 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;
}