From e80e73b4e59e0ce3129968984e212e8d04de182a Mon Sep 17 00:00:00 2001 From: raydenlim <109991591+raydenlim@users.noreply.github.com> Date: Fri, 15 Sep 2023 15:46:45 +0800 Subject: [PATCH] A-Assertions --- build.gradle | 5 +++-- data/tasks.txt | 13 +++++-------- src/main/java/Command/AddCommand.java | 2 +- src/main/java/Command/DeadLineCommand.java | 2 ++ src/main/java/Command/DeleteCommand.java | 1 + src/main/java/Command/EchoCommand.java | 1 + src/main/java/Command/EventCommand.java | 3 +++ src/main/java/Command/FindCommand.java | 1 + src/main/java/Command/MarkCommand.java | 1 + src/main/java/Command/ToDoCommand.java | 1 + src/main/java/Command/UnmarkCommand.java | 1 + src/main/java/Duke/Duke.java | 2 ++ src/main/java/Parser/Parser.java | 10 ++++++---- 13 files changed, 28 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index 1306309ade..d31a8ed777 100644 --- a/build.gradle +++ b/build.gradle @@ -34,7 +34,6 @@ dependencies { } configurations { - // Define a configuration named 'runtime' that selects the appropriate classifier runtime { if (org.gradle.internal.os.OperatingSystem.current().isWindows()) { resolutionStrategy.eachDependency { DependencyResolveDetails details -> @@ -72,15 +71,17 @@ test { } application { - mainClass.set("duke.Duke") + mainClass.set("app.Launcher") } shadowJar { archiveBaseName = "duke" + archiveFileName = 'duke.jar' archiveClassifier = null dependsOn("distZip", "distTar") } run { standardInput = System.in + enableAssertions = true; } \ No newline at end of file diff --git a/data/tasks.txt b/data/tasks.txt index 65f164e533..3ae9af6f4a 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -1,8 +1,5 @@ -[T][ ] hehe -[T][ ] lol -[A][ ] lol -[A][ ] move -[A][ ] move1 -[A][ ] move2 -[A][ ] move 21 -[A][ ] move22 +[A][ ] hehe +[D][ ] (by: now) +[A][ ] airubg +[D][ ] ugbia (by: efuba woubjs) +[A][ ] ff diff --git a/src/main/java/Command/AddCommand.java b/src/main/java/Command/AddCommand.java index d7353211d9..3cc470a71e 100644 --- a/src/main/java/Command/AddCommand.java +++ b/src/main/java/Command/AddCommand.java @@ -39,9 +39,9 @@ public AddCommand(Task taskToAdd) { */ @Override public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + assert taskToAdd != null : "Task to add cannot be null."; taskList.addTask(taskToAdd); storage.saveTask(taskList.getTasks()); return ui.showTaskAdded(taskToAdd, taskList.getTaskCount()); - } } diff --git a/src/main/java/Command/DeadLineCommand.java b/src/main/java/Command/DeadLineCommand.java index 8292615f1e..284f24ca0f 100644 --- a/src/main/java/Command/DeadLineCommand.java +++ b/src/main/java/Command/DeadLineCommand.java @@ -42,6 +42,8 @@ public DeadLineCommand(String description, String by) { */ @Override public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + assert description != null : "Description cannot be null."; + assert by != null : "by attribute cannot be null."; DeadLine deadline = new DeadLine(description, by); taskList.addTask(deadline); storage.saveTask(taskList.getTasks()); diff --git a/src/main/java/Command/DeleteCommand.java b/src/main/java/Command/DeleteCommand.java index c8a41557cd..cf1be98760 100644 --- a/src/main/java/Command/DeleteCommand.java +++ b/src/main/java/Command/DeleteCommand.java @@ -42,6 +42,7 @@ public DeleteCommand(int taskIndex) { @Override public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { try { + assert taskIndex > 0 && taskIndex < taskList.getTaskCount() : "Invalid task index/ out of range"; Task removedTask = taskList.deleteTask(taskIndex); storage.saveTask(taskList.getTasks()); return ui.showDeletedTask(removedTask, taskList.getTaskCount()); diff --git a/src/main/java/Command/EchoCommand.java b/src/main/java/Command/EchoCommand.java index 5ca0acf6bf..da971cef37 100644 --- a/src/main/java/Command/EchoCommand.java +++ b/src/main/java/Command/EchoCommand.java @@ -36,6 +36,7 @@ public EchoCommand(String message) { */ @Override public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + assert message != null : "You cant echo nothing :("; return ui.showEcho(message); } } diff --git a/src/main/java/Command/EventCommand.java b/src/main/java/Command/EventCommand.java index 25b9b43314..ff8a17d867 100644 --- a/src/main/java/Command/EventCommand.java +++ b/src/main/java/Command/EventCommand.java @@ -45,6 +45,9 @@ public EventCommand(String description, String from, String to) { */ @Override public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + assert description != null : "Description cannot be null."; + assert from != null : "from attribute cannot be null."; + assert to != null : "to attribute cannot be null."; Event event = new Event(description, from, to); taskList.addTask(event); storage.saveTask(taskList.getTasks()); diff --git a/src/main/java/Command/FindCommand.java b/src/main/java/Command/FindCommand.java index 304ff32ebe..db4ff73563 100644 --- a/src/main/java/Command/FindCommand.java +++ b/src/main/java/Command/FindCommand.java @@ -35,6 +35,7 @@ public FindCommand(String keyword) { */ @Override public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + assert keyword != null : "Search word cannot be null."; return ui.showMatchingTasks(taskList.findTasks(keyword)); } } diff --git a/src/main/java/Command/MarkCommand.java b/src/main/java/Command/MarkCommand.java index 997b40ade8..27b7b7e0aa 100644 --- a/src/main/java/Command/MarkCommand.java +++ b/src/main/java/Command/MarkCommand.java @@ -46,6 +46,7 @@ public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeExce if (taskIndex < 0 || taskIndex >= taskList.getTaskCount()) { throw new DukeException("Invalid task index."); } + assert taskIndex >= 0 && taskIndex < taskList.getTaskCount() : "Invalid task index."; try { Task taskToMark = taskList.getTasks().get(taskIndex); diff --git a/src/main/java/Command/ToDoCommand.java b/src/main/java/Command/ToDoCommand.java index 7491281927..3a15f180c8 100644 --- a/src/main/java/Command/ToDoCommand.java +++ b/src/main/java/Command/ToDoCommand.java @@ -38,6 +38,7 @@ public ToDoCommand(Task taskToDo) { */ @Override public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException { + assert taskToDo != null : "Task todo cannot be null."; taskList.addTask(taskToDo); storage.saveTask(taskList.getTasks()); return ui.showTaskAdded(taskToDo, taskList.getTaskCount()); diff --git a/src/main/java/Command/UnmarkCommand.java b/src/main/java/Command/UnmarkCommand.java index a8778a38d0..98e30dcc5d 100644 --- a/src/main/java/Command/UnmarkCommand.java +++ b/src/main/java/Command/UnmarkCommand.java @@ -39,6 +39,7 @@ public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeExce if (taskIndex < 0 || taskIndex >= taskList.getTaskCount()) { throw new DukeException("Invalid task index."); } + assert taskIndex >= 0 && taskIndex < taskList.getTaskCount() : "Invalid task index."; try { Task taskToUnmark = taskList.getTasks().get(taskIndex); diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java index cf7e9ff8b7..3c3d1d9f15 100644 --- a/src/main/java/Duke/Duke.java +++ b/src/main/java/Duke/Duke.java @@ -39,6 +39,8 @@ public Duke(String filePath) throws DukeException, FileNotFoundException { scanner = new Scanner(System.in); taskList = new TaskList(); ui = new Ui(); + assert directoryPath != null : "Directory path cannot be empty"; + assert textPath != null : "Text path cannot be empty"; storage = new Storage(directoryPath, textPath, taskList); storage.loadTasks(); } diff --git a/src/main/java/Parser/Parser.java b/src/main/java/Parser/Parser.java index 6b2e48d811..92b0ba9284 100644 --- a/src/main/java/Parser/Parser.java +++ b/src/main/java/Parser/Parser.java @@ -94,10 +94,6 @@ public static Command parse(String input) throws DukeException { String[] words = input.split(" "); String command = words[0].toLowerCase(); - if (words.length < 2 && !command.equals("bye") && !command.equals("list") && !command.equals("help")) { - throw new DukeException("The description of the command cannot be empty."); - } - switch (command) { case "bye": return new ByeCommand(); @@ -108,6 +104,9 @@ public static Command parse(String input) throws DukeException { Task todoTask = new ToDo(todoDesc); return new ToDoCommand(todoTask); case "add": + if (words.length < 2) { + throw new DukeException("Please specify a task to add."); + } String addDesc = input.substring(4).trim(); Task addTask = new Add(addDesc); return new AddCommand(addTask); @@ -150,6 +149,9 @@ public static Command parse(String input) throws DukeException { String echoText = input.substring(5).trim(); // Extract the text return new EchoCommand(echoText); case "delete": + if (words.length < 2) { + throw new DukeException("Please provide me a task index to delete"); + } int taskNumToDel = Integer.parseInt(words[1]) - 1; return new DeleteCommand(taskNumToDel); case "help":