refactoring : shotgun surgery - move field

This commit is contained in:
haerong22
2022-03-18 13:00:37 +09:00
parent 7aa3953c83
commit 09f76d51d6
5 changed files with 128 additions and 0 deletions

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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));
}
}