Skip to content

Commit

Permalink
Merge pull request #1 from tjwjoe/branch-A-Assertions
Browse files Browse the repository at this point in the history
Add relevant assert statements
  • Loading branch information
tjwjoe committed Sep 4, 2020
2 parents 456f71f + 5c2c463 commit d44a6c7
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 12 deletions.
5 changes: 3 additions & 2 deletions data.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
D # 1 # IPPT # 31/10/1998 23:59
E # 0 # Friend Dinner # 25/08/2021 11:53
D # 0 # IPPT next year # 31/10/1999 23:59
D # 0 # ippt # 31/10/1997 23:59
E # 0 # National Day # 09/08/2020 12:00
D # 1 # ippt # 31/10/1997 23:59
E # 0 # National Day # 09/08/2020 12:00
D # 0 # Tutorial # 03/09/2020 23:59
12 changes: 7 additions & 5 deletions src/main/java/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public AddCommand(String text) {
@Override
public void exec(TaskList tasks, Ui ui, Storage storage) throws InvalidTaskException, UnknownCmdException,
InvalidTimeException, BadDtFormatException {
assert ui != null : "Null UI";
assert tasks != null : "Null TaskList";
String[] info = extractInfo(text);
if (info[0].equals("todo")) {
tasks.addItem(new Todo(info[1], false));
Expand All @@ -63,26 +65,27 @@ public void exec(TaskList tasks, Ui ui, Storage storage) throws InvalidTaskExcep
*/

public String[] extractInfo(String str) throws InvalidTaskException, UnknownCmdException, InvalidTimeException {
assert str != null : "Input is null!";
String[] store = new String[3];
// Handling the classification of event type
if (str.startsWith("todo")) {
if (str.equals("todo") || str.strip().equals("todo")) {
if (str.strip().equals("todo")) {
throw new InvalidTaskException("Your task cannot be empty!");
} else if (!str.startsWith("todo ")) {
throw new UnknownCmdException("Unknown command entered!");
} else {
store[0] = "todo";
}
} else if (str.startsWith("deadline")) {
if (str.equals("deadline") || str.strip().equals("deadline")) {
if (str.strip().equals("deadline")) {
throw new InvalidTaskException("Your task cannot be empty!");
} else if (!str.startsWith("deadline ")) {
throw new UnknownCmdException("Unknown command entered!");
} else {
store[0] = "deadline";
}
} else if (str.startsWith("event")) {
if (str.equals("event") || str.strip().equals("event")) {
if (str.strip().equals("event")) {
throw new InvalidTaskException("Your task cannot be empty!");
} else if (!str.startsWith("event ")) {
throw new UnknownCmdException("Unknown command entered!");
Expand All @@ -92,8 +95,7 @@ public String[] extractInfo(String str) throws InvalidTaskException, UnknownCmdE
} else {
throw new UnknownCmdException("Unknown command entered!");
}

// handling the content of the event
// Handling the content of the event
int splitPrefix = str.indexOf(" ");
String content = str.substring(splitPrefix).strip();
if (content.length() <= 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public DeleteCommand(int index) {
@Override
public void exec(TaskList tasks, Ui ui, Storage storage) throws TaskNotFoundException,
InvalidTaskNumberException {
// ui.printDelTask(tasks, index);
assert ui != null : "Null UI";
assert tasks != null : "Null TaskList";
ui.setMessageDeleteTask(tasks, index);
deleteTask(index, tasks);
}
Expand All @@ -40,6 +41,7 @@ public void exec(TaskList tasks, Ui ui, Storage storage) throws TaskNotFoundExce
* @throws InvalidTaskNumberException If user enters a non-integer argument.
*/
public void deleteTask(int idx, TaskList tasks) throws TaskNotFoundException, InvalidTaskNumberException {
assert tasks != null : "Null TaskList";
try {
tasks.getTasks().remove(idx - 1);
} catch (IndexOutOfBoundsException e) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public DoneCommand(int index) {
@Override
public void exec(TaskList tasks, Ui ui, Storage storage) throws TaskNotFoundException,
InvalidTaskNumberException {
// ui.printDoneTask();
assert ui != null : "Null UI";
assert tasks != null : "Null TaskList";
ui.setMessageDoneTask();
doneTask(index, tasks);
}
Expand All @@ -41,6 +42,7 @@ public void exec(TaskList tasks, Ui ui, Storage storage) throws TaskNotFoundExce
*/

public void doneTask(int idx, TaskList tasks) throws TaskNotFoundException, InvalidTaskNumberException {
assert tasks != null;
try {
tasks.getTasks().get(idx - 1).setDone(true);
} catch (IndexOutOfBoundsException e) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class ExitCommand extends Command {
*/
@Override
public void exec(TaskList tasks, Ui ui, Storage storage) throws InvalidFileException {
// ui.printExitMessage();
assert ui != null : "Null Ui";
assert storage != null : "Null Storage";
assert tasks != null : "Null TaskList";
ui.setMessageExit();
storage.writeToFile("data.txt", tasks.writeString());
Platform.exit();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public FindCommand(String text) {
*/
@Override
public void exec(TaskList tasks, Ui ui, Storage storage) {
assert ui != null : "Null UI";
assert tasks != null : "Null Tasklist";
StringBuilder output = new StringBuilder();
int counter = 1;
for (Task task : tasks.getTasks()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class HelpCommand extends Command {
*/
@Override
public void exec(TaskList tasks, Ui ui, Storage storage) {
assert ui != null : "Null UI";
ui.setMessagePrintHelp();
}
}
2 changes: 2 additions & 0 deletions src/main/java/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class ListCommand extends Command {
*/
@Override
public void exec(TaskList tasks, Ui ui, Storage storage) {
assert ui != null : "Null UI";
assert tasks != null : "Null TaskList";
ui.setMessagePrintList(tasks);
}
}
3 changes: 3 additions & 0 deletions src/main/java/commands/UndoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public UndoneCommand(int index) {
@Override
public void exec(TaskList tasks, Ui ui, Storage storage) throws TaskNotFoundException,
InvalidTaskNumberException {
assert ui != null : "Null UI";
assert tasks != null : "Null TaskList";
ui.setMessageUndoneTask();
undoneTask(index, tasks);
}
Expand All @@ -40,6 +42,7 @@ public void exec(TaskList tasks, Ui ui, Storage storage) throws TaskNotFoundExce
*/

public void undoneTask(int idx, TaskList tasks) throws TaskNotFoundException, InvalidTaskNumberException {
assert tasks != null : "Null TaskList";
try {
tasks.getTasks().get(idx - 1).setDone(false);
} catch (IndexOutOfBoundsException e) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void run() {
}

public String getResponse() {
assert ui.getMessage() != null : "Message is null!";
return ui.getMessage();
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public boolean getQuit() {
* @return A Command object corresponding to what the user has input.
*/
public Command parse(String input) {
assert !isQuit;
if (input.equals("exit")) {
isQuit = true;
return new ExitCommand();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ public Storage(File file, TaskList tasks) {
*/
public Storage() {
file = new File("data.txt");
tasks = new TaskList();
}

/**
* Reads data from a pre-existing .txt file.
* @throws FileNotFoundException If required file is not found.
*/
public void readData() throws FileNotFoundException {
assert file != null : "Null file";
assert tasks != null : "Null TaskList";
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String[] line = reader.nextLine().split(" # ");
Expand Down Expand Up @@ -70,6 +73,7 @@ public void readData() throws FileNotFoundException {
* @throws InvalidFileException File was not found at the end of the input path.
*/
public void writeToFile(String path, String text) throws InvalidFileException {
assert path.equals("data.txt");
try {
FileWriter fw = new FileWriter(path);
fw.write(text);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void setMessagePrintHelp() {
this.message = "Welcome to Duke! Here is a list of commands you can use: \n"
+ "exit - shuts down the bot\n"
+ "todo <name> - adds a Todo task to your list\n"
+ "deadline <name> <time> - adds a Deadline task to your list\n"
+ "event <name> <time> - adds an Event task to your list\n"
+ "deadline <name> /by <time> - adds a Deadline task to your list\n"
+ "event <name> /at <time> - adds an Event task to your list\n"
+ "done <number> - marks a task as done\n"
+ "delete <number> - deletes a task from your list\n"
+ "list - displays the current list of your tasks\n"
Expand Down

0 comments on commit d44a6c7

Please sign in to comment.