desislava_topuzakova

03. House Party

Oct 16th, 2021
1,699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package Lists_Exercise;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class HouseParty_03 {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         int countCommands = Integer.parseInt(scanner.nextLine());
  13.         List<String> guestNames = new ArrayList<>();
  14.         for (int count = 1; count <= countCommands; count++) {
  15.             String command = scanner.nextLine();
  16.             //•   "{name} is going!" -> split -> ["{name}", "is", "going!"] -> collect -> {"{name}", "is", "going!"} -> 3 елемента
  17.             //•   "{name} is not going!" -> split -> ["{name}", "is", "not", "going!"] -> collect -> {"{name}", "is", "not", "going!"} -> 4 елемента
  18.             List<String> commandParts = Arrays.stream(command.split(" ")).collect(Collectors.toList());
  19.             String name = commandParts.get(0);
  20.             //is going -> има или го няма
  21.             if (commandParts.size() == 3) { //GOING
  22.                 if (guestNames.contains(name)) {
  23.                     System.out.printf("%s is already in the list!%n", name);
  24.                 } else {
  25.                     guestNames.add(name);
  26.                 }
  27.             }
  28.             //is not going -> има (премахвам) или няма
  29.             else if (commandParts.size() == 4) { //NOT GOING
  30.                 if (guestNames.contains(name)) {
  31.                     guestNames.remove(name);
  32.                 } else {
  33.                     System.out.printf("%s is not in the list!%n", name);
  34.                 }
  35.             }
  36.         }
  37.         for (String name : guestNames) {
  38.             System.out.println(name);
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment