The interface implementation
The Java InvoiceTotal class

-
is created manually only once
-
implements all methods of the generated interface
-
provides a method with which the rule module can be executed
-
provides additional helper methods if necessary, such as converting double to BigDecimal in this example
Common development tools such as e.g. IntelliJ or Eclipse, can generate an implementation from an interface at a "touch of a button" so that essentially only the methods need to be programmed in terms of content, e.g. all methods with the @Override annotation.
The implementation of the interface:
package lfet.demo;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class InvoiceTotal implements InvoiceTotal_iFace {
private InvoiceTotal_model model;
private final InvoiceTotal_rulesEngine rulesEngine = new InvoiceTotal_rulesEngine();
public void computeInvoiceTotal(InvoiceTotal_model model) {
this.model = model;
this.model.setTotalDiscount(BigDecimal.ZERO);
this.model.setTotalPostage(BigDecimal.ZERO);
this.model.setTotalAmount(BigDecimal.ZERO);
rulesEngine.execute(this);
}
/**
* @param d any double value,
* NEGATIVE_INFINITY will be replaced Double.MAX_VALUE * -1.0,
* POSITIVE_INFINITY will be replaced Double.MAX_VALUE
* @return the value as BigDecimal, scale 2, RoundingMode.HALF_UP
*/
public BigDecimal bigDecimal(double d) {
return new BigDecimal(Double.toString(
d < Double.MAX_VALUE * -1.0 ? Double.MAX_VALUE * -1.0
: Math.min(d, Double.MAX_VALUE)))
.setScale(2, RoundingMode.HALF_EVEN);
}
/**
* <b>C01: Purchase order value</b><br>
* <br>
* <b>C01/01: < 20.00 - low</b>
*/
@Override
public boolean isPurchaseOrderValue_Less2000() {
return model.getOrderValue().compareTo(bigDecimal(20.00)) < 0;
}
/**
* <b>C01: Purchase order value</b><br>
* <br>
* <b>C01/02: 20.00 - 75.00 - normal</b>
*/
@Override
public boolean isPurchaseOrderValue_20007500() {
return model.getOrderValue().compareTo(bigDecimal(20.00)) >= 0 &&
model.getOrderValue().compareTo(bigDecimal(75.00)) <= 0;
}
/**
* <b>C02: Discount</b><br>
* <br>
* <b>C02/01: >0 - discount given</b>
*/
public boolean isDiscount_Greater0() {
return model.getOrderDiscount().compareTo(BigDecimal.ZERO) > 0;
}
/**
* <b>C03: Discount type</b><br>
* <br>
* <b>C03/01: % - percentage discount on the purchase order value</b>
*/
@Override
public boolean isDiscountType_PercentageDiscountOnThePurchaseOrderValue() {
return model.isDiscountPercent();
}
/**
* <b>C04: Discount < purchase order value</b><br>
* <br>
* <b>C04/01: Y - Yes</b>
*/
@Override
public boolean isDiscountLessPurchaseOrderValue() {
return model.getOrderDiscount().compareTo(model.getOrderValue()) < 0;
}
/**
* <b>A01: Apply discount</b>
*/
@Override
public void doApplyDiscount() {
model.setTotalDiscount(model.isDiscountPercent()
? model.getOrderValue()
.multiply(model.getOrderDiscount())
.multiply(bigDecimal(0.01))
.setScale(2, RoundingMode.HALF_UP)
: model.getOrderDiscount());
}
/**
* <b>A02: Calculate shipping costs</b>
*/
@Override
public void doCalculateShippingCosts() {
model.setTotalPostage(model.getPostage());
}
/**
* <b>A03: Invoice total</b><br>
* <br>
* <b>A03/01: upd - Invoice total must be updated</b>
*/
@Override
public void doInvoiceTotal_Upd() {
model.setTotalAmount(model.getOrderValue()
.subtract(model.getTotalDiscount())
.add(model.getTotalPostage()));
}
/**
* <b>A03: Invoice total</b><br>
* <br>
* <b>A03/02: pov - Purchase order value can be taken over as invoice total</b>
*/
@Override
public void doInvoiceTotal_Pov() {
model.setTotalAmount(model.getOrderValue());
}
/**
* @param dtName - the decision table name
* @param version - the timestamp of program generation
* @param rules - the number of rules in the decision table
* @param rule - the rule which is executed now
*/
@Override
public void doTrace(String dtName, String version, int rules, int rule) {
System.out.printf("%s, %s, %d, %d, %s\n", dtName, version, rules, rule, model);
}
}