refactoring : mutable data - replace derived variable with query
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user