Skip to content

Commit

Permalink
Merge branch-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonfoocy committed Sep 17, 2020
2 parents 7b12b17 + b17e763 commit a264f12
Show file tree
Hide file tree
Showing 28 changed files with 390 additions and 45 deletions.
25 changes: 17 additions & 8 deletions src/main/java/sparrow/Sparrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Scanner;

import sparrow.commands.Command;
import sparrow.data.exceptions.IncorrectCommandException;
import sparrow.data.task.TaskList;
import sparrow.data.trivia.VocabList;
import sparrow.parser.Parser;
Expand Down Expand Up @@ -54,11 +55,15 @@ public void run() {
Parser parser = new Parser();
boolean isExit = false;
while (!isExit) {
String command = sc.nextLine();
Command c = parser.parseCommand(command);
String result = c.execute(tasks, vocabList, ui, storage);
ui.replyToUser(result);
isExit = c.getIsExit();
try {
String command = sc.nextLine();
Command c = parser.parseCommand(command);
String result = c.execute(tasks, vocabList, ui, storage);
ui.replyToUser(result);
isExit = c.getIsExit();
} catch (IncorrectCommandException ice) {
ui.replyToUser(ice.getMessage());
}
}
}

Expand All @@ -68,9 +73,13 @@ public void run() {
* @return Reply to user.
*/
public String getResponse(String userInput) {
Parser parser = new Parser();
Command c = parser.parseCommand(userInput);
return c.execute(tasks, vocabList, ui, storage);
try {
Parser parser = new Parser();
Command c = parser.parseCommand(userInput);
return c.execute(tasks, vocabList, ui, storage);
} catch (IncorrectCommandException ice) {
return ice.getMessage();
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/sparrow/commands/AddTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ public String execute(TaskList tasks, VocabList vocabList, Ui ui, Storage storag
return fee.getMessage();
}
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof AddTaskCommand
&& toAdd.equals(((AddTaskCommand) other).toAdd));
}
}
4 changes: 3 additions & 1 deletion src/main/java/sparrow/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sparrow.commands;

import sparrow.data.exceptions.FileErrorException;
import sparrow.data.exceptions.SparrowException;
import sparrow.data.task.Task;
import sparrow.data.task.TaskList;
import sparrow.data.trivia.VocabList;
Expand All @@ -25,7 +26,8 @@ public String execute(TaskList tasks, VocabList vocabList, Ui ui, Storage storag
storage.saveTaskListToFile(tasks);
return String.format(MESSAGE_DELETE_TASK_SUCCESS, deletedTask);
} catch (IndexOutOfBoundsException e) {
return "INDEX OUT OF BOUNDS";
return String.format(SparrowException.STANDARD_EXCEPTION_MESSAGE,
"No task found at index specified.");
} catch (FileErrorException fee) {
return fee.getMessage();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sparrow/commands/FilterCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public FilterCommand(LocalDate date) {

@Override
public String execute(TaskList tasks, VocabList vocabList, Ui ui, Storage storage) {
ArrayList<Task> matchingTasks = tasks.filterList(date);
ArrayList<Task> matchingTasks = tasks.filterTasks(date);
if (matchingTasks.size() == 0) {
return MESSAGE_FIND_FAILURE;
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/sparrow/commands/IncorrectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ public String execute(TaskList tasks, VocabList vocabList, Ui ui, Storage storag
return feedbackToUser;
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof IncorrectCommand
&& feedbackToUser.equals(((IncorrectCommand) other).feedbackToUser));
}
}
4 changes: 3 additions & 1 deletion src/main/java/sparrow/commands/ListCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sparrow.commands;

import sparrow.data.exceptions.IncorrectCommandException;
import sparrow.data.task.TaskList;
import sparrow.data.trivia.VocabList;
import sparrow.storage.Storage;
Expand All @@ -10,6 +11,7 @@ public class ListCommand extends Command {
public static final String COMMAND_WORD = "list";
public static final String TASK_MESSAGE_SUCCESS = "Here are the tasks in your list: \n%s";
public static final String VOCAB_MESSAGE_SUCCESS = "Here are the words in your list: \n%s";
public static final String HELP_MESSAGE = "Ye ought to specify either 'tasks' or 'vocab' after the 'list' command.";

private final String data;

Expand All @@ -26,7 +28,7 @@ public String execute(TaskList tasks, VocabList vocabList, Ui ui, Storage storag
String vocabsAsString = ui.vocabListToString(vocabList.getVocabList());
return String.format(VOCAB_MESSAGE_SUCCESS, vocabsAsString);
} else {
return "No matching data found";
return HELP_MESSAGE;
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/sparrow/commands/VocabCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ public VocabCommand(String newWord, String definition) {
@Override
public String execute(TaskList tasks, VocabList vocabList, Ui ui, Storage storage) {
try {
vocabList.getVocabList().add(newVocab);
vocabList.addVocab(newVocab);
storage.saveTaskListToFile(tasks);
storage.saveVocabListToFile(vocabList);
return newVocab.getWord() + ADD_VOCAB_SUCCESS;
} catch (FileErrorException fee) {
return fee.getMessage();
}
}

@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof VocabCommand
&& newVocab.equals((Vocabulary) other));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sparrow.data.exceptions;

public class IncorrectCommandException extends SparrowException {

public IncorrectCommandException(String message) {
super(message);
}
}
6 changes: 5 additions & 1 deletion src/main/java/sparrow/data/exceptions/SparrowException.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
public class SparrowException extends Exception {

/** Emoji to be pre-pended to an exception message.*/
public static final String STANDARD_EXCEPTION_MESSAGE = "ARR!\uD83C\uDFF4\u200D\u2620\uFE0F️ ";
public static final String STANDARD_EXCEPTION_MESSAGE = "ARR!\uD83C\uDFF4\u200D\u2620\uFE0F%s";

public SparrowException(String message, Throwable cause) {
super(message, cause);
}

public SparrowException(String message) {
super(message);
}
}
8 changes: 8 additions & 0 deletions src/main/java/sparrow/data/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public String toString() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
return String.format("[D]%s (by: %s)", super.toString(), this.dueDate.format(formatter));
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof Deadline
&& description.equals(((Deadline) other).description)
&& dueDate.equals(((Deadline) other).dueDate));
}
}
8 changes: 8 additions & 0 deletions src/main/java/sparrow/data/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public String toString() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
return String.format("[E]%s (at: %s)", super.toString(), this.date.format(formatter));
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof Event
&& description.equals(((Event) other).description)
&& date.equals(((Event) other).date));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sparrow/data/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ public void markAsDone() {
public String toString() {
return String.format("[%s] %s", getStatusIcon(), this.description);
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof Task
&& description.equals(((Task) other).description));
}
}
7 changes: 3 additions & 4 deletions src/main/java/sparrow/data/task/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sparrow.data.task;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -33,13 +32,13 @@ public List<Task> getTasks() {
public void addTask(Task toAdd) {
tasks.add(toAdd);
}

/**
* Returns a task list matching the inputted date filter.
* @param dateFilter User's command and date to filter by.
* @param dateFilter User's date to filter by.
* @return Filtered list based on input.
* @throws DateTimeParseException if date passed cannot be converted to a LocalDate object.
*/
public ArrayList<Task> filterList(LocalDate dateFilter) throws DateTimeParseException {
public ArrayList<Task> filterTasks(LocalDate dateFilter) {
ArrayList<Task> filteredList = new ArrayList<>();

for (Task task : tasks) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/sparrow/data/trivia/VocabList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public VocabList(List<Vocabulary> vocabList) {
public List<Vocabulary> getVocabList() {
return vocabList;
}

public void addVocab(Vocabulary toAdd) {
this.vocabList.add(toAdd);
}
}
8 changes: 8 additions & 0 deletions src/main/java/sparrow/data/trivia/Vocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public String define() {
public String toString() {
return String.format("%s: %s", word, define());
}

@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof Vocabulary
&& word.equals(((Vocabulary) other).word)
&& define().equals(((Vocabulary) other).define()));
}
}
18 changes: 3 additions & 15 deletions src/main/java/sparrow/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sparrow.commands.IncorrectCommand;
import sparrow.commands.ListCommand;
import sparrow.commands.VocabCommand;
import sparrow.data.exceptions.IncorrectCommandException;
import sparrow.data.task.Deadline;
import sparrow.data.task.Event;
import sparrow.data.task.Todo;
Expand Down Expand Up @@ -55,15 +56,13 @@ public class Parser {
* @param userInput Text entered by user.
* @return Command to be executed.
*/
public Command parseCommand(String userInput) {
assert !userInput.isBlank() : "No input entered"; // do I need this assertion?
public Command parseCommand(String userInput) throws IncorrectCommandException {
//@@author jonfoocy-reused
//Reused from https://github.com/se-edu/addressbook-level2 with modifications,
//including idea of preparing Commands.
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
// doesn't seem to enter this block
return new IncorrectCommand("Unknown Command");
throw new IncorrectCommandException("Command cannot be empty");
}

final String commandWord = matcher.group("commandWord");
Expand Down Expand Up @@ -105,14 +104,12 @@ public Command parseCommand(String userInput) {
return new ExitCommand();

default:
//todo
return new HelpCommand();
}
//@@author
}

private Command prepareAddTodo(String args) {
assert !args.isEmpty();
final Matcher matcher = TODO_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
return new IncorrectCommand("Description of a Todo can't be empty.");
Expand All @@ -123,7 +120,6 @@ private Command prepareAddTodo(String args) {

// TODO: trim whitespace from end of deadline description
private Command prepareAddDeadline(String args) {
assert !args.isEmpty();
final Matcher matcher = DEADLINE_FORMAT.matcher((args.trim()));
if (!matcher.matches()) {
return new IncorrectCommand("Wrong deadline format.");
Expand All @@ -139,7 +135,6 @@ private Command prepareAddDeadline(String args) {

// TODO: trim whitespace from end of event description
private Command prepareAddEvent(String args) {
assert !args.isEmpty();
final Matcher matcher = EVENT_FORMAT.matcher((args.trim()));
if (!matcher.matches()) {
return new IncorrectCommand("Wrong event format.");
Expand All @@ -154,7 +149,6 @@ private Command prepareAddEvent(String args) {
}

private Command prepareDelete(String args) {
assert !args.isEmpty();
try {
int targetIndex = Integer.parseInt(args.trim());
return new DeleteCommand(targetIndex);
Expand All @@ -164,7 +158,6 @@ private Command prepareDelete(String args) {
}

private Command prepareDone(String args) {
assert !args.isEmpty();
try {
int targetIndex = Integer.parseInt(args.trim());
return new DoneCommand(targetIndex);
Expand All @@ -175,7 +168,6 @@ private Command prepareDone(String args) {

// TODO: add functionality to search by >1 keyword
private Command prepareFind(String args) {
assert !args.isEmpty();
final Matcher matcher = KEYWORDS_ARGS_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
return new IncorrectCommand("Please pass a keyword to search for.");
Expand All @@ -184,10 +176,8 @@ private Command prepareFind(String args) {
}

private Command prepareFilter(String args) {
assert !args.isEmpty();
final Matcher matcher = FILTER_ARGS_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
System.out.println("Wrong filter format");
return new IncorrectCommand("Please pass a proper date to filter by.");
}
try {
Expand All @@ -213,10 +203,8 @@ private Command prepareVocab(String args) {
}

if (matcher.group("definition") == null) {
System.out.println("no definition provided");
return new VocabCommand(matcher.group("word"));
} else {
System.out.println("definition provided");
return new VocabCommand(matcher.group("word"), matcher.group("definition"));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/sparrow/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class Storage {

private final Path path;

public Storage() throws Exception {
this(DEFAULT_FILE_PATH);
public Storage() {
path = Path.of(DEFAULT_FILE_PATH);
}

/**
Expand Down
6 changes: 0 additions & 6 deletions src/test/java/sparrow/ParserTest.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/test/java/sparrow/SparrowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,5 @@

public class SparrowTest {

@Test
public void exceptionMessage() {
assertEquals("ARR!\uD83C\uDFF4\u200D\u2620\uFE0F️ ", SparrowException.STANDARD_EXCEPTION_MESSAGE);
}

}

0 comments on commit a264f12

Please sign in to comment.