Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class App {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Hashtable<String, Queue<String>> office = new Hashtable<String, Queue<String>>();
  8. String[] names = new String[] { "Ofir", "Jeremy", "Lotem", "Tzahi", "Daniel", "David", "Elad" };
  9. String service = null;
  10. int i, j;
  11.  
  12. // Add 3 cells to office Hashtable
  13. office.put("bank", new PriorityQueue<String>());
  14. office.put("mail", new PriorityQueue<String>());
  15. office.put("payment", new PriorityQueue<String>());
  16.  
  17. /*
  18. This loop sets the queue type by the value of i (there are 3 queues).
  19. The inner loop adds the people from names array to the desired queue.
  20. */
  21. for (i = 0; i < 3; i++) {
  22. switch (i) {
  23. case 0:
  24. service = "bank";
  25. break;
  26. case 1:
  27. service = "mail";
  28. break;
  29. case 2:
  30. service = "payment";
  31. break;
  32. }
  33.  
  34. System.out.print(service + " queue: ");
  35. for (j = 0; j < 7; j++) {
  36. office.get(service).add(names[j]);
  37. System.out.print(names[j] + " ");
  38. }
  39. System.out.println();
  40. }
  41.  
  42. // Printing newly-created queues. <----------- why is it not the same way I added them?
  43.  
  44. System.out.println("----- Calls -----");
  45.  
  46. // Poll 2 people from bank queue
  47. for (i = 0; i < 2; i++) {
  48. System.out.println("Next in queue for bank: " + office.get("bank").poll());
  49. }
  50.  
  51. // Poll 3 people from mail queue
  52. for (i = 0; i < 3; i++) {
  53. System.out.println("Next in queue for mail: " + office.get("mail").poll());
  54. }
  55.  
  56. // Poll 1 person from payment queue
  57. System.out.println("Next in queue for payment: " + office.get("payment").poll());
  58.  
  59.  
  60. // Shows the remaining size of each queue
  61. System.out.println("----- Sizes -----");
  62. System.out.println("bank: " + office.get("bank").size());
  63. System.out.println("mail: " + office.get("mail").size());
  64. System.out.println("payment: " + office.get("payment").size());
  65. }
  66.  
  67. }
  68.  
  69.  
  70. ************ OUTPUT ************
  71.  
  72. bank queue: Ofir Jeremy Lotem Tzahi Daniel David Elad
  73. mail queue: Ofir Jeremy Lotem Tzahi Daniel David Elad
  74. payment queue: Ofir Jeremy Lotem Tzahi Daniel David Elad
  75. ----- Calls -----
  76. Next in queue for bank: Daniel
  77. Next in queue for bank: David
  78. Next in queue for mail: Daniel
  79. Next in queue for mail: David
  80. Next in queue for mail: Elad
  81. Next in queue for payment: Daniel
  82. ----- Sizes -----
  83. bank: 5
  84. mail: 4
  85. payment: 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement