Skip to content

Commit

Permalink
Merge pull request #16 from lijiayu980606/master
Browse files Browse the repository at this point in the history
Completed B-Snooze: snooze/postpone/reschedule a task
  • Loading branch information
lijiayu980606 authored Sep 19, 2019
2 parents a4fa86c + 02e18c6 commit e01a30d
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 4 deletions.
Binary file modified data/duke.txt
Binary file not shown.
112 changes: 110 additions & 2 deletions src/main/java/command/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
Expand Down Expand Up @@ -61,6 +62,15 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Storage stor
} else if (isWithinPeriodTask(input)) {
processWithin(input, tasklist, ui);
storage.save(tasklist.returnArrayList());
}else if (isSnooze(input)) {
processSnooze(input, tasklist, ui);
storage.save(tasklist.returnArrayList());
}else if (isPostpone(input)) {
processPostpone(input, tasklist, ui);
storage.save(tasklist.returnArrayList());
}else if (isReschedule(input)) {
processReschedule(input, tasklist, ui);
storage.save(tasklist.returnArrayList());
} else {
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
Expand All @@ -70,8 +80,6 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Storage stor
return false;
}



/**
* Processes the find command and outputs a list of tasks containing the word.
* @param input Input from the user.
Expand Down Expand Up @@ -222,6 +230,94 @@ private static void processWithin(String input, TaskList tasklist, Ui ui) {
}
}

/**
* Process the snooze command and automatically postpone the selected deadline task by 1 hour.
* @param input Input from the user.
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void processSnooze(String input, TaskList tasklist, Ui ui) {
try {
String[] arr = input.split(" ", 2);
int nsnooze = Integer.parseInt(arr[1]) - 1;
if(tasklist.get(nsnooze).getType().equals("D")){
String taskTime = tasklist.get(nsnooze).getBy();
Date formattedtime = dataformat.parse(taskTime);
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime(formattedtime);
calendar.add(Calendar.HOUR_OF_DAY,1);
Date newDate = calendar.getTime();
tasklist.get(nsnooze).setBy(dataformat.format(newDate));
ui.printSnoozeMessage(tasklist.get(nsnooze));
} else {
ui.exceptionMessage(" ☹ OOPS!!! Please select a deadline type task to snooze.");
}

} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" ☹ OOPS!!! Please input the list number to snooze.");
}catch (ParseException e) {
ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong.");
}
}
/**
* Process the postpone command and postpone the selected deadline task by required number of hours.
* @param input Input from the user.
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void processPostpone(String input, TaskList tasklist, Ui ui) {
try {
String[] splitspace = input.split(" ", 2);
String[] splittime = splitspace[1].split(" ", 2);
int npostpone = Integer.parseInt(splittime[0]) - 1;
int delaytime = Integer.parseInt(splittime[1]);
if(tasklist.get(npostpone).getType().equals("D")){
String taskTime = tasklist.get(npostpone).getBy();
Date formattedtime = dataformat.parse(taskTime);
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime(formattedtime);
calendar.add(Calendar.HOUR_OF_DAY,delaytime);
Date newDate = calendar.getTime();
tasklist.get(npostpone).setBy(dataformat.format(newDate));
ui.printPostponeMessage(tasklist.get(npostpone));
} else {
ui.exceptionMessage(" ☹ OOPS!!! Please select a deadline type task to postpone.");
}

} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" ☹ OOPS!!! Please input the list number to postpone. Format:'postpone <index> <no.of hours to postpone>'");
}catch (ParseException e) {
ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong. Format:'postpone <index> <no.of hours to postpone>");
}
}

private static void processReschedule(String input, TaskList tasklist, Ui ui) {
try {
String[] splitspace = input.split(" ", 2);
String[] splittime = splitspace[1].split(" ", 2);
int nreschedule = Integer.parseInt(splittime[0]) - 1;
String delay = splittime[1];
if(tasklist.get(nreschedule).getType().equals("D")){
Date formattedtime = dataformat.parse(delay);
String newschedule = dataformat.format(formattedtime);
tasklist.get(nreschedule).setBy(newschedule);
ui.printRescheduleMessage(tasklist.get(nreschedule));
} else if(tasklist.get(nreschedule).getType().equals("E")){
Date formattedtime = dataformat.parse(delay);
String newschedule = dataformat.format(formattedtime);
tasklist.get(nreschedule).setAt(newschedule);
ui.printRescheduleMessage(tasklist.get(nreschedule));
} else {
ui.exceptionMessage(" ☹ OOPS!!! Please select a deadline or event type task to reschedule.");
}

} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" ☹ OOPS!!! Please input the list number to reschedule. Format:'postpone <index> <the new scheduled time in dd/mm/yyyy HHmm>'");
}catch (ParseException e) {
ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong. Format:'postpone <index> <the new scheduled time in dd/mm/yyyy HHmm>");
}
}


private static boolean isBye(String input) {
return input.equals("bye");
Expand Down Expand Up @@ -258,4 +354,16 @@ private static boolean isFind(String input) {
private static boolean isWithinPeriodTask(String input) {
return input.startsWith("within");
}

private static boolean isSnooze(String input) {
return input.startsWith("snooze");
}

private static boolean isPostpone(String input) {
return input.startsWith("postpone");
}

private static boolean isReschedule(String input) {
return input.startsWith("reschedule");
}
}
3 changes: 2 additions & 1 deletion src/main/java/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

public class Deadline extends Task implements Serializable {
protected String by;
//protected String by;

/**
* Creates a Deadline instance and initialises the required attributes.
Expand All @@ -17,6 +17,7 @@ public class Deadline extends Task implements Serializable {
public Deadline(String description, String by) {
super(description);
this.by = by;
this.type = "D";
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Task containing information of a deadline.
*/
public class Event extends Task implements Serializable {
protected String at;
//protected String at;

/**
* Creates an Event instance and initialises the required attributes.
Expand All @@ -16,6 +16,7 @@ public class Event extends Task implements Serializable {
public Event(String description, String at) {
super(description);
this.at = at;
this.type = "E";
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
public class Task implements Serializable {
protected String description;
protected boolean isDone;
protected String type;
protected String by;
protected String at;

/**
* Creates a Task instance and initialises the required attributes.
Expand Down Expand Up @@ -41,11 +44,43 @@ public void setDone() {
this.isDone = true;
}

/**
* Sets a new value to the by attribute.
*/
public void setBy(String by) {
this.by = by;
}

/**
* Sets a new value to the at attribute.
*/
public void setAt(String at) {
this.at = at;
}

/**
* Gets the description of the Task.
* @return Task Description.
*/
public String getDescription() {
return this.description;
}

/**
* Gets the type of the Task.
* @return Task type.
*/
public String getType(){ return this.type; }

/**
* Gets the deadline of the Task.
* @return Task by.
*/
public String getBy(){ return this.by; }

/**
* Gets the period of the Task.
* @return Task at.
*/
public String getAt(){ return this.at; }
}
1 change: 1 addition & 0 deletions src/main/java/task/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Todo extends Task implements Serializable {
*/
public Todo(String description) {
super(description);
this.type = "T";
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,36 @@ public void printAddedMessage(Task task, TaskList tasklist) {
System.out.print(line);
}

/**
* Prints message to indicate a Task being snoozed.
* @param task Task to be snoozed.
*/
public void printSnoozeMessage(Task task) {
System.out.print(line + " Got it. I've snoozed this task: \n");
System.out.print(" " + task.giveTask() + "\n");
System.out.print(line);
}

/**
* Prints message to indicate a Task being postponed.
* @param task Task to be postponed.
*/
public void printPostponeMessage(Task task) {
System.out.print(line + " Got it. I've postponed this task: \n");
System.out.print(" " + task.giveTask() + "\n");
System.out.print(line);
}

/**
* Prints message to indicate a Task being rescheduled.
* @param task Task to be snoozed.
*/
public void printRescheduleMessage(Task task) {
System.out.print(line + " Got it. I've rescheduled this task: \n");
System.out.print(" " + task.giveTask() + "\n");
System.out.print(line);
}

/**
* Prints the message for the exception thrown.
* @param message Exception message.
Expand Down

0 comments on commit e01a30d

Please sign in to comment.