Skip to content

Commit

Permalink
Fix input validation and checkstyle issues
Browse files Browse the repository at this point in the history
Valid input dates via regex. Restrict file loading to .txt files only. Add Javadoc for more classes and methods.
  • Loading branch information
koonweee committed Oct 17, 2020
1 parent d6b9584 commit a669452
Show file tree
Hide file tree
Showing 28 changed files with 281 additions and 83 deletions.
3 changes: 3 additions & 0 deletions data/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
D%d%false%d%flying%d%1829-12-23
T%d%false%d%test
E%d%false%d%test%d%2020-12-12
33 changes: 19 additions & 14 deletions src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.io.IOException;
import java.time.LocalDate;

/**
* Parses commands as well as provide differnet type of subclasses
Expand All @@ -8,9 +7,9 @@
abstract class Command {
protected boolean exit;

protected TaskList tasks;
protected Ui ui;
protected Storage storage;
protected final TaskList tasks;
protected final Ui ui;
protected final Storage storage;

Command(TaskList tasks, Ui ui, Storage storage) {
this.tasks = tasks;
Expand All @@ -25,7 +24,8 @@ abstract class Command {
* @return appropriate Command object
* @throws InvalidInputException if String command not available
*/
static Command parse(String command, TaskList tasks, Ui ui, Storage storage) throws InvalidInputException, EmptyTodoException {
static Command parse(String command, TaskList tasks, Ui ui, Storage storage)
throws InvalidInputException, EmptyTodoException, InvalidDeadlineException {
if (command.equals("bye")) {
return new ExitCommand(tasks, ui, storage);
} else if (command.equals("list")) {
Expand All @@ -43,15 +43,20 @@ static Command parse(String command, TaskList tasks, Ui ui, Storage storage) thr
return new LoadFileCommand(tasks, ui, storage);
} else {
//actual entry
String postFix = command.split(" ", 2)[1];
String preFix = command.split(" ", 2)[0];
if (preFix.equals("todo")) {
return new TodoCommand(postFix, tasks, ui, storage);
} else if (preFix.equals("deadline")) {
return new DeadlineCommand(postFix, tasks, ui, storage);
} else if (preFix.equals("event")) {
return new EventCommand(postFix, tasks, ui, storage);
} else {
try {
String postFix = command.split(" ", 2)[1];
String preFix = command.split(" ", 2)[0];
switch (preFix) {
case "todo":
return new TodoCommand(postFix, tasks, ui, storage);
case "deadline":
return new DeadlineCommand(postFix, tasks, ui, storage);
case "event":
return new EventCommand(postFix, tasks, ui, storage);
default:
throw new InvalidInputException();
}
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidInputException();
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@
* It is initialized via 'deadline (taskname) /by (yyyy-dd-mm)'
*/
class Deadline extends Task {
private LocalDate date;
private final LocalDate date;

/**
* Deadline constructor
* @param task String representation of task name
* @param deadline String representation of deadline
*/
Deadline(String task, String deadline) {
super(task);
System.out.println("here");
this.date = LocalDate.parse(deadline);
updateRep();
}

/**
* Returns string representation of deadline
* @return formatted string representation of deadline
*/
@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + date.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")";
}

/**
* Updates the representation of the deadline object to be saved to the storage file
*/
@Override
public void updateRep() {
super.updateRep();
this.saveRep = "D%d%" + this.done + "%d%" + this.task + "%d%" + this.date;
}
}
}
31 changes: 30 additions & 1 deletion src/main/java/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,46 @@ class DeadlineCommand extends Command {
private String task;
private String deadline;

DeadlineCommand(String toParse, TaskList tasks, Ui ui, Storage storage) {
/**
* Constructor for DeadlineCommand
* @param toParse Partial user input to be parsed
* @param tasks Existing list of tasks
* @param ui User interface to be updated
* @param storage Storage to be updated
* @throws InvalidDeadlineException Exception thrown when an empty or incorrectly formatted date is provided
*/
DeadlineCommand(String toParse, TaskList tasks, Ui ui, Storage storage) throws InvalidDeadlineException {
super(tasks, ui, storage);
String[] split = toParse.split(" /by ");
if (split.length < 2) {
throw new InvalidDeadlineException("no deadline");
}
this.task = split[0];
this.deadline = split[1];
if (!isValidDate(this.deadline)) {
throw new InvalidDeadlineException(this.deadline);
}
}

/**
* Executes the DeadlineCommand
* @return Returns String confirmation of added task
* @throws IOException Exception thrown when updating storage file
*/
@Override
public String execute() throws IOException {
Task task = tasks.addDeadline(this.task, this.deadline);
storage.saveFile(tasks);
return ui.printf("Got it. I've added this task:\n" + task + "\n" + tasks.taskCount());
}

/**
* Verifies date matches yyyy-mm-dd format
* @param date String of date to be checked
* @return True if date is valid
*/
public boolean isValidDate(String date) {
String validation = "^\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$";
return date.matches(validation);
}
}
14 changes: 13 additions & 1 deletion src/main/java/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@
* A delete command to delete a given task number
*/
class DeleteCommand extends Command {
private int deleteTask;
private final int deleteTask;

/**
* Constructor for DeleteCommand
* @param task Index of task to be deleted
* @param tasks Existing list of tasks
* @param ui User interface to be updated
* @param storage Storage to be updated
*/
DeleteCommand(int task, TaskList tasks, Ui ui, Storage storage) {
super(tasks, ui, storage);
this.deleteTask = task;
}

/**
* Executes the DeleteCommand
* @return Returns String confirmation of deleted task
* @throws IOException Exception thrown when updating storage file
*/
@Override
public String execute() throws IOException {
//retrieves the string representation of the task before it is deleted
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/DialogBox.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import java.io.IOException;
import java.util.Collections;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
Expand Down Expand Up @@ -41,4 +36,4 @@ public static DialogBox getJarvisDialog(String text, Image img) {
var db = new DialogBox(text, img);
return db;
}
}
}
14 changes: 13 additions & 1 deletion src/main/java/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@
* A done command to mark a given task number as done
*/
class DoneCommand extends Command {
private int doneTask;
private final int doneTask;

/**
* Constructor for DoneCommand
* @param task Index of task to be marked as done
* @param tasks Existing list of tasks
* @param ui User interface to be updated
* @param storage Storage to be updated
*/
DoneCommand(int task, TaskList tasks, Ui ui, Storage storage) {
super(tasks, ui, storage);
this.doneTask = task;
}

/**
* Executes the DoneCommand
* @return Returns String confirmation of completed task
* @throws IOException Exception thrown when updating storage file
*/
@Override
public String execute() throws IOException {
tasks.done(this.doneTask);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/DukeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ class DukeException extends Exception {
public DukeException(String errorMessage) {
super(errorMessage);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/EmptyTodoException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ class EmptyTodoException extends DukeException {
public EmptyTodoException() {
super("☹ OOPS!!! The description of a todo cannot be empty.");
}
}
}
16 changes: 14 additions & 2 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@
import java.time.format.DateTimeFormatter;

class Event extends Task {
private LocalDate date;
private final LocalDate date;

/**
* Event constructor
* @param task String representation of task name
* @param duration String representation of duration
*/
Event(String task, String duration) {
super(task);
this.date = LocalDate.parse(duration);
updateRep();
}

/**
* Returns string representation of event
* @return formatted string representation of event
*/
@Override
public String toString() {
return "[E]" + super.toString() + " (at: " + date.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")";
}

/**
* Updates the representation of the event object to be saved to the storage file
*/
@Override
public void updateRep() {
super.updateRep();
this.saveRep = "E%d%" + this.done + "%d%" + this.task + "%d%" + this.date;
}
}
}
36 changes: 33 additions & 3 deletions src/main/java/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,49 @@
* An Event command to add an Event Task to the TaskList
*/
class EventCommand extends Command {
private String task;
private String at;
EventCommand(String toParse, TaskList tasks, Ui ui, Storage storage) {
private final String task;
private final String at;

/**
* Constructor for EventCommand
* @param toParse Partial user input to be parsed
* @param tasks Existing list of tasks
* @param ui User interface to be updated
* @param storage Storage to be updated
* @throws InvalidDeadlineException Exception thrown when an empty or incorrectly formatted date is provided
*/
EventCommand(String toParse, TaskList tasks, Ui ui, Storage storage) throws InvalidDeadlineException {
super(tasks, ui, storage);
String[] split = toParse.split(" /at ");
if (split.length < 2) {
throw new InvalidDeadlineException("no deadline");
}
this.task = split[0];
this.at = split[1];
if (!isValidDate(this.at)) {
throw new InvalidDeadlineException(this.at);
}
}

/**
* Executes the EventCommand
* @return Returns String confirmation of added task
* @throws IOException Exception thrown when updating storage file
*/
@Override
public String execute() throws IOException {
Task task = tasks.addEvent(this.task, this.at);
storage.saveFile(tasks);
return ui.printf("Got it. I've added this task:\n" + task + "\n" + tasks.taskCount());
}

/**
* Verifies date matches yyyy-mm-dd format
* @param date String of date to be checked
* @return True if date is valid
*/
public boolean isValidDate(String date) {
String validation = "^\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$";
return date.matches(validation);
}
}
14 changes: 13 additions & 1 deletion src/main/java/ExceptionCommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import java.io.IOException;

public class ExceptionCommand extends Command {
Exception e;
final Exception e;

/**
* Constructor for ExceptionCommand
* @param e Exception thrown from previously executed command
* @param tasks Existing list of tasks
* @param ui User interface to be updated
* @param storage Storage to be updated
*/
public ExceptionCommand(Exception e, TaskList tasks, Ui ui, Storage storage) {
super(tasks, ui, storage);
this.e = e;
}

/**
* Executes the ExceptionCommand
* @return Returns String message of exception thrown
* @throws IOException Exception thrown when updating storage file
*/
@Override
public String execute() throws IOException {
return "Jarvis exception: " + this.e.getMessage();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@
* An exit command
*/
class ExitCommand extends Command {

/**
* Constructor for ExitCommand
* @param tasks Existing list of tasks
* @param ui User interface to be updated
* @param storage Storage to be updated
*/
ExitCommand(TaskList tasks, Ui ui, Storage storage) {
super(tasks, ui, storage);
this.exit = true;
}

/**
* Executes the ExitCommand
* @return Returns String goodbye message
*/
@Override
public String execute() {
return "Jarvis is exiting, goodbye!";
Expand Down
Loading

0 comments on commit a669452

Please sign in to comment.