Advertisement
damesova

XY

May 30th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.util.function.BiFunction;
  4. import java.util.function.Consumer;
  5.  
  6. public class _04_AppliedArithmetics {
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.         int[] nums = Arrays.stream(sc.nextLine()
  10.                 .split("\\s+"))
  11.                 .mapToInt(Integer::parseInt)
  12.                 .toArray();
  13.  
  14.         BiFunction<int[], Integer, int[]> addY = (x, y) -> {
  15.             for (int i = 0; i < x.length; i++) {
  16.                 x[i] += y;
  17.             }
  18.             return x;
  19.         };
  20.  
  21.         BiFunction<int[], Integer, int[]> subtractY = (x, y) -> {
  22.             for (int i = 0; i < x.length; i++) {
  23.                 x[i] -= y;
  24.             }
  25.             return x;
  26.         };
  27.  
  28.         BiFunction<int[], Integer, int[]> multiplyY = (x, y) -> {
  29.             for (int i = 0; i < x.length; i++) {
  30.                 x[i] *= y;
  31.             }
  32.             return x;
  33.         };
  34.  
  35.         Consumer<int[]> printArr = x -> {
  36.             for (int num : x) {
  37.                 System.out.print(num + " ");
  38.             }
  39.         };
  40.  
  41.         String str = "";
  42.         while (!"end".equals(str = sc.nextLine())) {
  43.             doTransformation(nums, addY, subtractY, multiplyY, printArr, str);
  44.         }
  45.     }
  46.  
  47.     private static void doTransformation(int[] nums,
  48.                                          BiFunction<int[], Integer, int[]> addY,
  49.                                          BiFunction<int[], Integer, int[]> subtractY,
  50.                                          BiFunction<int[], Integer, int[]> multiplyY,
  51.                                          Consumer<int[]> printArr,
  52.                                          String str
  53.     ) {
  54.         switch (str) {
  55.             case "add":
  56.                 addY.apply(nums, 1);
  57.                 break;
  58.             case "subtract":
  59.                 subtractY.apply(nums, 1);
  60.                 break;
  61.             case "multiply":
  62.                 multiplyY.apply(nums, 2);
  63.                 break;
  64.             case "print":
  65.                 printArr.accept(nums);
  66.                 break;
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement