diff --git a/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/Customer.java b/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/Customer.java new file mode 100644 index 00000000..d6f39937 --- /dev/null +++ b/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/Customer.java @@ -0,0 +1,38 @@ +package com.example.refactoring._08_shotgun_surgery._27_move_field; + +import java.math.BigDecimal; +import java.time.LocalDate; + +public class Customer { + + private String name; + + private CustomerContract contract; + + public Customer(String name, double discountRate) { + this.name = name; + this.contract = new CustomerContract(dateToday(), discountRate); + } + + public double getDiscountRate() { + return this.contract.getDiscountRate(); + } + + public void setDiscountRate(double discountRate) { + this.contract.setDiscountRate(discountRate); + } + + public void becomePreferred() { + this.setDiscountRate(this.getDiscountRate() + 0.03); + // 다른 작업들 + } + + public double applyDiscount(double amount) { + BigDecimal value = BigDecimal.valueOf(amount); + return value.subtract(value.multiply(BigDecimal.valueOf(this.getDiscountRate()))).doubleValue(); + } + + private LocalDate dateToday() { + return LocalDate.now(); + } +} diff --git a/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/CustomerContract.java b/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/CustomerContract.java new file mode 100644 index 00000000..c6490338 --- /dev/null +++ b/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/CustomerContract.java @@ -0,0 +1,23 @@ +package com.example.refactoring._08_shotgun_surgery._27_move_field; + +import java.time.LocalDate; + +public class CustomerContract { + + private LocalDate startDate; + + private double discountRate; + + public CustomerContract(LocalDate startDate, double discountRate) { + this.startDate = startDate; + this.discountRate = discountRate; + } + + public double getDiscountRate() { + return discountRate; + } + + public void setDiscountRate(double discountRate) { + this.discountRate = discountRate; + } +} diff --git a/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/_before/Customer.java b/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/_before/Customer.java new file mode 100644 index 00000000..90fd7df4 --- /dev/null +++ b/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/_before/Customer.java @@ -0,0 +1,37 @@ +package com.example.refactoring._08_shotgun_surgery._27_move_field._before; + +import java.math.BigDecimal; +import java.time.LocalDate; + +public class Customer { + + private String name; + + private double discountRate; + + private CustomerContract contract; + + public Customer(String name, double discountRate) { + this.name = name; + this.discountRate = discountRate; + this.contract = new CustomerContract(dateToday()); + } + + public double getDiscountRate() { + return discountRate; + } + + public void becomePreferred() { + this.discountRate += 0.03; + // 다른 작업들 + } + + public double applyDiscount(double amount) { + BigDecimal value = BigDecimal.valueOf(amount); + return value.subtract(value.multiply(BigDecimal.valueOf(this.discountRate))).doubleValue(); + } + + private LocalDate dateToday() { + return LocalDate.now(); + } +} diff --git a/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/_before/CustomerContract.java b/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/_before/CustomerContract.java new file mode 100644 index 00000000..e697d0ad --- /dev/null +++ b/refactoring/src/main/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/_before/CustomerContract.java @@ -0,0 +1,12 @@ +package com.example.refactoring._08_shotgun_surgery._27_move_field._before; + +import java.time.LocalDate; + +public class CustomerContract { + + private LocalDate startDate; + + public CustomerContract(LocalDate startDate) { + this.startDate = startDate; + } +} diff --git a/refactoring/src/test/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/CustomerTest.java b/refactoring/src/test/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/CustomerTest.java new file mode 100644 index 00000000..d1573d58 --- /dev/null +++ b/refactoring/src/test/java/com/example/refactoring/_08_shotgun_surgery/_27_move_field/CustomerTest.java @@ -0,0 +1,18 @@ +package com.example.refactoring._08_shotgun_surgery._27_move_field; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CustomerTest { + + @Test + void applyDiscount() { + Customer customer = new Customer("kim", 0.5); + assertEquals(50, customer.applyDiscount(100)); + + customer.becomePreferred(); + assertEquals(47, customer.applyDiscount(100)); + } + +} \ No newline at end of file