Skip to content

Instantly share code, notes, and snippets.

@afcastano
Last active August 14, 2016 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afcastano/59ea31314f583a40a3a929eb25147417 to your computer and use it in GitHub Desktop.
Save afcastano/59ea31314f583a40a3a929eb25147417 to your computer and use it in GitHub Desktop.
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