Skip to content

Commit

Permalink
Merge pull request #36 from jing-xuan/Recurring
Browse files Browse the repository at this point in the history
[CS2113T-F14-2]-jing-xuan-B-RecurringTasks
  • Loading branch information
jing-xuan authored Sep 15, 2019
2 parents fa9cfcb + 3b6b4f0 commit afc1cf9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.io.*;
import java.text.ParseException;

import command.Command;
import javafx.application.Application;
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import process.*;
import task.TaskList;

import java.text.ParseException;

/**
* Represents a command registered in Duke
*/
Expand All @@ -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
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/command/RecurringCommand.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
21 changes: 20 additions & 1 deletion src/main/java/process/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> command_list = new ArrayList<String>(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule","task"));
ArrayList<String> command_list = new ArrayList<String>(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule", "task", "daily", "weekly"));
String operation;
String date;
int index =-1;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit afc1cf9

Please sign in to comment.