Skip to content

Commit

Permalink
B-Snooze
Browse files Browse the repository at this point in the history
  • Loading branch information
tzw0 committed Sep 14, 2019
1 parent 734d678 commit 74c7364
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/main/java/command/RescheduleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package command;

import process.DukeException;
import process.Storage;
import process.Ui;
import task.TaskList;

public class RescheduleCommand extends Command{
private int index;
private String datetime;
public RescheduleCommand(int index_, String datetime_) {
index = index_;
datetime = datetime_;
}
public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {
if (this.index >= tasks.size()) throw new DukeException("index error");
tasks.rescheduleTask(index, datetime);
storage.save(tasks);
return ui.showTaskRescheduled(tasks.get(index).toString());
}
}
12 changes: 11 additions & 1 deletion src/main/java/process/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Parser {
* @throws DukeException if Duke cannot make sense of the input
*/
public static Command parse(String input) throws DukeException { //input validation
ArrayList<String> command_list = new ArrayList<String>(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event"));
ArrayList<String> command_list = new ArrayList<String>(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule"));
String operation;
String date;
int index =-1;
Expand Down Expand Up @@ -66,6 +66,16 @@ public static Command parse(String input) throws DukeException { //input validat
return new ExitCommand();
} else if (operation.equals("list")) {
return new ListCommand();
} else if (operation.equals("reschedule")) {
try {
index = Integer.parseInt(operation_list[1]) - 1;
} catch (Exception e) {
throw new DukeException("index error");
}
int to_index = input.indexOf(" /to ");
date = input.substring(to_index + 5).trim();
if (to_index < 0 || date.isBlank()) throw new DukeException("datetime");
return new RescheduleCommand(index, date);
}
}
throw new DukeException("unknown");
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/process/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public String showError(String error_msg) {
out_ = "You have no tasks in your list";
} else if (error_msg.equals("empty task")) {
out_ ="OOPS!!! The description of a task cannot be empty.";
} else if (error_msg.equals("reschedule")) {
out_ = "OOPS!!! A Todo task cannot be rescheduled";
}
else out_ = ("OOPS!!! " + error_msg);
System.out.println(out_);
Expand All @@ -94,7 +96,7 @@ public String showLoadingError() {
}
/**
* Shows and
* @return the given taska as done
* @return the given task as done
* @param task to be displayed as done
*/
public String showTaskDone(String task) {
Expand All @@ -103,6 +105,17 @@ public String showTaskDone(String task) {
System.out.println(out_);
return out_;
}
/**
* Shows and
* @return the given task as rescheduled
* @param task to be displayed as rescheduled
*/
public String showTaskRescheduled(String task) {
// String out_ = "Nice! I've marked this task as done:\n" + "[✓] " + task;
String out_ = "Nice! I've rescheduled this task:\n" + task;
System.out.println(out_);
return out_;
}
/**
* Shows and
* @return the given task as deleted
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Event(String description, String at, boolean b) throws DukeException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy hhmm");
Date date = sdf.parse(at);
this.at = date;
super.tt = "D";
super.tt = "E";
super.extra = at;
} catch (ParseException e){
throw new DukeException("datetime");
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ public void deleteTask(int index) {
public void doneTask(int index) {
this.get(index).done(true);
}

public void rescheduleTask(int index, String datetime) throws DukeException{
String description = this.get(index).description;
String tasktype = this.get(index).tt;
if (tasktype.equals("D")) {
this.add(index, new Deadline(description, datetime, false));
} else if (tasktype.equals("E")) {
this.add(index, new Event(description, datetime, false));
} else throw new DukeException("reschedule");
this.remove(index + 1);
}
/**
* Searches for a task with the given keyword
* @param keyword to be found
Expand Down

0 comments on commit 74c7364

Please sign in to comment.