In: Computer Science
NEed UML diagram for this java code:
import java.util.ArrayList;
import java.util.Scanner;
class ToDoList {
private ArrayList<Task> list;//make private array
public ToDoList() {
//this keyword refers to the current object in a method or constructor
this.list = new ArrayList<>();
}
public Task[] getSortedList() {
Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array
//fills array with given values by using a for loop
for (int i = 0; i < this.list.size(); i++) {
sortedList[i] = this.list.get(i);
}
//sorting values in array using bubble sort
Task temp;//temp var to hold sorted values
for (int i = 0; i < sortedList.length; i++) {
for (int j = 1; j < (sortedList.length - i); j++) {
if (sortedList[j].isEarlier(sortedList[j - 1])) {
temp = sortedList[j - 1];
sortedList[j - 1] = sortedList[j];
sortedList[j] = temp;
}
}
}
return sortedList;
}//end of getSortedList
public void addTask(Task task) {
this.list.add(task);//.add to add elements in the list
}
public void deleteTask(int taskID) {
//delete tasks using task ID
for (int i = 0; i < this.list.size(); i++) {
if (this.list.get(i).getTaskID() == taskID) {
this.list.remove(i);
}
}
}
public void deleteAllTasks() {
this.list.clear();
}
public int getTaskNumber() {
return this.list.size();
}
public void printList(Task[] tasks) {
System.out.println("-------------------------");
for (Task task : tasks) {
task.printTask();
}
System.out.println("-------------------------");
}
public static void displayMenu() {
System.out.println("1)Add tasks");
System.out.println("2)List tasks");
System.out.println("3)Delete Tasks");
System.out.println("4)Delete all tasks");
System.out.println("5)Exit");
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ToDoList toDoList = new ToDoList();
boolean run = true;
while (run) {//will continue to loop until user chooses a valid option
displayMenu();
int choice = in.nextInt();
switch (choice) {
//nextLine() method advances this scanner past the current line and returns the input that was skipped
case 1:
in.nextLine();
System.out.print("Task Name: ");
String taskTitle = in.nextLine();
System.out.print("Task Deadline (dd mm yyyy): ");
int taskDay = in.nextInt();
int taskMonth = in.nextInt();
int taskYear = in.nextInt();
in.nextLine();
System.out.print("Time Deadline (hh mm): ");
int taskHour = in.nextInt();
int taskMin = in.nextInt();
in.nextLine();
System.out.print("Is task Regular or Important?: ");
String taskImportant = in.nextLine();
//Task ID made with all info set up neatly
Task newTask = new Task(new Time(taskHour, taskMin), new Date(taskDay, taskMonth, taskYear), taskTitle, taskImportant, toDoList.getTaskNumber() + 1);
//Adds the new task to the list
toDoList.addTask(newTask);
//Display the sorted tasks
System.out.printf("Task %d is added. The To-Do list is as follows:\n", newTask.getTaskID());
toDoList.printList(toDoList.getSortedList());
break;
case 2:
//List all tasks
toDoList.printList(toDoList.getSortedList());
break;
case 3:
//Delete a task
toDoList.printList(toDoList.getSortedList());
in.nextLine();
System.out.print("Enter ID number to delete task: ");
int taskID = in.nextInt();
toDoList.deleteTask(taskID);
System.out.printf("\nTask %d is deleted. The new list is:\n", taskID);
toDoList.printList(toDoList.getSortedList());
break;
case 4:
//Delete all tasks
toDoList.deleteAllTasks();
System.out.println("Tasks Deleted");
break;
case 5:
//Exit program
System.out.println("Exiting program, goodbye.");
run = false;
break;
}
}
}
}
class Date {
private int day;
private int month;
private int year;
//Default constructor for date,
public Date() {
this(0, 0, 0);
}
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
//accessors and mutators
public int getDay() {
return this.day;
}
public int getMonth() {
return this.month;
}
public int getYear() {
return this.year;
}
public void setDay(int day) {
this.day = day;
}
public void setMonth(int month) {
this.month = month;
}
public void setYear(int year) {
this.year = year;
}
public String toString() {
return String.format("%d/%d/%d", this.day, this.month, this.year);
}
//Returns whether this date is earlier than another date provided as parameter. If they are the same, returns false
public boolean isEarlier(Date date) {
//If the years are the same, narrow the comparison down the months
if (this.year == date.year) {
//Repeat the same thing as years, but narrowed down for months
if (this.month == date.month) {
if (this.day == date.day) {
//Returns false because if they are the same date, then one is not earlier than the other
return false;
} else if (this.day < date.day) {
return true;
} else {
return false;
}
} else if (this.month < date.month) {
return true;
} else {
return false;
}
} else if (this.year < date.year) {
return true;
} else {
return false;
}
}
}
class Time {
private int hour;
private int min;
//Default constructor for time
public Time() {
this(0, 0);
}
public Time(int hour, int min) {
this.hour = hour;
this.min = min;
}
//Accessors and mutator methods for hour and min attributes
public int getHour() {
return this.hour;
}
public int getMin() {
return this.min;
}
public void setHour(int hour) {
this.hour = hour;
}
public void setMin(int min) {
this.min = min;
}
//Returns time in HH:MM format
public String toString() {
return String.format("%d:%d", this.hour, this.min);
}
//Returns whether or not this time is earlier than the time provided as a parameter. Returns false if both same
public boolean isEarlier(Time time) {
//If they are the same hour, narrow the comparison down to the minute
if (this.hour == time.hour) {
//Exact same process, but for min
if (this.min == time.min) {
//Returns false because if they're the same time, one is not earlier than the other
return false;
} else if (this.min < time.min) {
return true;
} else {
return false;
}
//If this hour is less than the other hour, it is earlier, so return true
} else if (this.hour < time.hour) {
return true;
//Otherwise, the time isn't earlier
} else {
return false;
}
}
}
class Task {
private Time time;
private Date date;
private String title;
private String Important;
private int taskID;
//Default constructor leaves attributes blank
public Task() {
this(null, null, null, null, 0);
}
public Task(Time time, Date date, String title, String Important, int taskID) {
this.time = time;
this.date = date;
this.title = title;
this.Important = Important;
this.taskID = taskID;
}
//accessors and mutators for time, date, title, Important, and taskID
public Time getTime() {
return this.time;
}
public Date getDate() {
return this.date;
}
public String getTitle() {
return this.title;
}
public String getImportant() {
return this.Important;
}
public int getTaskID() {
return this.taskID;
}
public void setTime(Time time) {
this.time = time;
}
public void setDate(Date date) {
this.date = date;
}
public void setTitle(String title) {
this.title = title;
}
public void setImportant(String Important) {
this.Important = Important;
}
//Prints task date
public void printTask() {
System.out.printf("* Task %d: %s, %s, %s, %s\n", this.taskID, "title: ", this.title, this.date.toString(), this.time.toString(), this.Important);
}
//Returns true if this task is earlier than task provided as parameter.
public boolean isEarlier(Task task) {
//If the other task's date is earlier than this task's date, it must be earlier
if (this.date.isEarlier(task.getDate())) {
return true;
//However, if the dates are the same, narrow the check down to the tasks' times
} else if (!this.date.isEarlier(task.getDate()) && !task.getDate().isEarlier(this.date)) {
return this.time.isEarlier(task.time);
}
return false;
}
}
Description:
Since the class Task has members Data and Time,there is a dependencey between them.After that there will be a dependency between ToDOList and Task.
This Relationship is depicted in the following UML Diagram:
