Skip to content

Commit ea12299

Browse files
committed
[2장_이지훈] 1일차 예제 파일 작성
1 parent 02e8bb1 commit ea12299

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ezhoon.chapter02
2+
3+
class Customer {
4+
5+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ezhoon.chapter02
2+
3+
import java.math.BigDecimal
4+
5+
/**
6+
* TODO
7+
*
8+
* @property amount 가격 (BigDecimal은 Java언어에서 숫자를 정밀하게 저장하고 표현할 수 있는 유일한 방법이다.)
9+
* - double도 소수점을 표현하지만 정밀도에 있어 한계가 있어 값 유실이 존재한다.
10+
* - 돈과 소수점을 다룬다면 BigDecimal은 필수적이고 단점으로는 느린 속도와 기본 타입보다 조금 불편한 사용이다.
11+
*/
12+
class Money(
13+
private val amount: BigDecimal
14+
) {
15+
16+
fun getWons(amount: Long): Money {
17+
return Money(BigDecimal.valueOf(amount))
18+
}
19+
20+
fun getWons(amount: Double): Money {
21+
return Money(BigDecimal.valueOf(amount))
22+
}
23+
24+
fun plus(other: Money): Money {
25+
return Money(this.amount.add(other.amount))
26+
}
27+
28+
fun minus(other: Money): Money {
29+
return Money(this.amount.subtract(other.amount))
30+
}
31+
32+
fun times(percent: Double): Money {
33+
return Money(this.amount.multiply(BigDecimal(percent)))
34+
}
35+
36+
fun isLessThan(other: Money) = amount < other.amount
37+
38+
fun isGreaterThan(other: Money) = amount >= other.amount
39+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ezhoon.chapter02
2+
3+
import java.math.BigDecimal
4+
5+
data class Movie(
6+
val fee: Int
7+
) {
8+
fun calculateMovieFee(screening: Screening): Money {
9+
return Money(BigDecimal(10).plus(BigDecimal(screening.getMovieFee())))
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ezhoon.chapter02
2+
3+
class Reservation(
4+
customer: Customer,
5+
screening: Screening,
6+
calculateFee: Money,
7+
audienceCount: Int
8+
) {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ezhoon.chapter02
2+
3+
import java.time.LocalDateTime
4+
5+
class Screening(
6+
private val movie: Movie,
7+
private val sequence: Int,
8+
val whenScreened: LocalDateTime
9+
) {
10+
fun isSequence(sequence: Int) = this.sequence == sequence
11+
12+
fun getMovieFee() = movie.fee
13+
14+
fun reserve(customer: Customer, audienceCount: Int): Reservation {
15+
return Reservation(customer, this, calculateFee(audienceCount), audienceCount)
16+
}
17+
18+
private fun calculateFee(audienceCount: Int): Money {
19+
return movie.calculateMovieFee(this).times(audienceCount.toDouble())
20+
}
21+
}

0 commit comments

Comments
 (0)