-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 05 후반의 리팩토링 과정을 따라하라
- Loading branch information
Showing
8 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/com/johngrib/objects/_05_refactoring/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/johngrib/objects/_05_refactoring/DiscountCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/johngrib/objects/_05_refactoring/DiscountConditionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/main/java/com/johngrib/objects/_05_refactoring/Movie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/johngrib/objects/_05_refactoring/MovieType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 // 미적용 | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/johngrib/objects/_05_refactoring/Reservation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/johngrib/objects/_05_refactoring/ReservationAgency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/main/java/com/johngrib/objects/_05_refactoring/Screening.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |