Skip to content

Commit

Permalink
Merge pull request #192 from H-horizon/file-name-handling
Browse files Browse the repository at this point in the history
Handle slash in file name
  • Loading branch information
Hemrish authored Mar 30, 2021
2 parents 597c644 + 5205ffa commit 031623d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
23 changes: 17 additions & 6 deletions src/main/java/seedu/duke/commands/AddCheatSheetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/seedu/duke/commands/ListLessonsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -18,6 +19,7 @@
public class ListLessonsCommand extends Command {

//@@author H-horizon

/**
* Prints list of lessons in selected module.
*
Expand All @@ -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);
}
}

/**
Expand Down

0 comments on commit 031623d

Please sign in to comment.