In: Computer Science
java code: adds a new regular task, delete a task , show all tasks, and show regular tasks, mark a task as important (possibly through ID), complete task, show all completed tasks, show important tasks.
I also need a UML diagram for the code
Task.java
public class Task {
private String name, importance, status;
public Task()
{
this.name = this.importance = this.status = "";
}
public Task(String name, String importance, String status)
{
this.name = name;
this.importance = importance;
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImportance() {
return importance;
}
public void setImportance(String importance) {
this.importance = importance;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString()
{
return("Task: " + getName() + ", Important: " + getImportance() +
", Completed: " + getStatus());
}
}
TaskListMain.java (Main class)
import java.util.ArrayList;
import java.util.Scanner;
public class TaskListMain {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
ArrayList<Task> taskList = new ArrayList<>();
displayMainMenu();
performAction(taskList);
}
private static void displayMainMenu()
{
System.out.print("COMMAND MENU:\n"
+ "list - List all tasks\n"
+ "add - Add a task\n"
+ "complete - Complete a task\n"
+ "delete - Delete a task\n"
+ "exit - Exit program\n\n");
}
private static void performAction(ArrayList<Task> list)
{
Scanner sc = new Scanner(System.in);
String command;
do
{
System.out.print("Command: ");
command = sc.nextLine().trim();
switch(command)
{
case "list":
{
if(list.isEmpty())
System.out.println("No tasks to show!\n");
else
{
for(int i = 0; i < list.size(); i++)
{
System.out.println((i + 1) + ". " + list.get(i));
}
System.out.println();
}
break;
}
case "add":
{
System.out.print("Description: ");
String name = sc.nextLine().trim();
System.out.print("Important? [y/n]: ");
char imp = sc.nextLine().trim().charAt(0);
while(imp != 'Y' && imp != 'y' && imp != 'N'
&& imp != 'n')
{
System.out.println("Invalid selection!");
System.out.print("Important? [y/n]: ");
imp = sc.nextLine().trim().charAt(0);
}
if(imp == 'Y' || imp == 'y')
list.add(new Task(name, "Yes", "Not completed"));
else
list.add(new Task(name, "No", "Not completed"));
System.out.println();
break;
}
case "complete":
{
System.out.print("Number: ");
int taskNumber = Integer.parseInt(sc.nextLine().trim());
while(taskNumber <= 0 || taskNumber > list.size() - 1)
{
System.out.println("Please enter a valid task number: ");
taskNumber = Integer.parseInt(sc.nextLine().trim());
}
list.get(taskNumber - 1).setStatus("complete");
System.out.println();
break;
}
case "delete":
{
System.out.print("Number: ");
int taskNumber = Integer.parseInt(sc.nextLine().trim());
while(taskNumber <= 0 || taskNumber > list.size())
{
System.out.println("Please enter a valid task number: ");
taskNumber = Integer.parseInt(sc.nextLine().trim());
}
list.remove(taskNumber - 1);
System.out.println();
break;
}
case "exit":
{
System.out.println("\nThank you, Goodbye!\n");
System.exit(0);
}
default:
System.out.println("\nInvalid command!\n");
}
}while(!command.equalsIgnoreCase("exit"));
}
}
*********************************************************** SCREENSHOT *****************************************************
UML DIAGRAM :