diff --git a/src/main/java/duke/command/Snooze.java b/src/main/java/duke/command/Snooze.java new file mode 100644 index 00000000..6c2d8fbc --- /dev/null +++ b/src/main/java/duke/command/Snooze.java @@ -0,0 +1,36 @@ +package duke.command; + +import duke.exception.DukeException; +import duke.parser.Parser; +import duke.storage.Storage; +import duke.task.Task; +import duke.task.TaskList; +import duke.ui.Ui; + +import java.util.Date; + +public class Snooze extends Command { + + private int taskNb; + private String until; + private Date date; + + public Snooze(String taskNb, String until){ + this.taskNb = Integer.parseInt(taskNb.trim())-1; + this.until=until; + this.date= Parser.getDate(until); + } + + @Override + public void execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + if (taskNb < taskList.size() && taskNb >= 0) { + if(taskList.getTask(taskNb).isDone()) + throw new DukeException("Seems like you've already finished that task, no need to snooze it now"); + taskList.changeTaskDate(taskNb, until); + ui.showChangedDate(Parser.getDateString(date, until),taskList.getTask(taskNb).toString() ); + storage.changeContent(taskNb); + } else + throw new DukeException("Enter a valid task number after snooze, between 1 and " + taskList.size()); + } +} + diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 6a676085..1ead23e1 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -8,6 +8,13 @@ import duke.task.Event; import duke.task.Todo; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.ZoneId; +import java.util.Date; + /** * Represents a parser used to parse the input String from the user into a Duke understandable {@link Command} */ @@ -64,6 +71,16 @@ public static Command parse(String fullCommand) throws DukeException { int taskNb = Integer.parseInt(splitted[1]); return new DeleteCommand(taskNb - 1); } else throw new DukeException("Need a task number after done!"); + + case "snooze": + if ((splitted.length == 1) || splitted[1].isBlank()) + throw new DukeException("The description of a snooze cannot be empty."); + String[] getUntil = splitted[1].split("/until ", 2); + if (getUntil.length < 2) + throw new DukeException("The description of a snooze must contain /until date!"); + return new Snooze(getUntil[0], getUntil[1]); + + case "period": if ((splitted.length == 1) || splitted[1].isEmpty()) throw new DukeException("The description of a period cannot be empty."); @@ -79,8 +96,59 @@ public static Command parse(String fullCommand) throws DukeException { catch(Exception e){ throw new DukeException("NO"); } + default: throw new DukeException("I'm sorry, but I don't know what that means :-("); } } + + /** + * Returns the suffix to be used after the days in the Date, usefull for printing the Date in the desired format + * @param n indication the Day of the month + * @return the suffix accordingly to the day of the month needed + */ + public static String getDaySuffix(int n) { + if (n >= 11 && n <= 13) { + return "th"; + } + switch (n % 10) { + case 1: + return "st"; + case 2: + return "nd"; + case 3: + return "rd"; + default: + return "th"; + } + } + + /** + * Returns a {@link Date} instance representation of a String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy" + * @param date String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy", used to extract a {@link Date} instance from + * @return the {@link Date} instance created from the argument string + */ + public static Date getDate(String date) { + DateFormat dateFormat = (date.length() > 11) ? new SimpleDateFormat("dd/MM/yyyy hhmm") : new SimpleDateFormat("dd/MM/yyyy"); + try { + dateFormat.parse(date); + return dateFormat.parse(date); + } catch (ParseException e) { + //case the date was not valid! + } + return null; + } + /** + * Returns the {@link Date } instance as a String to be printed in the file + * @param date deadline {@link Date} for finishing the task + * @return String the date for the deadline + */ + public static String getDateString(Date date, String dateString) { + if (date == null) + return dateString; + LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + String pattern = dateString.length() > 11 ? "d'" + Parser.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha " : "d'" + Parser.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy"; + SimpleDateFormat formatter = new SimpleDateFormat(pattern); + return formatter.format(date); + } } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 81343da2..df0a9376 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,5 +1,7 @@ package duke.task; +import duke.parser.Parser; + import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; @@ -15,43 +17,40 @@ public class Deadline extends Task { public Deadline(String description, String by) { super(description); this.by = by; - this.date = super.getDate(by); + this.date = Parser.getDate(by); + } + + @Override + public void setNewDate(String date) { + by=date; + this.date=Parser.getDate(by); + } + + @Override + public Date getCurrentDate() { + return date; } + @Override public String toString() { - return (super.getDate(by) == null) ? "[D]" + super.toString() + "(by: " + by + ")" : "[D]" + super.toString() + "(by: " + getDateString(date) + ")"; + return (Parser.getDate(by) == null) ? "[D]" + super.toString() + "(by: " + by + ")" : "[D]" + super.toString() + "(by: " + Parser.getDateString(date,by) + ")"; } /** * Returns the data by which the deadline must be met * @return date the final {@link Date } for finishing the Deadline */ - /* public Date getDate() { return date; } - */ - /** - * Returns the {@link Date } instance as a String to be printed in the file - * @param date deadline {@link Date} for finishing the task - * @return String the date for the deadline - */ - private String getDateString(Date date) { - if (date == null) - return by; - LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); - String pattern = by.length() > 11 ? "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha " : "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy"; - SimpleDateFormat formatter = new SimpleDateFormat(pattern); - return formatter.format(date); - } /** * Returns the String representation of the {@link Deadline} in format compatible to be easily read and written in a text file on the hard disc * @return String used to print the {@link Task } in the text file */ public String printInFile() { - return this.isDone() ? "D|1|" + this.getDescription() + "|" + this.getDateString(date) : "D|0|" + this.getDescription() + "|" + this.getDateString(date); + return this.isDone() ? "D|1|" + this.getDescription() + "|" + Parser.getDateString(date, by) : "D|0|" + this.getDescription() + "|" + Parser.getDateString(date, by); } } \ No newline at end of file diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 7ef127f6..4421d22b 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,5 +1,7 @@ package duke.task; +import duke.parser.Parser; + import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; @@ -15,39 +17,30 @@ public class Event extends Task { public Event(String description, String at) { super(description); this.at = at; - this.date = super.getDate(at); + this.date = Parser.getDate(at); + } + + @Override + public void setNewDate(String date) { + at=date; + this.date=Parser.getDate(at); + } + + @Override + public Date getCurrentDate() { + return date; } @Override public String toString() { - return "[E]" + super.toString() + "(at: " + getDateString(date) + ")"; + return "[E]" + super.toString() + "(at: " + Parser.getDateString(date, at) + ")"; } /** * Returns the String representation of the {@link Event} in format compatible to be easily read and written in a text file on the hard disc * @return String used to print the {@link Task } in the text file */ public String printInFile() { - return this.isDone() ? "E|1|" + getDescription() + "|" + this.getDateString(date) : "E|0|" + this.getDescription() + "|" + getDateString(date); + return this.isDone() ? "E|1|" + getDescription() + "|" + Parser.getDateString(date, at) : "E|0|" + this.getDescription() + "|" + Parser.getDateString(date, at); } - /** - * Returns the {@link Date } instance as a String to be printed in the file - * @param date the {@link Date} at which the event is happening - * @return String the date for the event - */ - private String getDateString(Date date) { - if (date == null) - return at; - LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); - //String pattern = at.length() > 11 ? "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha " : "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy"; - String pattern; - if(at.length() > 11){ - pattern = "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha "; - } - else{ - pattern = "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy"; - } - SimpleDateFormat formatter = new SimpleDateFormat(pattern); - return formatter.format(date); - } } \ No newline at end of file diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 7f7cef50..b09f5f96 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -13,12 +13,14 @@ public abstract class Task { private String description; private boolean isDone; - + private Date date; public Task(String description) { this.description = description; this.isDone = false; } + public abstract void setNewDate(String date); + public abstract Date getCurrentDate(); /** * Returns a String representation of the status icon, tick or cross, indicating whether the {@link Task} was done * @return a String representation of a tick or a cross, representing a done, respectively not yet finished {@link Task} @@ -57,41 +59,5 @@ public boolean isDone() { return isDone; } - /** - * Returns the suffix to be used after the days in the Date, usefull for printing the Date in the desired format - * @param n indication the Day of the month - * @return the suffix accordingly to the day of the month needed - */ - static String getDaySuffix(int n) { - if (n >= 11 && n <= 13) { - return "th"; - } - switch (n % 10) { - case 1: - return "st"; - case 2: - return "nd"; - case 3: - return "rd"; - default: - return "th"; - } - } - /** - * Returns a {@link Date} instance representation of a String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy" - * @param date String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy", used to extract a {@link Date} instance from - * @return the {@link Date} instance created from the argument string - */ - protected Date getDate(String date) { - DateFormat dateFormat = (date.length() > 11) ? new SimpleDateFormat("dd/MM/yyyy hhmm") : new SimpleDateFormat("dd/MM/yyyy"); - try { - //dateFormat.parse(date); - return dateFormat.parse(date); - } catch (ParseException e) { - //case the date was not valid! - return new Date(); - } - //return null; - } } \ No newline at end of file diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java index 3e497e73..24994ffc 100644 --- a/src/main/java/duke/task/TaskList.java +++ b/src/main/java/duke/task/TaskList.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.Date; import java.util.List; /** @@ -72,5 +73,8 @@ public Task removeTask(int taskNb) { return taskList.remove(taskNb); } + public void changeTaskDate(int taskNb, String date){ + taskList.get(taskNb).setNewDate(date); + } } diff --git a/src/main/java/duke/task/Todo.java b/src/main/java/duke/task/Todo.java index fd592dc8..8ffb1544 100644 --- a/src/main/java/duke/task/Todo.java +++ b/src/main/java/duke/task/Todo.java @@ -1,18 +1,35 @@ package duke.task; +import duke.parser.Parser; + +import java.util.Date; + /** * Represents a specific {@link Task} todo, not necessarily indicating a deadline or a specific date */ public class Todo extends Task { + private Date date; public Todo(String description) { super(description); + date=null; + } + + @Override + public void setNewDate(String date) { + this.date= Parser.getDate(date); + } + + @Override + public Date getCurrentDate() { + return date; } @Override public String toString() { return "[T]" + super.toString(); } + @Override public String printInFile() { return this.isDone() ? "T|1|" + this.getDescription() : "T|0|" + this.getDescription(); diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 26e5ab5a..a73870f7 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -55,7 +55,11 @@ public void showTask(String task) { public void showMarkDone(String doneTask) { System.out.println("\t Nice! I've marked this task as done:"); System.out.println("\t " + doneTask); + } + public void showChangedDate(String date, String changedTask){ + System.out.println("\t Nice! I've snoozed this task as until "+ date+":"); + System.out.println("\t " + changedTask); } public void showAddCommand(String command, int size) { diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 00000000..06a63803 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,9 @@ +/** + * + */ +/** + * @author Sara + * + */ +module duke { +} \ No newline at end of file diff --git a/src/test/data/tasks.txt b/src/test/data/tasks.txt new file mode 100644 index 00000000..e69de29b