borovaneca

AppliedArithmetic

May 25th, 2023
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. package Advance.FunctionalProgramming.Exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.function.Consumer;
  7. import java.util.function.Function;
  8. import java.util.function.Predicate;
  9. import java.util.stream.Collectors;
  10.  
  11. public class AppliedArithmetic {
  12.     public static void main(String[] args) {
  13.         Scanner scanner = new Scanner(System.in);
  14.  
  15.  
  16.         List<Integer> numbers = Arrays.stream(scanner.nextLine().split("\\s+"))
  17.                 .map(Integer::parseInt).collect(Collectors.toList());
  18.  
  19.         Function<List<Integer>, List<Integer>> add = nums -> nums.stream().map(number -> number + 1).collect(Collectors.toList());
  20.         Function<List<Integer>, List<Integer>> subtract = nums -> nums.stream().map(number -> number - 1).collect(Collectors.toList());
  21.         Function<List<Integer>, List<Integer>> multiply = nums -> nums.stream().map(number -> number * 2).collect(Collectors.toList());
  22.         Consumer<List<Integer>> print = e -> e.forEach(item -> System.out.printf("%d ", item));
  23.  
  24.         String command = scanner.nextLine();
  25.         while (!"end".equals(command)) {
  26.             if (command.equals("add")) {
  27.                 numbers = add.apply(numbers);
  28.             } else if (command.equals("subtract")) {
  29.                 numbers = subtract.apply(numbers);
  30.             } else if (command.equals("multiply")) {
  31.                 numbers = multiply.apply(numbers);
  32.             } else if (command.equals("print")) {
  33.                 print.accept(numbers);
  34.                 System.out.println();
  35.             }
  36.             command = scanner.nextLine();
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment