Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 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. for (j = 0; j < 7; j++) {
  35. office.get(service).add(names[j]);
  36. }
  37. }
  38.  
  39. // Printing newly-created queues. <----------- why is it not the same way I added them?
  40. System.out.println(office.get("bank"));
  41. System.out.println(office.get("mail"));
  42. System.out.println(office.get("payment"));
  43. System.out.println("----- Calls -----");
  44.  
  45. // Poll 2 people from bank queue
  46. for (i = 0; i < 2; i++) {
  47. System.out.println("Next in queue for bank: " + office.get("bank").poll());
  48. }
  49.  
  50. // Poll 3 people from mail queue
  51. for (i = 0; i < 3; i++) {
  52. System.out.println("Next in queue for mail: " + office.get("mail").poll());
  53. }
  54.  
  55. // Poll 1 person from payment queue
  56. System.out.println("Next in queue for payment: " + office.get("payment").poll());
  57.  
  58.  
  59. // Shows the remaining size of each queue
  60. System.out.println("----- Sizes -----");
  61. System.out.println("bank: " + office.get("bank").size());
  62. System.out.println("mail: " + office.get("mail").size());
  63. System.out.println("payment: " + office.get("payment").size());
  64. }
  65.  
  66. }
  67.  
  68.  
  69. --------------- OUTPUT ---------------
  70. [Daniel, Jeremy, David, Tzahi, Ofir, Lotem, Elad]
  71. [Daniel, Jeremy, David, Tzahi, Ofir, Lotem, Elad]
  72. [Daniel, Jeremy, David, Tzahi, Ofir, Lotem, Elad]
  73. ----- Calls -----
  74. Next in queue for bank: Daniel
  75. Next in queue for bank: David
  76. Next in queue for mail: Daniel
  77. Next in queue for mail: David
  78. Next in queue for mail: Elad
  79. Next in queue for payment: Daniel
  80. ----- Sizes -----
  81. bank: 5
  82. mail: 4
  83. payment: 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement