import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
// Reads and validates user input
class Parser {
final static int INPUT_BREAK = 0;
public static List<Integer> read() {
Scanner scanner = new Scanner(System.in);
List<Integer> elements = new ArrayList<>();
while(true) {
try {
System.out.printf("Enter an integer value ('%d' to exit): ", INPUT_BREAK);
int input = scanner.nextInt();
if (input == INPUT_BREAK) { break; }
elements.add(input);
} catch(Exception ex) {
System.out.printf("Invalid input! Please try again.\n");
// To clear up any garbage input after an exception
scanner.nextLine();
}
};
scanner.close();
return elements;
}
}
// Does the math processing
class Arithmetic {
public static void process(List<Integer> elements) {
System.out.printf("Sum: %d \n", sum(elements));
System.out.printf("Average: %f \n", average(elements));
}
private static int sum(List<Integer> elements) {
return elements.stream().reduce(0, (a, b) -> a + b);
}
private static double average(List<Integer> elements) {
return (double) sum(elements)/elements.size();
}
}
class Main {
public static void main(String[] args) {
Arithmetic.process(Parser.read());
}
}