-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToDoList.java
More file actions
114 lines (114 loc) · 3.31 KB
/
ToDoList.java
File metadata and controls
114 lines (114 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
/*
* @author Tien N.
*/
public class ToDoList {
private Queue<Task> list;
/**
* default constructor creates a new Priority Queue to hold task items
*/
public ToDoList() {
this.list = new PriorityQueue<>();
}
/**
* @param a String of input for adding a task
* @return true if the task is added successfully to the Prioriry Queue list
* @return false if String is in invalid format or already exits in the list
* @throws InvalidInputException
*/
public boolean addTask(String task) throws InvalidInputException{
boolean added = false;
String[] strArray = task.split("\\s+");
// check if command is valid in this format,
// ["add"] [priority number] [description]
if (strArray.length < 3) {
throw new InvalidInputException ("! Invalid command."
+ "\nPlease check priority number, task description and try again.");
}
// if format passes the first check
// try to parseInt index 1 to check if task has a priority number
try {
int priority = Integer.parseInt(strArray[1]);
String description = "";
for (int i = 2; i<strArray.length; i++) {
description += strArray[i] + " ";
}
// if it's an integer, check the bounds from 1 to 9
// if it's an integer from 1 to 9, check the description for duplicated
if (priority >= 1 && priority <= 9) {
Task newTask = new Task(priority, description.trim());
int count = list.size();
Iterator<Task> it = list.iterator();
while (it.hasNext()) {
if (newTask.equals(it.next())) {
// if found duplicated description, throw a warning message
throw new InvalidInputException("! Task '" + description.trim() + "' already exists in the list.");
}
else {
count--;
}
}
// count = 0 means no duplicated
if (count == 0) {
list.add(newTask);
added = true;
}
}
// out of bounds 1 to 9, throw a warning message
else {
throw new InvalidInputException("! The priority must be an integer between 1 and 9.");
}
}
// throw a warning message if it's not an integer
catch (NumberFormatException e) {
throw new InvalidInputException("! The priority must be an integer between 1 and 9.");
}
return added;
}
/**
* if the list is empty, throw a warning message
* otherwise, return the top most urgent task in the list based on its priority number
* @return the most priority Task object
* @throws InvalidInputException
*/
public Task nextTask() throws InvalidInputException{
if (list.isEmpty()) {
throw new InvalidInputException("! There are no tasks in the list.");
}
else {
return list.poll();
}
}
/**
* return true/false if the Priority Queue is empty
* @return true if it's empty
* @return false if it's not empty
*/
public boolean isEmpty() {
if (this.list.isEmpty()) {
return true;
}
else {
return false;
}
}
@Override
/**
* description all items in the list
*/
public String toString() {
Iterator<Task> it = list.iterator();
String str = "";
if (list.isEmpty()) {
str = "! There are no tasks in the list.";
}
else {
while (it.hasNext()) {
str += "~" + it.next() + "\n";
}
}
return str;
}
}//end of class