refactoring : mutable data - replace derived variable with query

This commit is contained in:
haerong22
2022-03-12 23:17:33 +09:00
parent 055b3cd831
commit 6cc60fd90c
6 changed files with 113 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
package com.example.refactoring._06_mutable_data._21_replace_derived_variable_with_query;
public class Discount {
private double discount;
private double baseTotal;
public Discount(double baseTotal) {
this.baseTotal = baseTotal;
}
public double getDiscountedTotal() {
return this.baseTotal - this.discount;
}
public void setDiscount(double number) {
this.discount = number;
}
}

View File

@@ -0,0 +1,17 @@
package com.example.refactoring._06_mutable_data._21_replace_derived_variable_with_query;
import java.util.ArrayList;
import java.util.List;
public class ProductionPlan {
private List<Double> adjustments = new ArrayList<>();
public void applyAdjustment(double adjustment) {
this.adjustments.add(adjustment);
}
public double getProduction() {
return this.adjustments.stream().mapToDouble(Double::valueOf).sum();
}
}

View File

@@ -0,0 +1,22 @@
package com.example.refactoring._06_mutable_data._21_replace_derived_variable_with_query.before;
public class Discount {
private double discountedTotal;
private double discount;
private double baseTotal;
public Discount(double baseTotal) {
this.baseTotal = baseTotal;
}
public double getDiscountedTotal() {
return this.discountedTotal;
}
public void setDiscount(double number) {
this.discount = number;
this.discountedTotal = this.baseTotal - this.discount;
}
}

View File

@@ -0,0 +1,19 @@
package com.example.refactoring._06_mutable_data._21_replace_derived_variable_with_query.before;
import java.util.ArrayList;
import java.util.List;
public class ProductionPlan {
private double production;
private List<Double> adjustments = new ArrayList<>();
public void applyAdjustment(double adjustment) {
this.adjustments.add(adjustment);
this.production += adjustment;
}
public double getProduction() {
return this.production;
}
}

View File

@@ -0,0 +1,18 @@
package com.example.refactoring._06_mutable_data._21_replace_derived_variable_with_query;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class DiscountTest {
@Test
void discount() {
Discount discount = new Discount(100);
assertEquals(100, discount.getDiscountedTotal());
discount.setDiscount(10);
assertEquals(90, discount.getDiscountedTotal());
}
}

View File

@@ -0,0 +1,17 @@
package com.example.refactoring._06_mutable_data._21_replace_derived_variable_with_query;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ProductionPlanTest {
@Test
void production() {
ProductionPlan productionPlan = new ProductionPlan();
productionPlan.applyAdjustment(10);
productionPlan.applyAdjustment(20);
assertEquals(30, productionPlan.getProduction());
}
}