Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public abstract Result<Integer> readIntFromFile(String file);
public Result<Integer> adjustValue(Integer value) {
if (value > 5) {
Result.error("Value " + value + " should no be greater than 5");
}
return Result.ok(5 - value);
}
public Double calculateAverage(Integer val1, Integer val2) {
return (val1 + val2)/2d;
}
/**
* Read ints from two files, Adjust the first one and then calculate average.
* Returns a Result wrapping the positive outcome or any error.
*/
public Result<Double> businessOperation(String fileName, String fileName2) {
Result<Integer> val1 = readIntFromFile(fileName);
if (val1.isError()) {
return Result.error(val1.getError());
}
Result<Integer> adjusted1 = adjustValue(val1.getValue());
if (adjusted1.isError()) {
return Result.error(adjusted1.getError());
}
Result<Integer> val2 = readIntFromFile(fileName2);
if (val2.isError()) {
return Result.error(val2.getError());
}
Double average = calculateAverage(adjusted1.getValue(), val2.getValue());
return Result.ok(average);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.