Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.ArrayDeque;
  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 Agency {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12. ArrayDeque<String> ids = new ArrayDeque<>();
  13. ArrayDeque<String> agents = new ArrayDeque<>();
  14.  
  15. Arrays.stream(scanner.nextLine().split("\\s+")).forEach(ids::push);
  16. Arrays.stream(scanner.nextLine().split("\\s+")).forEach(agents::offer);
  17.  
  18. String line = scanner.nextLine();
  19.  
  20. while (!line.equalsIgnoreCase("stop")) {
  21. String[] tokens = line.split("\\s+");
  22. String command = tokens[0];
  23.  
  24. switch (command) {
  25. case "add-ID":
  26. String newId = tokens[1];
  27. ids.push(newId);
  28. break;
  29. case "add-agent":
  30. String newAgent = tokens[1];
  31. agents.offer(newAgent);
  32. break;
  33. case "remove-ID":
  34. System.out.printf("%s has been removed.%n", ids.pop());
  35. break;
  36. case "remove-agent":
  37. System.out.printf("%s has left the queue.%n", agents.pollLast());
  38. break;
  39. case "sort-ID":
  40. ids = ids.stream().sorted((x, y) -> y.compareTo(x)).collect(Collectors.toCollection(ArrayDeque::new));
  41. break;
  42. }
  43.  
  44. line = scanner.nextLine();
  45. }
  46.  
  47.  
  48. while (!ids.isEmpty() && !agents.isEmpty()) {
  49. String id = ids.pop();
  50. String agent = agents.poll();
  51.  
  52. System.out.printf("%s takes ID number: %s%n", agent, id);
  53. }
  54.  
  55. while (!ids.isEmpty()) {
  56. System.out.println("No more agents left.");
  57. System.out.print("ID number: ");
  58. System.out.printf("%s ", ids.pop());
  59. System.out.println();
  60. }
  61.  
  62. while (!agents.isEmpty()) {
  63. System.out.printf("%s does not have an ID.%n", agents.poll());
  64. }
  65.  
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement