Skip to content

Commit

Permalink
Merge branch 'branch-B-Snooze'
Browse files Browse the repository at this point in the history
  • Loading branch information
quzhetao01 committed Sep 12, 2023
2 parents db7ef35 + bd085d7 commit 88274cb
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/main/java/duke/command/RescheduleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package duke.command;

import duke.exception.DukeException;
import duke.list.TaskList;
import duke.storage.Storage;
import duke.task.Task;
import duke.ui.Ui;

public class RescheduleCommand extends Command {

public static final String COMMAND_WORD = "reschedule";

private int index;
private String deadline;

public RescheduleCommand(int index, String deadline) {
this.index = index;
this.deadline = deadline;
}

/**
* @inheritDoc
*/
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {
Task task = taskList.rescheduleTask(index, deadline);
storage.write(taskList);
return ui.showRescheduleMessage(task);
}
}
9 changes: 9 additions & 0 deletions src/main/java/duke/list/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public Task deleteTask(int index) throws DukeException {
this.taskList.remove(index - 1);
return task;
}

public Task rescheduleTask(int index, String deadline) throws DukeException {
if (index < 1 || index > this.taskList.size()) {
throw new DukeException("Index does not exist");
}
Task task = this.taskList.get(index - 1);
task.rescheduleTask(deadline);
return task;
}
public void add(Task task) {
this.taskList.add(task);
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import duke.command.DeleteCommand;
import duke.command.ListCommand;
import duke.command.ExitCommand;
import duke.command.RescheduleCommand;
import duke.command.WrongCommand;
import duke.command.FindCommand;
import duke.exception.DukeException;
Expand Down Expand Up @@ -47,6 +48,8 @@ public static Command parse(String input) throws DukeException {
return createDeleteCommand(input);
case FindCommand.COMMAND_WORD:
return createFindCommand(input);
case RescheduleCommand.COMMAND_WORD:
return createRescheduleCommand(input);
case ListCommand.COMMAND_WORD:
return new ListCommand();
case ExitCommand.COMMAND_WORD:
Expand All @@ -56,6 +59,21 @@ public static Command parse(String input) throws DukeException {
}
}

private static RescheduleCommand createRescheduleCommand(String input) throws DukeException {
String[] rescheduleData = input.split(" ", 3);
if (rescheduleData.length < 3) {
throw new DukeException("Provide the command as such:\nreschedule [index] [deadline]");
}
try {
String newDeadline = LocalDate.parse(rescheduleData[2]).format(DateTimeFormatter.ofPattern("MMM d yyyy"));
return new RescheduleCommand(Integer.valueOf(rescheduleData[1]), newDeadline);
} catch (NumberFormatException e) {
throw new DukeException("Please specify the index of the task (Numbers only)");
} catch (DateTimeException e) {
throw new DukeException("Please represent time in a proper time format of yyyy-mm-dd");
}
}

private static FindCommand createFindCommand(String input) throws DukeException {
String[] searchData = input.split(" ", 2);
if (searchData.length < 2) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public void writeToFile(FileWriter fw) throws IOException {
String storedRow = "D|" + (this.isDone ? "1|" : "0|") + this.getTask() + "|" + this.due;
fw.write(storedRow);
}

@Override
public void rescheduleTask(String newDeadline) {
this.due = newDeadline;
}

}
5 changes: 5 additions & 0 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public void writeToFile(FileWriter fw) throws IOException {
fw.write(storedRow);
}

@Override
public void rescheduleTask(String newDeadline) {
this.to = newDeadline;
}

}
5 changes: 5 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.task;

import duke.exception.DukeException;

import java.io.FileWriter;
import java.io.IOException;

Expand Down Expand Up @@ -75,11 +77,14 @@ public boolean match(String search) {
return this.task.contains(search);
}


/**
* Writes the task data to a FileWriter.
*
* @param fw The FileWriter to write task data to.
* @throws IOException If there is an issue writing the data.
*/
public abstract void writeToFile(FileWriter fw) throws IOException;

public abstract void rescheduleTask(String newDeadline) throws DukeException;
}
7 changes: 7 additions & 0 deletions src/main/java/duke/task/ToDo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.task;

import duke.exception.DukeException;

import java.io.FileWriter;
import java.io.IOException;

Expand Down Expand Up @@ -44,4 +46,9 @@ public void writeToFile(FileWriter fw) throws IOException {
String storedRow = "T|" + (this.isDone ? "1|" : "0|") + this.getTask();
fw.write(storedRow);
}

@Override
public void rescheduleTask(String newDeadline) throws DukeException {
throw new DukeException("Chosen task has no time restriction");
}
}
7 changes: 6 additions & 1 deletion src/main/java/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public String showItems(TaskList taskList) {
* @return Message containing the details on the deletion of the task
*/
public String showDeleteMessage(Task task, TaskList taskList) {
return "Noted. I've removed this duke.task:\n"
return "Noted. I've removed this task:\n"
+ "\t" + task + "\n"
+ "Now you have " + taskList.size() + " tasks in the list";
}
Expand All @@ -160,6 +160,11 @@ public String showMatchingTasks(ArrayList<Task> tasks) {
}
}

public String showRescheduleMessage(Task task) {
return "Noted. I've rescheduled the deadline for this task as shown:\n"
+ "\t" + task;
}

/**
* Returns a goodbye message confirming the termination of the program.
*
Expand Down

0 comments on commit 88274cb

Please sign in to comment.