Each function have to take into account the last result, but I need to pass different arguments in each function too.
You may be looking for currying
Currying is when you break down a function that takes multiple arguments into a series of functions that take part of the arguments.
In java, this will normally look like using some arguments to create a new object, which includes an interface to take the remaining arguments. (In more recent versions of Java, you are more likely to see lambda's used, but the basic idea is the same.)
double volumeToPaint = ... // calculate from car and mixture
double necessaryVolumePaint = ... // calculate from volumeToPaint, car and paint
double necessaryKgPaint = ... // calculate from necessaryVolumePaint and paint
double cost = ... // calculate from necessaryKgPaint and paint
return cost;
So in this example, we want to build a bunch of functions that accept a double as an argument, and return a double as a result, that will all get chained together.
DoubleUnaryOperator computeNecessaryVolume = someFactory.createComputeNecessaryVolume(car, paint);
DoubleUnaryOperator computeNecessaryKgPaint = someFactory.createComputeNecessaryKgPaint(paint);
DoubleUnaryOperator computeCost = someFactory.createComputeCost(paint);
double volumeToPaint = computeVolume(car, mixture);
double necessaryVolumeToPaint = computeNecessaryVolume.applyAsDouble(volumeToPaint);
double necessaryKgPaint = computeNecessaryKgPaint.applyAsDouble(necessaryVolumeToPaint);
double cost = computeCost.applyAsDouble(necessaryKgPaint);
That's the basic idea; you can possibly "simplify" it by then chaining the functions together into a pipeline, as we might do with a stream....
cost = computeVolume(car, mixture)
.map(volumeToPaint -> computeNecessaryVolume.applyAsDouble(volumeToPaint))
.map(necessaryVolumeToPaint -> computeNecessaryKgPaint.applyAsDouble(necessaryVolumeToPaint))
.map(necessaryKgPaint -> computeCost.applyAsDouble(necessaryKgPaint) )
But more likely you would just compose the functions together.
DoubleUnaryOperation doIt = computeNecessaryVolume
.andThen(computeNecessaryKgPaint)
.andThen(computeCost);
double cost = doIt.applyAsDouble(computeVolume(car, mixture))
Can you write some example about function declaration of computeNecessaryVolume, computeNecessaryKgPaint and computeCost functions?
class ComputeNecessaryVolume implements DoubleUnaryOperator {
final Car car;
final Mixture mixture;
ComputeNecessaryVolume(Car car, Mixture mixture) {
this.car = car;
this.mixture = mixture;
}
@Override
double applyAsDouble(double volumeToPaint) {
// Do whatever you were doing before with car, mixture, and volumeToPaint
double necessaryVolumeToPaint = ...
return necessaryVolumeToPaint;
}
}