Question: //need to edit the day portion of the code to put in strings likemonday tuesday instead of number as it does now Task.java public class
//need to edit the day portion of the code to put in strings like"monday" "tuesday" instead of number as it does now
Task.java
public class Task { private String name, comment; private int day; public Task() { this.name = ""; this.day = 0; this.comment = ""; }
public Task(int day, String name, String comment) { this.name = name; this.comment = comment; this.day = day; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getComment() { return comment; }
public void setComment(String comment) { this.comment = comment; }
public int getDay() { return day; }
public void setDay(int day) { this.day = day; } @Override public String toString() { return("Day of the week: " + this.day + ", Task Name: " + this.name + ", Comment: " + this.comment); } }
TaskList.java
import java.util.ArrayList; import java.util.Scanner;
public class TaskList { private ArrayList taskList; public TaskList() { this.taskList = new ArrayList<>(); } public void addTask() { Scanner sc = new Scanner(System.in); System.out.print(" Enter day of the week: "); int day = Integer.parseInt(sc.nextLine().trim()); System.out.print("Enter task name: "); String name = sc.nextLine().trim(); String comment; // search for the day and the task name boolean found = false; for(Task t : taskList) { if(day == t.getDay() && name.equalsIgnoreCase(t.getName())) { found = true; break; } } if(found) System.out.println("A similar task is already scheduled! "); else { System.out.print("Enter a comment for the task: "); comment = sc.nextLine().trim(); taskList.add(new Task(day, name, comment)); System.out.println(name + " is successfully scheduled on day " + day + " "); } } public void deleteTaskOnSpecifiedDay() { Scanner sc = new Scanner(System.in); System.out.print(" Enter day of the week: "); int day = Integer.parseInt(sc.nextLine().trim()); System.out.print("Enter task name: "); String name = sc.nextLine().trim(); // search for the day and the task name boolean found = false; int index = 0; for(int i = 0; i < taskList.size(); i++) { if(day == taskList.get(i).getDay() && name.equalsIgnoreCase(taskList.get(i).getName())) { found = true; index = i; break; } } if(!found) System.out.println(" No such task scheduled on day " + day + " "); else { System.out.println(taskList.get(index).getName() + " successfully removed! "); taskList.remove(index); } } public void deleteSepcifiedTask() { Scanner sc = new Scanner(System.in); System.out.print(" Enter task name: "); String name = sc.nextLine().trim(); // search for the task name // add all the indices of the matching task to a list ArrayList tasks = new ArrayList<>(); for(int i = 0; i < taskList.size(); i++) { if(name.equalsIgnoreCase(taskList.get(i).getName())) { tasks.add(taskList.get(i)); } } if(tasks.isEmpty()) System.out.println(" No such task found on any day! "); else { int count = tasks.size(); for(Task task : tasks) { taskList.remove(task); } System.out.println(" Total " + count + " such tasks were found and deleted successfully! "); } } public void printTaskList() { if(taskList.isEmpty()) System.out.println(" Sorry, the list is empty. Please add some tasks to the list! "); else { System.out.println(" *** THE TASK LIST *** ---------------------"); for(Task task : taskList) { System.out.println(task.toString()); } System.out.println(); } } public void searchDayForTasks() { Scanner sc = new Scanner(System.in); System.out.print(" Enter day of the week: "); int day = Integer.parseInt(sc.nextLine().trim()); // search if any task is scheduled ArrayList filterTasks = new ArrayList<>(); for(Task task : taskList) { if(task.getDay() == day) { filterTasks.add(task); } } if(filterTasks.isEmpty()) System.out.println(" Day " + day + " has got no tasks scheduled! "); else { System.out.println(" Tasks scheduled on day " + day + ":"); for(Task task : filterTasks) { System.out.println(task.toString()); } System.out.println(); } } }
TaskApp.java (Driver class)
import java.util.Scanner;
public class TaskApp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); TaskList taskList = new TaskList(); int choice; do { printMenu(); choice = Integer.parseInt(sc.nextLine().trim()); switch(choice) { case 1: { taskList.addTask(); break; } case 2: { taskList.deleteTaskOnSpecifiedDay(); break; } case 3: { taskList.deleteSepcifiedTask(); break; } case 4: { taskList.printTaskList(); break; } case 5: { taskList.searchDayForTasks(); break; } case 0: { System.out.println(" Good Bye! "); System.exit(0); } default: System.out.println(" Invalid choice! "); } }while(choice != 0); } private static void printMenu() { System.out.print("0. Exit " + "1. Add a task " + "2. Delete a task on a specified day " + "3. Delete a specified task " + "4. Print all tasks " + "5. Search day for tasks " + "Enter your choice: "); } }