Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 18 additions & 1 deletion src/main/java/com/johngrib/objects/_02_movie/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import lombok.ToString;

import java.math.BigDecimal;
import java.util.Objects;

/** 금액. */
@EqualsAndHashCode
@ToString
public class Money {
public static final Money ZERO = Money.wons(0);
Expand Down Expand Up @@ -44,4 +44,21 @@ public boolean isLessThan(Money other) {
public boolean isGreaterThanOrEqual(Money other) {
return amount.compareTo(other.amount) >= 0;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Money money = (Money) o;
return amount.compareTo(money.amount) == 0;
}

@Override
public int hashCode() {
return Objects.hash(amount);
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/johngrib/objects/_10_call/Call.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.johngrib.objects._10_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._10_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/_10_call/Phone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.johngrib.objects._10_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/_10_call/RegularPhone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.johngrib.objects._10_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());
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/johngrib/objects/_10_call/PhoneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.johngrib.objects._10_call;

import com.johngrib.objects._02_movie.Money;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.*;

@DisplayName("Phone 클래스")
class PhoneTest {
@Nested
@DisplayName("calculateCallFee 메소드")
class Describe_calculateCallFee {

@Nested
@DisplayName("10초에 5원씩 부과되는 요금제에 가입했고")
class Context_with_10sec_5won_phone {
RegularPhone givenPhone() {
return new RegularPhone(Money.wons(5), Duration.ofSeconds(10));
}

@Nested
@DisplayName("1분짜리 통화를 했다면")
class Context_with_1_min_call {

Call getFirstCall() {
return new Call(
LocalDateTime.of(2018, 1, 1, 12, 10, 0),
LocalDateTime.of(2018, 1, 1, 12, 11, 0)
);
}

@Test
@DisplayName("30원을 리턴한다")
void it_returns_calculated_won() {
assertEquals(givenPhone().calculateCallFee(getFirstCall()), Money.wons(30.0));
}
}
}
}
}