Question: I need this code to give me this output: Code: import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Iterator; import java.util.PriorityQueue; import java.util.Scanner; import java.lang.Integer; enum Priority {
I need this code to give me this output:
Code:
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Iterator; import java.util.PriorityQueue; import java.util.Scanner; import java.lang.Integer; enum Priority { URGENT, HIGH, NORMAL, LOW } enum Status { NOT_STARTED, IN_PROGRESS, WAITING, DEFERRED } class Task implements Comparable { int taskId; String subject; Priority priority; Status status; LocalDate startDate; LocalDate dueDate; public Task(int taskId, String subject, Priority priority, Status status, LocalDate startDate, LocalDate dueDate) { this.taskId = taskId; this.subject = subject; this.priority = priority; this.status = status; this.startDate = startDate; this.dueDate = dueDate; } public int getTaskId() { return taskId; } @Override public String toString() { return "Id:" + taskId + "; Subject: " + subject + "; Status: " + status + "; Priority: " + priority + "; StartDate: " + startDate.toString() + "; Due date: " + dueDate; } public Priority getPriority() { return priority; } @Override public int compareTo(Task task) { return this.getPriority().compareTo(task.getPriority()); } } public class ToDoListApplication { PriorityQueue taskPriorityQueue = new PriorityQueue<>(); Scanner scanner = new Scanner(System.in); int taskID = 0; public static void main(String[] args) { ToDoListApplication app = new ToDoListApplication(); app.testPriorityQueue(); } public void displayNextTask() { if(taskPriorityQueue.isEmpty()) { System.out.println("No tasks in the queue."); } else { System.out.println(taskPriorityQueue.peek()); taskPriorityQueue.poll(); } } public void showTaskDetail(int taskId) { var it = taskPriorityQueue.iterator(); while (it.hasNext()) { Task task = getTaskById(taskId); if (task.getTaskId() == taskId) { System.out.println(task); return; } } System.out.println("TaskId not found..."); } public Task getTaskById(int taskId) { for (Task task : taskPriorityQueue) { if (task.getTaskId () == taskId) return task; System.out.println ( "TaskId not found..." ); } return null; } public void removeTask(int taskId) { Iterator it = taskPriorityQueue.iterator(); while (it.hasNext()) { Task task = it.next(); if (task.getTaskId() == taskId) { it.remove(); return ; } } System.out.println("TaskId not found..."); } public void testPriorityQueue() { String menuItem; System.out.println("Welcome to My Task List "); do { System.out.println("Choose action (Add(a), Next(n), List(l), Detail(d), Edit(e), Remove(r), Quit(q):"); menuItem = scanner.nextLine(); switch (menuItem) { case "a": addTask(); break; case "n": displayNextTask(); break; case "l": showTaskSummaryList(); break; case "d": System.out.println("Enter taskId: "); int taskId = Integer.parseInt(scanner.nextLine()); showTaskDetail(taskId); break; case "r": System.out.println("Enter taskId: "); taskId = Integer.parseInt(scanner.nextLine()); removeTask(taskId); break; case "q": break; default: throw new IllegalStateException ( "Unexpected value: " + menuItem ); } } while (!menuItem.equals("q")); System.out.println("Goodbye"); } void showTaskSummaryList() { for (Task task: taskPriorityQueue) System.out.println(task); } private Priority scanForPriority() { Priority priority = Priority.NORMAL; System.out.println("Enter priority abbreviation Normal=n,Low=l,High=h,Urgent=u): ");String abbrev = scanner.nextLine(); switch (abbrev) { case "n" -> { } case "l" -> priority = Priority.LOW; case "h" -> priority = Priority.HIGH; case "u" -> priority = Priority.URGENT; } return priority; } void addTask() { System.out.println("Adding new Task..."); taskID++; System.out.println("Enter subject:"); Scanner scanner = new Scanner(System.in); String subject = scanner.nextLine(); System.out.println("Enter due date (yyyy-MM-dd):"); String input = scanner.nextLine(); DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern ( "yyyy-MM-dd" ); LocalDate dueDate = LocalDate.parse(input); Priority priority = scanForPriority(); Status status = Status.NOT_STARTED; LocalDate date = LocalDate.now(); LocalDate startDate = LocalDate.now(); Task task = new Task(taskID, subject, priority, status, startDate, dueDate); var add = taskPriorityQueue.add ( task ); } } Sample Output
Welcome to My Task List
Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): a Adding new Task... Enter subject: Square the circle Enter due date (yyyy-MM-dd): 2020-11-31 Date format exception. Try again. Enter due date (yyyy-MM-dd): 2020-10-31 Enter priority abbreviation Normal=n,Low=l,High=h,Urgent=u): l Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): a Adding new Task... Enter subject: Remove the rubble Enter due date (yyyy-MM-dd): 2020-11-20 Enter priority abbreviation Normal=n,Low=l,High=h,Urgent=u): n Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): a Adding new Task... Enter subject: Drive the drivers Enter due date (yyyy-MM-dd): 2020-11-15 Enter priority abbreviation Normal=n,Low=l,High=h,Urgent=u): h Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): a Adding new Task... Enter subject: Circle the square Enter due date (yyyy-MM-dd): 2021-11-05 Enter priority abbreviation Normal=n,Low=l,High=h,Urgent=u): u Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): l Id:4; Subject: Circle the square; Status: Not Started; Priority: Urgent; StartDate: 2020-10-30; Due date: 2021-11-05 Id:3; Subject: Drive the drivers; Status: Not Started; Priority: High; StartDate: 2020-10-30; Due date: 2020-11-15 Id:2; Subject: Remove the rubble; Status: Not Started; Priority: Normal; StartDate: 2020-10-30; Due date: 2020-11-20 Id:1; Subject: Square the circle; Status: Not Started; Priority: Low; StartDate: 2020-10-30; Due date: 2020-10-31 Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): n Id:4; Subject: Circle the square; Status: Not Started; Priority: Urgent; StartDate: 2020-10-30; Due date: 2021-11-05 Is this task complete? y/n: y Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): l Id:3; Subject: Drive the drivers; Status: Not Started; Priority: High; StartDate: 2020-10-30; Due date: 2020-11-15 Id:1; Subject: Square the circle; Status: Not Started; Priority: Low; StartDate: 2020-10-30; Due date: 2020-10-31 Id:2; Subject: Remove the rubble; Status: Not Started; Priority: Normal; StartDate: 2020-10-30; Due date: 2020-11-20 Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): e Enter taskId: 2 TaskId not found... TaskId not found... Edit Task: Enter new subject or press enter to leave unchanged: Remove the rabble Enter new due date (yyyy-MM-dd), or press enter to leave unchanged:
Enter status abbreviation Not Started=n,Deferred=d,Waiting=w,Complete=c): u Enter priority abbreviation Normal=n,Low=l,High=h,Urgent=u): l Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): l Id:3; Subject: Drive the drivers; Status: Not Started; Priority: High; StartDate: 2020-10-30; Due date: 2020-11-15 Id:1; Subject: Square the circle; Status: Not Started; Priority: Low; StartDate: 2020-10-30; Due date: 2020-10-31 Id:2; Subject: Remove the rabble; Status: Not Started; Priority: Low; StartDate: 2020-10-30; Due date: 2020-11-20 Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): 2 Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): e Enter taskId: 2 TaskId not found... TaskId not found... Edit Task: Enter new subject or press enter to leave unchanged:
Enter new due date (yyyy-MM-dd), or press enter to leave unchanged:
Enter status abbreviation Not Started=n,Deferred=d,Waiting=w,Complete=c): w Enter priority abbreviation Normal=n,Low=l,High=h,Urgent=u): u Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): l Id:3; Subject: Drive the drivers; Status: Not Started; Priority: High; StartDate: 2020-10-30; Due date: 2020-11-15 Id:1; Subject: Square the circle; Status: Not Started; Priority: Low; StartDate: 2020-10-30; Due date: 2020-10-31 Id:2; Subject: Remove the rabble; Status: Waiting; Priority: Urgent; StartDate: 2020-10-30; Due date: 2020-11-20 Choose action (Add(a),Next(n),List(l),Detail(d),Edit(e),Remove(r),Quit(q): q Goodbye
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
