RGAlex

Test4

Apr 6th, 2021
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.IntStream;
  4.  
  5. public class DeskOfCards {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         List<String> list = Arrays.stream(scan.nextLine().split(", ")).collect(Collectors.toList());
  10.         int n = Integer.parseInt(scan.nextLine());
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             List<String> commands = Arrays.stream(scan.nextLine().split(", ")).collect(Collectors.toList());
  14.             switch (commands.get(0)) {
  15.                 case "Add":
  16.                     if (list.contains(commands.get(1))) {
  17.                         System.out.println("Card is already in the desk");
  18.                     } else {
  19.                         System.out.println("Card successfully added");
  20.                         list.add(commands.get(1));
  21.                     }
  22.                     break;
  23.                 case "Remove":
  24.                     if (list.contains(commands.get(1))) {
  25.                         list.remove(commands.get(1));
  26.                         System.out.println("Card successfully removed");
  27.                     } else {
  28.                         System.out.println("Card not found");
  29.                     }
  30.                     break;
  31.                 case "Remove At":
  32.                     if (Integer.parseInt(commands.get(1)) > list.size() - 1 || Integer.parseInt(commands.get(1)) < 0) {
  33.                         System.out.println("Index out of range");
  34.                     } else {
  35.                         System.out.println("Card successfully removed");
  36.                         list.remove(Integer.parseInt(commands.get(1)));
  37.                     }
  38.                     break;
  39.                 case "Insert":
  40.                     if (!(Integer.parseInt(commands.get(1)) > list.size() - 1 || Integer.parseInt(commands.get(1)) < 0)) {
  41.                         if (!(list.contains(commands.get(2)))) {
  42.                             System.out.println("Card successfully added");
  43.                             list.add(Integer.parseInt(commands.get(1)), commands.get(2));
  44.  
  45.                         } else {
  46.                             System.out.println("Card is already added");
  47.                         }
  48.                     } else {
  49.                         System.out.println("Index out of range");
  50.                     }
  51.             }
  52.         }
  53.         IntStream.range(0, list.size()).forEach(i -> {
  54.             if (i == list.size() - 1) {
  55.                 System.out.print(list.get(i));
  56.             } else {
  57.                 System.out.print(list.get(i) + ", ");
  58.             }
  59.         });
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment