Advertisement
CuBG

Untitled

May 25th, 2023
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | Software | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.function.Function;
  6. import java.util.stream.Collectors;
  7.  
  8. public class _04_AppliedArithmetic {
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.         List<Integer> numbers = Arrays.stream(sc.nextLine().split("\\s+"))
  13.                 .map(Integer::parseInt)
  14.                 .collect(Collectors.toList());
  15.  
  16.         String input = sc.nextLine();
  17.         while (!"end".equals(input)) {
  18.             if (input.equals("print")) {
  19.                 numbers.forEach(n -> System.out.print(n + " "));
  20.             } else {
  21.                 Function<List<Integer>, List<Integer>> function = getFunctionCommand(input);
  22.                 numbers = function.apply(numbers);
  23.             }
  24.             input = sc.nextLine();
  25.         }
  26.  
  27.         numbers.forEach(n -> System.out.print(n + " "));
  28.     }
  29.  
  30.     private static Function<List<Integer>, List<Integer>> getFunctionCommand(String input) {
  31.         Function<List<Integer>, List<Integer>> func = null;
  32.         switch (input) {
  33.             case "add":
  34.                 List<Integer> addList = new ArrayList<>();
  35.                 func = numbers -> {
  36.                     for (Integer number : numbers) {
  37.                         addList.add(number + 1);
  38.                     }
  39.                     return addList;
  40.                 };
  41.                 break;
  42.             case "multiply":
  43.                 List<Integer> multiplyList = new ArrayList<>();
  44.                 func = numbers -> {
  45.                     for (Integer number : numbers) {
  46.                         multiplyList.add(number * 2);
  47.                     }
  48.                     return multiplyList;
  49.                 };
  50.                 break;
  51.             case "subtract":
  52.                 List<Integer> subtractList = new ArrayList<>();
  53.                 func = numbers -> {
  54.                     for (Integer number : numbers) {
  55.                         subtractList.add(number - 1);
  56.                     }
  57.                     return subtractList;
  58.                 };
  59.                 break;
  60.         }
  61.         return func;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement