Question
Implementing priority Queue Using LinkedList
Write a Java program that keeps track of your To-Do list for a given day. The list has a pre-set limit of 10. Use pop-up windows for user interactions (input/output). You should display a menu like the following:
Each chore has data/message item and a numeric/priority item. The list is formed as a Priority Queue. That means, entered sequentially, the following chores:
Message: Call Mom, Priority: 2
Message: Return Library Book, Priority: 2
Message: Pickup Paycheck, Priority: 1
Message: Start Working on Java Homework, Priority: 3
Message: Pay Phone Bill, Priority: 1
should be recorded as:
Message: Pickup Paycheck, Priority: 1
Message: Pay Phone Bill, Priority: 1
Message: Call Mom, Priority: 2
Message: Return Library Book, Priority: 2
Message: Start Working on Java Homework, Priority: 3
The menu operations are largely what it says, and as it was demonstrated in class. Unlike, as it was demonstrated in class, all your interactions with user should be through pop-up windows.
Things To Do List for l'Your Name] 1 Insert an entry 2 show the first entry Remove the first entry Length of queue 5: Is it full 6: Is it empty 7:Empty the queue 8:Display queue contents 0: Quit Enter your option:
Explanation / Answer
Writing a Java program for given task involves following high level steps. a) Defining a POJO (i.e. ToDoItem) to hold information of a to do item. b) Implementing a Priority Queue class with operations such as insert, get, remove entry using Java LinkedList. c) Implementing a ToDoApp which provides required menu operations as a pop up dialog. import javax.swing.*; import java.util.LinkedList; class ToDoItem { String message; int priority; public ToDoItem(String message, int priority) { this.message = message; this.priority = priority; } public String getMessage() { return message; } public int getPriority() { return priority; } } class PriorityQueue { LinkedList list = new LinkedList(); private static final int MAX_ITEMS = 10; public Boolean insert(String message, int priority) { if (isFull()) return false; int index = 0; for (ToDoItem entry : list) { if (priority >= entry.getPriority()) { break; } index++; } list.add(index, new ToDoItem(message, priority)); return true; } public String getMessage(int index) { if (isEmpty() || (index >= list.size())) return null; return list.get(list.size() - index - 1).getMessage(); } public Boolean remove() { if (isEmpty()) return false; list.removeLast(); return true; } public int size() { return list.size(); } public Boolean isFull() { return (list.size() >= MAX_ITEMS); } public Boolean isEmpty() { return list.isEmpty(); } public void clear() { list.clear(); } } public class ToDoApp { private static PriorityQueue pq = new PriorityQueue(); private static int getOption(String text) { int option = -1; if (text == null) return option; try { option = Integer.parseInt(text); } catch (NumberFormatException e) { handleError("Please enter a valid option [0-8]."); } return option; } private static void showMessage(String message) { JOptionPane.showMessageDialog(null, message); } private static void handleError(String errorMessage) { JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.ERROR_MESSAGE); } private static void handleInsertEntry() { if (pq.isFull()) { showMessage("Queue is full !"); return; } JTextField message = new JTextField(); JTextField priority = new JTextField(); Object[] entry = { "Message:", message, "Priority:", priority }; while (true) { int option = JOptionPane.showConfirmDialog(null, entry, "Insert Entry", JOptionPane.OK_CANCEL_OPTION); if (option == JOptionPane.OK_OPTION) { if (message.getText().isEmpty()) { handleError("Please enter a non empty message."); } else if (priority.getText().isEmpty()) { handleError("Please enter a non empty priority."); } else { try { pq.insert(message.getText(), Integer.parseInt(priority.getText())); return; } catch (NumberFormatException e) { handleError("Please enter a valid priority."); } } } else { return; } } } private static void handleShowFirstEntry() { if (pq.isEmpty()) { showMessage("Queue is empty."); } else { showMessage("First entry: " + pq.getMessage(0)); } } private static void handleRemoveFirstEntry() { if (pq.isEmpty()) { showMessage("Queue is empty."); } else { pq.remove(); showMessage("Removed first entry."); } } private static void handleQueueLength() { showMessage("Queue size is " + pq.size() + "."); } private static void handleIsFull() { String message = "Queue is NOT full."; if (pq.isFull()) { message = "Queue is full !"; } showMessage(message); } private static void handleIsEmpty() { String message = "Queue is NOT empty."; if (pq.isEmpty()) { message = "Queue is empty !"; } showMessage(message); } private static void handleClearQueue() { pq.clear(); showMessage("Queue is emptied."); } private static void handleDisplayQueue() { if (pq.isEmpty()) { showMessage("Queue is empty."); } else { StringBuilder messages = new StringBuilder("Queue contents: "); for (int i = 0; i