Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[T3A4][W09-A3]Yim Chia Hui #117

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/seedu/addressbook/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ private void exit() {
private void runCommandLoopUntilExitCommand() {
Command command;
do {
if (!storage.ifFileExists()) {
ui.showToUser("File just disappeared for no reason...");
}
String userCommandText = ui.getUserCommand();
command = new Parser().parseCommand(userCommandText);
CommandResult result = executeCommand(command);
Expand Down
16 changes: 16 additions & 0 deletions src/seedu/addressbook/storage/StorageFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

/**
* Represents the file used to store address book data.
Expand All @@ -28,6 +30,8 @@ public class StorageFile {
/** Default file path used if the user doesn't provide the file name. */
public static final String DEFAULT_STORAGE_FILEPATH = "addressbook.xml";

private static final String MESSAGE_ERROR_MISSING_STORAGE_FILE = null;

/* Note: Note the use of nested classes below.
* More info https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
*/
Expand Down Expand Up @@ -85,6 +89,18 @@ public StorageFile(String filePath) throws InvalidStorageFilePathException {
private static boolean isValidPath(Path filePath) {
return filePath.toString().endsWith(".xml");
}

public boolean ifFileExists() {
ArrayList<String> lines = null;
try {
lines = new ArrayList<>(Files.readAllLines(Paths.get(DEFAULT_STORAGE_FILEPATH)));
} catch (FileNotFoundException fnfe) {
return false;
} catch (IOException ioe) {
return false;
}
return true;
}

/**
* Saves all data to this storage file.
Expand Down