Skip to content

Commit 11e07b4

Browse files
committed
Separate the long method into smaller one to reduce nested method
1 parent bf9e7d1 commit 11e07b4

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/main/java/seedu/address/logic/parser/AutocompleteParser.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,22 +268,32 @@ private ArrayList<String> getAllFilesInArchiveDirectory() {
268268

269269
// Check if the directory exists and is indeed a directory
270270
if (directory.exists() && directory.isDirectory()) {
271-
// List all files and directories in the specified path
272-
File[] files = directory.listFiles();
273-
assert (files != null);
274-
ArrayList<String> result = new ArrayList<>();
275-
for (File file : files) {
276-
// add file name if it is a json file
277-
if (isJson(file)) {
278-
result.add(file.getName());
279-
}
280-
}
281-
return result;
271+
// Get all files and directories in the specified path
272+
return getAllJsonFiles(directory);
282273
} else {
274+
// Return an empty list if the directory does not exist
283275
return new ArrayList<>();
284276
}
285277
}
286278

279+
/**
280+
* Gets all the json file name from a directory of given Path
281+
* @param directory The file path to the directory
282+
* @return An array list of all json file name
283+
* */
284+
private ArrayList<String> getAllJsonFiles(File directory) {
285+
File[] files = directory.listFiles();
286+
assert (files != null);
287+
ArrayList<String> result = new ArrayList<>();
288+
for (File file : files) {
289+
// add file name if it is a json file
290+
if (isJson(file)) {
291+
result.add(file.getName());
292+
}
293+
}
294+
return result;
295+
}
296+
287297
/**
288298
* checks if the given file is a json file.
289299
* @param file the file to check
@@ -292,6 +302,8 @@ private boolean isJson(File file) {
292302
return file.isFile() && file.getName().toLowerCase().endsWith(".json");
293303
}
294304

305+
306+
295307
private String getStringWithPrefix(String word, String command) {
296308
if (word.length() <= 1) {
297309
return command;

0 commit comments

Comments
 (0)