Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added way for user to specify priorities of tasks #6

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class Deadline extends Task {
* @param description Description of the task.
* @param deadline deadline of the task.
*/
public Deadline(String description, String deadline) {
super(description);
public Deadline(String description, String deadline, Priority priority) {
super(description, priority);

this.deadlineString = makeDeadline(deadline);
this.deadlineDate = storeAsDateTime(deadlineString);
Expand All @@ -30,8 +30,8 @@ public Deadline(String description, String deadline) {
* @param deadline Date of the deadline
* @param status Status of completion
*/
public Deadline(String description, String deadline, boolean status) {
super(description);
public Deadline(String description, String deadline, boolean status, Priority priority) {
super(description, priority);
this.deadlineString = makeDeadline(deadline);
this.deadlineDate = storeAsDateTime(deadlineString);
this.isDone = status;
Expand Down Expand Up @@ -71,6 +71,7 @@ public String makeDeadline(String deadline) {
public String toFileFormat() {
StringBuilder fileFormat = new StringBuilder();

fileFormat.append(taskPriority.toString() + "~");
fileFormat.append("D~");

if (this.isDone) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public Duke(String filePath) {
storage = new Storage(filePath);
try {
tasks = new TaskList(storage.load());
tasks.updateQueue();
} catch (DukeException e) {
ui.showLoadingError();
tasks = new TaskList();
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class Event extends Task {
* @param description Description of the event.
* @param eventDate Date of the event.
*/
public Event(String description, String eventDate) {
super(description);
public Event(String description, String eventDate, Priority priority) {
super(description, priority);
this.eventDateString = makeEventDate(eventDate);
this.eventDate = storeAsDateTime(eventDateString);
}
Expand All @@ -29,8 +29,8 @@ public Event(String description, String eventDate) {
* @param deadline Date of the deadline
* @param status Status of completion
*/
public Event(String description, String eventDate, boolean status) {
super(description);
public Event(String description, String eventDate, boolean status, Priority priority) {
super(description, priority);
this.eventDateString = makeEventDate(eventDate);
this.eventDate = storeAsDateTime(eventDateString);
this.isDone = status;
Expand Down Expand Up @@ -71,6 +71,7 @@ public String makeEventDate(String eventDate) {
public String toFileFormat() {
StringBuilder fileFormat = new StringBuilder();

fileFormat.append(taskPriority.toString() + "~");
fileFormat.append("E~");

if (this.isDone) {
Expand Down
62 changes: 46 additions & 16 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Arrays;

/**
* Parser is a class that aids in parsing through the user input
* and understanding the commands and executing them accordingly.
Expand All @@ -6,6 +8,9 @@
*/
public class Parser {
private Ui ui;
private String taskType;
private String taskDesc;
Priority taskPriority = Priority.LOW;

/**
* Constructs a Parser object.
Expand All @@ -30,28 +35,27 @@ public Parser(Ui ui) {
*/
public String parseCommand(String input, TaskList taskList) throws DukeException {
assert input != null;

String[] inputArr = input.split(" ");

//Extracting task type and description from input
String taskType = inputArr[0];
String taskDesc = getDesc(inputArr);

if (!correctInput(taskType)) {
throw new IncorrectInputException(":( OOPS!!! I'm sorry, but I don't know what that means :-(");
}
preProcessInput(input);

if (taskType.equals("bye")) {

return ui.showGoodbye();

} else if (taskType.equals("list")) {

return ui.printTaskList(taskList);
return ui.showTaskList(taskList);

} else if (taskType.equals("priority")){

String output = ui.showPriorityTaskList(
taskList.getPriorityTaskList());
taskList.updateQueue();
return output;

} else if (taskDesc.isEmpty()) {

throw new NoDescriptionException(":( OOPS!!! The description of " + inputArr[0] + " cannot be empty.");
throw new NoDescriptionException(
":( OOPS!!! The description of " + taskType + " cannot be empty.");

} else if (taskType.equals("done")) {

Expand Down Expand Up @@ -83,6 +87,31 @@ public String parseCommand(String input, TaskList taskList) throws DukeException
}
}

private void preProcessInput(String input) throws DukeException {
String[] inputArr = input.split(" ");

if (inputArr[0].equals("high")) {
taskPriority = Priority.HIGH;
inputArr = Arrays.copyOfRange(inputArr,1, inputArr.length);
} else if (inputArr[0].equals("medium")) {
taskPriority = Priority.MEDIUM;
inputArr = Arrays.copyOfRange(inputArr,1, inputArr.length);
} else if (inputArr[0].equals("low")) {
inputArr = Arrays.copyOfRange(inputArr,1, inputArr.length);
} else{

}

//Extracting task type and description from input
taskType = inputArr[0];
taskDesc = getDesc(inputArr);

if (!correctInput(taskType)) {
throw new IncorrectInputException(
":( OOPS!!! I'm sorry, but I don't know what that means :-(");
}
}

private String doneCommand(String taskDesc, TaskList taskList) {
int taskNum = Integer.parseInt(taskDesc);
Task task = taskList.getTask(taskNum);
Expand All @@ -104,7 +133,7 @@ private String deleteCommand(String taskDesc, TaskList taskList) {
}

private String todoCommand(String taskDesc, TaskList taskList) {
Task newTodo = new Todo(taskDesc);
Task newTodo = new Todo(taskDesc, taskPriority);
taskList.addTask(newTodo);

return ui.showTaskAdded(newTodo, taskList);
Expand All @@ -113,7 +142,7 @@ private String todoCommand(String taskDesc, TaskList taskList) {
private String deadlineCommand(String taskDesc, TaskList taskList) {
String[] taskDescArr = taskDesc.split(" /");

Task newDeadline = new Deadline(taskDescArr[0], taskDescArr[1]);
Task newDeadline = new Deadline(taskDescArr[0], taskDescArr[1], taskPriority);

taskList.addTask(newDeadline);

Expand All @@ -123,7 +152,7 @@ private String deadlineCommand(String taskDesc, TaskList taskList) {
private String eventCommand(String taskDesc, TaskList taskList) {
String[] taskDescArr = taskDesc.split(" /");

Task newEvent = new Event(taskDescArr[0], taskDescArr[1]);
Task newEvent = new Event(taskDescArr[0], taskDescArr[1], taskPriority);

taskList.addTask(newEvent);

Expand Down Expand Up @@ -159,6 +188,7 @@ public static boolean correctInput(String input) {
input.equals("done") ||
input.equals("bye") ||
input.equals("delete") ||
input.equals("find");
input.equals("find") ||
input.equals("priority");
}
}
3 changes: 3 additions & 0 deletions src/main/java/Priority.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum Priority {
HIGH, MEDIUM, LOW;
}
43 changes: 32 additions & 11 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public Storage(String filePath) {
*/
public LinkedList<Task> load() throws DukeException {
try {
System.out.println("loaded");
if (!taskListFile.exists()) {
throw new NoExistingListException("No saved List found.");
}
Expand All @@ -52,17 +53,24 @@ public LinkedList<Task> load() throws DukeException {
String nextTask = sc.nextLine();

String[] nextTaskArr = nextTask.split("~");
boolean status = nextTaskArr[1].equals("1");

if (nextTaskArr[0].equals("T")) {
taskList.add(
new Todo(nextTaskArr[2], status));
} else if (nextTaskArr[0].equals("D")) {
taskList.add(
new Deadline(nextTaskArr[2], nextTaskArr[3], status));
} else if (nextTaskArr[0].equals("E")) {
taskList.add(
new Event(nextTaskArr[2], nextTaskArr[3], status));
boolean status = nextTaskArr[2].equals("1");
Priority taskPriority = getPriority(nextTaskArr[0]);

switch (nextTaskArr[1]) {
case "T":
taskList.add(
new Todo(nextTaskArr[3], status, taskPriority));
break;
case "D":
taskList.add(
new Deadline(nextTaskArr[3], nextTaskArr[4], status, taskPriority));
break;
case "E":
taskList.add(
new Event(nextTaskArr[3], nextTaskArr[4], status, taskPriority));
break;
default:
assert false;
}
}

Expand All @@ -73,6 +81,19 @@ public LinkedList<Task> load() throws DukeException {
}
}

private Priority getPriority(String priorityString) {
if (priorityString.equals("HIGH")) {
return Priority.HIGH;
} else if (priorityString.equals("MEDIUM")) {
return Priority.MEDIUM;
} else if (priorityString.equals("LOW")) {
return Priority.LOW;
} else {
assert false;
return Priority.MEDIUM;
}
}

/**
* Updates the file that saves the tasks based on the
* LinkedList of tasks stored in the TaskList file.
Expand Down
34 changes: 27 additions & 7 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.time.LocalDateTime;
import java.util.LinkedList;

/**
* Task is an object simulating a task, eg events.
Expand All @@ -10,19 +9,20 @@
* tasks such as doing the task and storing the deadlines
* of the task as a Java date.
*/
public class Task {
public class Task implements Comparable<Task>{
protected String description;
protected boolean isDone;
protected static LinkedList<Task> taskList = new LinkedList<>();
protected Priority taskPriority;

/**
* Constructs a Task object.
* A task generally constructed with a description what to do.
* @param description Description of what the task is about.
*/
public Task(String description) {
public Task(String description, Priority taskPriority) {
this.description = description;
this.isDone = false;
this.taskPriority = taskPriority;
}

/**
Expand Down Expand Up @@ -97,15 +97,17 @@ protected LocalDateTime storeAsDateTime(String date) {

sb.append(hour + ":" + minutes + ":00");

LocalDateTime dateTime = LocalDateTime.parse(sb.toString());

return dateTime;
return LocalDateTime.parse(sb.toString());
}

public String getDesc() {
return this.description;
}

public Priority getPriority() {
return taskPriority;
}

/**
* Filler method to imitate an interface.
* @return String empty string
Expand All @@ -120,4 +122,22 @@ public String toString() {
return task;
}

@Override
public int compareTo(Task task) {
if (this.taskPriority == task.taskPriority) {
return 0;
} else if (this.taskPriority == Priority.HIGH) {
return -1;
} else if (this.taskPriority == Priority.LOW) {
return 1;
} else if (task.taskPriority == Priority.HIGH) {
return 1;
} else if (task.taskPriority == Priority.LOW) {
return -1;
} else {
assert false;
return 0;
}
}

}
Loading