From 9dc4cb946700c713cd085f8535996d62381c2e13 Mon Sep 17 00:00:00 2001 From: Hemrish Bundhoo Date: Tue, 30 Mar 2021 23:36:09 +0800 Subject: [PATCH 1/2] Handle slash in file name --- .../duke/commands/AddCheatSheetCommand.java | 23 ++++++++++++++----- .../commands/DeleteCheatSheetCommand.java | 4 +++- .../duke/commands/ListLessonsCommand.java | 11 +++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main/java/seedu/duke/commands/AddCheatSheetCommand.java b/src/main/java/seedu/duke/commands/AddCheatSheetCommand.java index 9077c30c6..783ba101b 100644 --- a/src/main/java/seedu/duke/commands/AddCheatSheetCommand.java +++ b/src/main/java/seedu/duke/commands/AddCheatSheetCommand.java @@ -7,6 +7,7 @@ import seedu.duke.ui.UI; import java.io.File; +import java.io.FileNotFoundException; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Path; @@ -34,7 +35,7 @@ public void execute(UI ui) throws CommandException { throw new CommandException(MESSAGE_INVALID_FILE_NAME); } try { - String directoryPath = getDirectoryPath(module); + String directoryPath = getDirectoryPath(module, ui); String filePath = directoryPath + fileName + TXT_FORMAT; Path path; path = Paths.get(filePath); @@ -49,20 +50,30 @@ public void openTextEditor(UI ui, Path path, String filePath) { if (Files.exists(path)) { ui.printMessage(MESSAGE_CHEAT_SHEET_ALREADY_EXISTS); } else { - File file = new File(filePath); + try { + File file = new File(filePath); + } catch (NullPointerException e) { + ui.printMessage(MESSAGE_INVALID_FILE_NAME); + } + ui.printMessage(String.format(MESSAGE_CHEATSHEET_ADDED, fileName)); TextEditor textEditor = new TextEditor(filePath); textEditor.setTextAreaToVoid(); textEditor.saveTextToFile(); - textEditor.loadFile(filePath); } } - public String getDirectoryPath(Module module) { + public String getDirectoryPath(Module module, UI ui) { String directoryPath = FOLDER_PATH + PATH_DELIMITER + module.getModuleCode() + PATH_DELIMITER + STRING_CHEATSHEET + PATH_DELIMITER; - Path path = Paths.get(directoryPath); - assert Files.isDirectory(path) : "Directory missing"; + try { + Path path = Paths.get(directoryPath); + assert Files.isDirectory(path) : "Directory missing"; + } catch (InvalidPathException e) { + ui.printMessage(MESSAGE_INVALID_FILE_NAME); + } + + return directoryPath; } } diff --git a/src/main/java/seedu/duke/commands/DeleteCheatSheetCommand.java b/src/main/java/seedu/duke/commands/DeleteCheatSheetCommand.java index 80fb48cdd..e39c66c81 100644 --- a/src/main/java/seedu/duke/commands/DeleteCheatSheetCommand.java +++ b/src/main/java/seedu/duke/commands/DeleteCheatSheetCommand.java @@ -27,7 +27,9 @@ public DeleteCheatSheetCommand(String nameOfFile) { public void execute(UI ui) throws CommandException { Module module = ModuleList.getSelectedModule(); try { - String directoryPath = getDirectoryPath(module); + String directoryPath = getDirectoryPath(module, ui); + + filePath = directoryPath + fileName + TXT_FORMAT; Path path = Paths.get(filePath); performFunction(ui, path); diff --git a/src/main/java/seedu/duke/commands/ListLessonsCommand.java b/src/main/java/seedu/duke/commands/ListLessonsCommand.java index bb81bc098..810c23686 100644 --- a/src/main/java/seedu/duke/commands/ListLessonsCommand.java +++ b/src/main/java/seedu/duke/commands/ListLessonsCommand.java @@ -10,6 +10,7 @@ import static seedu.duke.common.Messages.FORMAT_INDEX_ITEM_DETAILS; import static seedu.duke.common.Messages.INDENTATION; +import static seedu.duke.common.Messages.MESSAGE_LESSONS_LIST_EMPTY; import static seedu.duke.common.Messages.MESSAGE_LESSONS_TO_LIST; /** @@ -18,6 +19,7 @@ public class ListLessonsCommand extends Command { //@@author H-horizon + /** * Prints list of lessons in selected module. * @@ -27,8 +29,13 @@ public class ListLessonsCommand extends Command { public void execute(UI ui) { Module module = ModuleList.getSelectedModule(); String moduleCode = module.getModuleCode(); - ui.printMessage(String.format(MESSAGE_LESSONS_TO_LIST, moduleCode)); - printLessons(module.getLessonList(), ui); + + if (module.getLessonList().size() == 0) { + ui.printMessage(String.format(MESSAGE_LESSONS_TO_LIST, moduleCode)); + printLessons(module.getLessonList(), ui); + } else { + ui.printMessage(MESSAGE_LESSONS_LIST_EMPTY); + } } /** From 5205ffae62420d69f979c886b8d7536cf767f70f Mon Sep 17 00:00:00 2001 From: Hemrish Bundhoo Date: Tue, 30 Mar 2021 23:41:39 +0800 Subject: [PATCH 2/2] Fix typo in printLessons --- src/main/java/seedu/duke/commands/ListLessonsCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/duke/commands/ListLessonsCommand.java b/src/main/java/seedu/duke/commands/ListLessonsCommand.java index 810c23686..5b8f83bd2 100644 --- a/src/main/java/seedu/duke/commands/ListLessonsCommand.java +++ b/src/main/java/seedu/duke/commands/ListLessonsCommand.java @@ -30,7 +30,7 @@ public void execute(UI ui) { Module module = ModuleList.getSelectedModule(); String moduleCode = module.getModuleCode(); - if (module.getLessonList().size() == 0) { + if (module.getLessonList().size() > 0) { ui.printMessage(String.format(MESSAGE_LESSONS_TO_LIST, moduleCode)); printLessons(module.getLessonList(), ui); } else {