diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3c6e7c5abe..5b7171ef15 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,5 @@ import java.io.*; +import java.text.ParseException; import command.Command; import javafx.application.Application; @@ -42,7 +43,7 @@ public class Duke extends Application { */ public Duke(String file) { String directory = System.getProperty("user.home"); - directory += "\\documents\\duke\\data"; + directory += "\\Documents\\CS2113T\\main\\data"; String savefile = file; String absolutePath = directory + File.separator + savefile; storage = new Storage(absolutePath); @@ -68,7 +69,7 @@ public void run() { Command c = Parser.parse(fullCommand); c.execute(tasks, ui, storage); isExit = c.isExit(); - } catch (DukeException e) { + } catch (DukeException | ParseException e) { ui.showError(e.getMessage()); } finally { @@ -198,7 +199,7 @@ String getResponse(String input) { Command c = Parser.parse(input); String toGui = c.execute(tasks, ui, storage); return toGui; - } catch (DukeException e) { + } catch (DukeException | ParseException e) { return ui.showError(e.getMessage()); } } diff --git a/src/main/java/command/Command.java b/src/main/java/command/Command.java index 7b9c7243eb..a1dd5eec33 100644 --- a/src/main/java/command/Command.java +++ b/src/main/java/command/Command.java @@ -2,6 +2,8 @@ import process.*; import task.TaskList; +import java.text.ParseException; + /** * Represents a command registered in Duke */ @@ -15,7 +17,7 @@ public abstract class Command { * @return the response as a string from the user interface * @throws DukeException if command cannot be executed */ - public abstract String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException; + public abstract String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, ParseException; /** * Checks if the programme should exit * @return True if the programme should exit diff --git a/src/main/java/command/RecurringCommand.java b/src/main/java/command/RecurringCommand.java new file mode 100644 index 0000000000..bd34b75872 --- /dev/null +++ b/src/main/java/command/RecurringCommand.java @@ -0,0 +1,56 @@ +package command; + +import process.DukeException; +import process.Storage; +import process.Ui; +import task.Deadline; +import task.Event; +import task.TaskList; + +import java.lang.reflect.Field; +import java.text.FieldPosition; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +public class RecurringCommand extends Command { + private String description; + private String tasktype; + private String datetime; + String type; + + public RecurringCommand(String tasktype, String description, String datetime, String type) { + this.description = description; + this.tasktype = tasktype; + this.datetime = datetime; + this.type = type; + } + + @Override + public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy hhmm"); + Calendar c = Calendar.getInstance(); + c.setTime((sdf.parse(datetime))); + int times = 0; int increment = 0; + if (type.equals("daily")) { + times = 7; increment = 1; + } else if (type.equals("weekly")) { + times = 4; increment = 7; + } + if (tasktype.equals("deadline")) tasks.add(new Deadline(description, datetime, false)); + else if (tasktype.equals("event")) tasks.add(new Event(description, datetime, false)); + for (int i = 1; i < times; i++) { + c.add(Calendar.DATE, increment); + StringBuffer stringBuffer = new StringBuffer(); + sdf.format(c.getTime(), stringBuffer, new FieldPosition(0)); + if (tasktype.equals("deadline")) { + tasks.add(new Deadline(description, stringBuffer.toString(), false)); + } else if (tasktype.equals("event")) { + tasks.add(new Event(description, stringBuffer.toString(), false)); + } + } + storage.save(tasks); + return ui.showTaskAdded(tasks.get(tasks.size()-1).toString(), tasks.size()); + } +} diff --git a/src/main/java/process/Parser.java b/src/main/java/process/Parser.java index b62b4c4a79..4d1e591473 100644 --- a/src/main/java/process/Parser.java +++ b/src/main/java/process/Parser.java @@ -19,7 +19,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 command_list = new ArrayList(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule","task")); + ArrayList command_list = new ArrayList(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule", "task", "daily", "weekly")); String operation; String date; int index =-1; @@ -87,6 +87,25 @@ public static Command parse(String input) throws DukeException { //input validat date = input.substring(to_index + 5).trim(); if (to_index < 0 || date.isBlank()) throw new DukeException("datetime"); return new RescheduleCommand(index, date); + } else if (operation.equals("daily") || operation.equals("weekly")) { + String s = input.substring(input.indexOf(" ") + 1); + if (s.startsWith("deadline")) { + int by_index = s.indexOf(" /by "); + if (by_index == -1) throw new DukeException("datetime"); + arg1 = s.substring(8, by_index).trim(); + if (arg1.isBlank()) throw new DukeException("arg1 error "+ operation); + date = s.substring(by_index + 5).trim(); + if (date.isBlank()) throw new DukeException("datetime"); + return new RecurringCommand("deadline", arg1, date, input.substring(0, input.indexOf(" "))); + } else if (s.startsWith("event")) { + int at_index = s.indexOf(" /at "); + if (at_index == -1) throw new DukeException("datetime"); + arg1 = s.substring(5, at_index).trim(); + if (arg1.isBlank()) throw new DukeException("arg1 error "+ operation); + date = s.substring(at_index + 5).trim(); + if (date.isBlank()) throw new DukeException("datetime"); + return new RecurringCommand("event", arg1, date, input.substring(0, input.indexOf(" "))); + } } } throw new DukeException("unknown");