Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class App {
- public static void main(String[] args) {
- Hashtable<String, Queue<String>> office = new Hashtable<String, Queue<String>>();
- String[] names = new String[] { "Ofir", "Jeremy", "Lotem", "Tzahi", "Daniel", "David", "Elad" };
- String service = null;
- int i, j;
- // Add 3 cells to office Hashtable
- office.put("bank", new PriorityQueue<String>());
- office.put("mail", new PriorityQueue<String>());
- office.put("payment", new PriorityQueue<String>());
- /*
- This loop sets the queue type by the value of i (there are 3 queues).
- The inner loop adds the people from names array to the desired queue.
- */
- for (i = 0; i < 3; i++) {
- switch (i) {
- case 0:
- service = "bank";
- break;
- case 1:
- service = "mail";
- break;
- case 2:
- service = "payment";
- break;
- }
- System.out.print(service + " queue: ");
- for (j = 0; j < 7; j++) {
- office.get(service).add(names[j]);
- System.out.print(names[j] + " ");
- }
- System.out.println();
- }
- // Printing newly-created queues. <----------- why is it not the same way I added them?
- System.out.println("----- Calls -----");
- // Poll 2 people from bank queue
- for (i = 0; i < 2; i++) {
- System.out.println("Next in queue for bank: " + office.get("bank").poll());
- }
- // Poll 3 people from mail queue
- for (i = 0; i < 3; i++) {
- System.out.println("Next in queue for mail: " + office.get("mail").poll());
- }
- // Poll 1 person from payment queue
- System.out.println("Next in queue for payment: " + office.get("payment").poll());
- // Shows the remaining size of each queue
- System.out.println("----- Sizes -----");
- System.out.println("bank: " + office.get("bank").size());
- System.out.println("mail: " + office.get("mail").size());
- System.out.println("payment: " + office.get("payment").size());
- }
- }
- ************ OUTPUT ************
- bank queue: Ofir Jeremy Lotem Tzahi Daniel David Elad
- mail queue: Ofir Jeremy Lotem Tzahi Daniel David Elad
- payment queue: Ofir Jeremy Lotem Tzahi Daniel David Elad
- ----- Calls -----
- Next in queue for bank: Daniel
- Next in queue for bank: David
- Next in queue for mail: Daniel
- Next in queue for mail: David
- Next in queue for mail: Elad
- Next in queue for payment: Daniel
- ----- Sizes -----
- bank: 5
- mail: 4
- payment: 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement