Advertisement
Merry123

Untitled

Oct 18th, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package Lab;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.stream.Collectors;
  9.  
  10. public class ListManipulationAdvanced {
  11.     public static void main(String[] args) {
  12.         Scanner sc = new Scanner(System.in);
  13.  
  14.         /*
  15.         Now we will implement more complicated list commands. Again, read a list, and until you receive "end" read commands:
  16.             Contains {number} – check if the list contains the number. If yes, print "Yes", otherwise, print "No such number"
  17.             Print even – print all the numbers that are even separated by a space
  18.             Print odd – print all the numbers that are oddly separated by a space
  19.             Get sum – print the sum of all the numbers
  20.             Filter {condition} {number} – print all the numbers that fulfill that condition. The condition will be either '<', '>', ">=", "<="
  21.  
  22.          */
  23.  
  24.         List<Integer> numbersList = Arrays.stream(sc.nextLine().split(" "))
  25.                 .map(Integer::parseInt)
  26.                 .collect(Collectors.toList());
  27.  
  28.         String input = sc.nextLine();
  29.         while (!input.equals("end")) {
  30.             String[] commandParts = input.split(" ");
  31.             switch (commandParts[0]) {
  32.                 case "Contains":
  33.                     printWhetherContainsNumber(numbersList, commandParts[1]);
  34.                     break;
  35.                 case "Print":
  36.                     printEvenOrOdd(numbersList, commandParts[1]);
  37.                     break;
  38.                 case "Get":
  39.                     printSum(numbersList);
  40.                     break;
  41.                 case "Filter":
  42.                     filter(numbersList, commandParts[1],commandParts[2]);
  43.                     break;
  44.             }
  45.  
  46.             input = sc.nextLine();
  47.         }
  48.     }
  49.  
  50.     public static void printWhetherContainsNumber(List<Integer> list, String n) {
  51.         if (list.contains(Integer.parseInt(n))) {
  52.             System.out.println("Yes");
  53.         } else {
  54.             System.out.println("No such number");
  55.         }
  56.     }
  57.  
  58.     public static void printEvenOrOdd(List<Integer> list, String evenOrOdd) {
  59.         if (evenOrOdd.equals("even")) {
  60.             for (int i : list) {
  61.                 if (i % 2 == 0) {
  62.                     System.out.print(i + " ");
  63.                 }
  64.             }
  65.         } else {
  66.             for (int i : list) {
  67.                 if (i % 2 != 0) {
  68.                     System.out.print(i + " ");
  69.                 }
  70.             }
  71.         }
  72.         System.out.println();
  73.     }
  74.  
  75.     public static void printSum(List<Integer> list) {
  76.         int sum = 0;
  77.         for (int i : list) {
  78.             sum += i;
  79.         }
  80.         System.out.println(sum);
  81.     }
  82.  
  83.     public static void filter(List<Integer> list, String condition, String number) {
  84.         List<Integer> copyList = new ArrayList<>(list);
  85.  
  86.         int num = Integer.parseInt(number);
  87.         switch (condition) {
  88.             case "<":
  89.                 copyList.removeIf(e -> e >= num);
  90.                 break;
  91.             case ">":
  92.                 copyList.removeIf(e -> e <= num);
  93.                 break;
  94.             case ">=":
  95.                 copyList.removeIf(e -> e < num);
  96.                 break;
  97.             case "<=":
  98.                 copyList.removeIf(e -> e > num);
  99.                 break;
  100.         }
  101.         printList(copyList);
  102.     }
  103.     public static void printList(List<Integer> list) {
  104.         for (int i : list) {
  105.             System.out.print(i + " ");
  106.         }
  107.         System.out.println();
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement