In: Computer Science
Implement and test a TaskList application in Java that will allow a client to manage information about daily tasks. A task has a task name, is scheduled on a specific day of the week and can have a comment attached to it. Together, the task name and day uniquely identify a task. There may be tasks with the same name scheduled for different days of the week. The application should allow the client to start with an empty TaskList and then manage it by adding, deleting, searching and displaying information about tasks from the TaskList according to the specifications below.
Your application should offer the client a menu with the following operations:
0. Exit.
1. Add a task.
2. Delete a task on a specified day.
3. Delete a specified task.
4. Print content of TaskList.
5. Search day for tasks.
Here is the specification of the functionality of the TaskList application:
1. Add a task: Adds information about a task to the TaskList. Requests the following input from the client: day, task name, and comment. If there is already a task with this name scheduled for that week day, the task cannot be added and the user should be notified of this fact.
2. Delete a task on a specified day: Deletes a specified task on a specified day from the TaskList. Requests the following input from the client: task name and day. The user should be notified if there is no task with that name scheduled for that day in the TaskList.
3. Delete a specified task. Deletes a specified task from any day it may appear in the TaskList. Requests the following input from the client: task name. The user should be notified if there is no task with that name in the TaskList.
4. Display content of TaskList: Displays information about all tasks in the TaskList. Requests the following input from the client: None. For each task in the TaskList, displays task name, day it is scheduled on and comment attached to it.
5. Search day for tasks: Displays information about the tasks registered for a specified day. Requests the following input from the client: day.
Hint: Make sure that you use object oriented design in your solution. In future applications the TaskList class may be used in an application used to manage several clients’ task lists. You should be able to extend your design without changes to the TaskList.
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<Task> taskList;
public TaskList()
{
this.taskList = new ArrayList<>();
}
public void addTask()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter 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!\n");
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 + "\n");
}
}
public void deleteTaskOnSpecifiedDay()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter 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("\nNo such task scheduled on day " + day +
"\n");
else
{
System.out.println(taskList.get(index).getName() + " successfully
removed!\n");
taskList.remove(index);
}
}
public void deleteSepcifiedTask()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter task name: ");
String name = sc.nextLine().trim();
// search for the task name
// add all the indices of the matching task to a list
ArrayList<Task> 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("\nNo such task found on any day!\n");
else
{
int count = tasks.size();
for(Task task : tasks)
{
taskList.remove(task);
}
System.out.println("\nTotal " + count + " such tasks were found and
deleted successfully!\n");
}
}
public void printTaskList()
{
if(taskList.isEmpty())
System.out.println("\nSorry, the list is empty. Please add some
tasks to the list!\n");
else
{
System.out.println("\n*** THE TASK LIST
***\n---------------------");
for(Task task : taskList)
{
System.out.println(task.toString());
}
System.out.println();
}
}
public void searchDayForTasks()
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter day of the week: ");
int day = Integer.parseInt(sc.nextLine().trim());
// search if any task is scheduled
ArrayList<Task> filterTasks = new ArrayList<>();
for(Task task : taskList)
{
if(task.getDay() == day)
{
filterTasks.add(task);
}
}
if(filterTasks.isEmpty())
System.out.println("\nDay " + day + " has got no tasks
scheduled!\n");
else
{
System.out.println("\nTasks 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("\nGood Bye!\n");
System.exit(0);
}
default:
System.out.println("\nInvalid choice!\n");
}
}while(choice != 0);
}
private static void printMenu()
{
System.out.print("0. Exit\n"
+ "1. Add a task\n"
+ "2. Delete a task on a specified day\n"
+ "3. Delete a specified task\n"
+ "4. Print all tasks\n"
+ "5. Search day for tasks\n"
+ "Enter your choice: ");
}
}
************************************************************************ SCREENSHOT ***************************************************
**********************************************************************************************************************************************