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 11 을 공부하라 #8

Merged
merged 17 commits into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
61bd8d5
실습 대상인 10장 코드를 가져오라
johngrib Apr 8, 2020
6f8b9f6
일반 요금제에 세금 정책을 조합한 클래스를 추가하라
johngrib Apr 9, 2020
30271de
Phone에 afterCalculated 메소드를 추가하라
johngrib Apr 8, 2020
577b89b
afterCalculated 메소드의 기본 구현을 제공하라
johngrib Apr 8, 2020
bb652f5
TexableRegularPhone 의 계산 요금에 세금을 부과하라
johngrib Apr 9, 2020
babf23d
심야 할인 요금제에 세금을 부과하라
johngrib Apr 9, 2020
0fe3771
일반 요금제와 기본 요금 할인 정책을 조합한 RateDiscountableRegularPhone을 추가하라
johngrib Apr 9, 2020
f21cd26
심야 할인 요금제와 기본 요금 할인 정책을 조합한 RateDiscountableNightlyDiscountPhone 을 추가하라
johngrib Apr 9, 2020
0ceb4c4
합성 관계로 변경: RatePolicy 인터페이스를 추가하라
johngrib Apr 9, 2020
7d0a60d
합성 관계로 변경: 중복 코드를 담을 추상 클래스를 추가하라
johngrib Apr 9, 2020
59c5085
일반 요금제를 구현하라
johngrib Apr 9, 2020
c190959
심야할인 요금제를 구현하라
johngrib Apr 10, 2020
ece0d29
기본 정책으로 요금을 계산할 수 있도록 Phone을 수정하라
johngrib Apr 10, 2020
d398547
부가 정책을 AdditionalRatePolicy 추상 클래스로 표현하라
johngrib Apr 11, 2020
551341c
세금 정책을 추가하라
johngrib Apr 11, 2020
7451613
기본 요금 할인 정책을 추가하라
johngrib Apr 11, 2020
42f7db2
Phone 클래스의 사용 방법을 기록하라
johngrib Apr 11, 2020
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
실습 대상인 10장 코드를 가져오라
  • Loading branch information
johngrib committed Apr 8, 2020
commit 61bd8d5316128e1e48ab20517859b7a8a9eaddab
24 changes: 24 additions & 0 deletions src/main/java/com/johngrib/objects/_11_call/Call.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.johngrib.objects._11_call;

import lombok.Getter;

import java.time.Duration;
import java.time.LocalDateTime;

/**
* 개별 통화 시간.
*/
public class Call {
@Getter
private LocalDateTime from;
private LocalDateTime to;

public Call(LocalDateTime from, LocalDateTime to) {
this.from = from;
this.to = to;
}

public Duration getDuration() {
return Duration.between(from, to);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.johngrib.objects._11_call;

import com.johngrib.objects._02_movie.Money;

import java.time.Duration;

/**
* 심야 할인 요금제.
*/
public class NightlyDiscountPhone extends Phone {
private static final int LATE_NIGHT_HOUR = 22;

private Money nightlyAmount;
private Money regularAmount;
private Duration seconds;

public NightlyDiscountPhone(Money nightlyAmount, Money regularAmount, Duration seconds) {
super(0);
this.nightlyAmount = nightlyAmount;
this.regularAmount = regularAmount;
this.seconds = seconds;
}

public NightlyDiscountPhone(Money nightlyAmount, Money regularAmount, Duration seconds, double taxRate) {
super(taxRate);
this.nightlyAmount = nightlyAmount;
this.regularAmount = regularAmount;
this.seconds = seconds;
}

@Override
protected Money calculateCallFee(Call call) {
if (call.getFrom().getHour() >= LATE_NIGHT_HOUR) {
return nightlyAmount.times(call.getDuration().getSeconds() / seconds.getSeconds());
}
return regularAmount.times(call.getDuration().getSeconds() / seconds.getSeconds());
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/johngrib/objects/_11_call/Phone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.johngrib.objects._11_call;

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

import java.util.ArrayList;
import java.util.List;

public abstract class Phone {

private double taxRate;
/** 전체 통화 목록 */
@Getter
private List<Call> calls = new ArrayList<>();

public Phone(double taxRate) {
this.taxRate = taxRate;
}

public Money calculateFee() {
Money result = Money.ZERO;

for (Call call : calls) {
result = result.plus(calculateCallFee(call));
}
return result.plus(result.times(taxRate));
}

abstract protected Money calculateCallFee(Call call);
}
34 changes: 34 additions & 0 deletions src/main/java/com/johngrib/objects/_11_call/RegularPhone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.johngrib.objects._11_call;

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

import java.time.Duration;

/**
* 일반 요금제.
*/
public class RegularPhone extends Phone {
/** 단위 요금. */
@Getter
private Money amount;
@Getter
private Duration seconds;

public RegularPhone(Money amount, Duration seconds) {
super(0);
this.amount = amount;
this.seconds = seconds;
}

public RegularPhone(Money amount, Duration seconds, double taxRate) {
super(taxRate);
this.amount = amount;
this.seconds = seconds;
}

@Override
protected Money calculateCallFee(Call call) {
return amount.times(call.getDuration().getSeconds() / seconds.getSeconds());
}
}