diff --git a/data/tasks.txt b/data/tasks.txt new file mode 100644 index 00000000..ed30e113 --- /dev/null +++ b/data/tasks.txt @@ -0,0 +1,4 @@ +T|0|read book +D|0|return book |tmr +E|0|meeting |2019-09-09 +[P][✘] pain (from: TODO to: TODO diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 1dcd52f3..6a676085 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -4,6 +4,7 @@ import duke.command.*; import duke.exception.DukeException; import duke.task.Deadline; +import duke.task.DoWithinPeriodTasks; import duke.task.Event; import duke.task.Todo; @@ -37,18 +38,18 @@ public static Command parse(String fullCommand) throws DukeException { return new DoneCommand(taskNb - 1); } else throw new DukeException("Need a task number after done!"); case "todo": - if ((splitted.length == 1) || splitted[1].isBlank()) + if ((splitted.length == 1) || splitted[1].isEmpty()) throw new DukeException("The description of a todo cannot be empty."); return new AddCommand(new Todo(splitted[1])); case "deadline": - if ((splitted.length == 1) || splitted[1].isBlank()) + if ((splitted.length == 1) || splitted[1].isEmpty()) throw new DukeException("The description of a deadline cannot be empty."); String[] getBy = splitted[1].split("/by ", 2); if (getBy.length < 2) throw new DukeException("The description of a deadline must contain /by date!"); return new AddCommand(new Deadline(getBy[0], getBy[1])); case "event": - if ((splitted.length == 1) || splitted[1].isBlank()) + if ((splitted.length == 1) || splitted[1].isEmpty()) throw new DukeException("The description of an event cannot be empty, and it must contain /at"); String[] getAt = splitted[1].split("/at ", 2); if (getAt.length < 2) @@ -63,6 +64,21 @@ 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 "period": + if ((splitted.length == 1) || splitted[1].isEmpty()) + throw new DukeException("The description of a period cannot be empty."); + String[] getPart = splitted[1].split("/from ", 2); + if (getPart.length < 2) + throw new DukeException("The description of a period must contain /from date!"); + String[] part = getPart[1].split("/to ", 2); + if (part.length < 2) + throw new DukeException("NO"); + try { + return new AddCommand(new DoWithinPeriodTasks(getPart[0], part[0], part[1])); + } + catch(Exception e){ + throw new DukeException("NO"); + } default: throw new DukeException("I'm sorry, but I don't know what that means :-("); } diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index eabbfe17..81343da2 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -15,21 +15,23 @@ public class Deadline extends Task { public Deadline(String description, String by) { super(description); this.by = by; - this.date = getDate(by); + this.date = super.getDate(by); } @Override public String toString() { - return (getDate(by) == null) ? "[D]" + super.toString() + "(by: " + by + ")" : "[D]" + super.toString() + "(by: " + getDateString(date) + ")"; + return (super.getDate(by) == null) ? "[D]" + super.toString() + "(by: " + by + ")" : "[D]" + super.toString() + "(by: " + getDateString(date) + ")"; } /** * 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 diff --git a/src/main/java/duke/task/DoWithinPeriodTasks.java b/src/main/java/duke/task/DoWithinPeriodTasks.java new file mode 100644 index 00000000..a7099212 --- /dev/null +++ b/src/main/java/duke/task/DoWithinPeriodTasks.java @@ -0,0 +1,25 @@ +package duke.task; + +import java.util.Date; + +public class DoWithinPeriodTasks extends Task{ + + private Date from; + private Date to; + + public DoWithinPeriodTasks(String d, String f, String t) { + //CONSTRUCTOR + super(d); + from = super.getDate(f); + to = super.getDate(t); + } + + public String printInFile(){ + return this.toString(); + } + + @Override + public String toString(){ + return "[P]" + super.toString() + "(from: " + "TODO" + " to: " + "TODO"; + } +} diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 1bd1db11..7ef127f6 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -15,7 +15,7 @@ public class Event extends Task { public Event(String description, String at) { super(description); this.at = at; - this.date = getDate(at); + this.date = super.getDate(at); } @Override @@ -39,7 +39,14 @@ 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 = 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); } diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 1c8c5e39..7f7cef50 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -83,14 +83,15 @@ static String getDaySuffix(int n) { * @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 */ - static Date getDate(String date) { + 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); + //dateFormat.parse(date); return dateFormat.parse(date); } catch (ParseException e) { //case the date was not valid! + return new Date(); } - return null; + //return null; } } \ No newline at end of file