Skip to content

Commit

Permalink
Merge ad4fb0c into ed58f5c
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangj1an committed Nov 10, 2019
2 parents ed58f5c + ad4fb0c commit 49d1691
Show file tree
Hide file tree
Showing 5 changed files with 516 additions and 9 deletions.
51 changes: 50 additions & 1 deletion src/main/java/com/dukeacademy/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class MainApp extends Application {
/**
* The constant VERSION.
*/
private static Config config;
private static final Version VERSION = new Version(1, 3, 1, true);
private static Logger logger = LogsCenter.getLogger(MainApp.class);

Expand All @@ -72,13 +73,17 @@ public class MainApp extends Application {
private NotesLogic notesLogic;
private ApplicationState applicationState;

public static Config getConfig() {
return config;
}

@Override
public void init() throws Exception {
super.init();

// Retrieves config parameters
AppParameters appParameters = AppParameters.parse(getParameters());
Config config = initConfig(appParameters.getConfigPath());
config = initConfig(appParameters.getConfigPath());

// Sets logging level as described
initLogging(config);
Expand Down Expand Up @@ -154,6 +159,7 @@ private void initLogging(Config config) {
private void initAppPaths(Config config) {
Path testOutputPath = config.getTestOutputPath();
Path dataOutputPath = config.getDataPath();
Path loadPath = config.getLoadPath();

if (!testOutputPath.toFile().exists()) {
logger.info("Test output folder not found at : " + testOutputPath);
Expand All @@ -164,6 +170,18 @@ private void initAppPaths(Config config) {
}
}

if (!loadPath.toFile().exists()) {
logger.info("Loading new question folder not found at : " + loadPath);
logger.info("Creating load new questions folder at : " + loadPath);

if (!loadPath.toFile().mkdirs()) {
logger.warning("Unable to create load new questions directory"
+ " : " + testOutputPath);
}

createCustomQuestionFile(loadPath.resolve("NewProblems.txt"));
}

if (!dataOutputPath.toFile().exists()) {
logger.info("Data folder not found at : " + dataOutputPath);
logger.info("Creating data folder at : " + testOutputPath);
Expand All @@ -173,6 +191,37 @@ private void initAppPaths(Config config) {
}
}

/**
* Helper method to create a custom question file at the specified
* location. Default custom questions are copied.
* @param loadPath the path at which to create the file.
*/
private void createCustomQuestionFile(Path loadPath) {
try {
logger.info("Creating custom question text file.");
// Copy default questions
FileUtil.createIfMissing(loadPath);
InputStream customQuestionsInputStream =
this.getClass().getClassLoader().getResourceAsStream(
"NewProblems.txt");
if (customQuestionsInputStream != null) {
logger.info("Copying custom questions into the specified "
+ "sample \"DukeAcademy/newQuestions\" directory");
Files.copy(customQuestionsInputStream, loadPath,
StandardCopyOption.REPLACE_EXISTING);
customQuestionsInputStream.close();
} else {
logger.warning("Fatal: custom questions for loadquestions "
+ "command not found.");
this.stop();
}
} catch (IOException | NullPointerException e) {
logger.warning("Unable to create custom question data file for "
+ "loadquestions usage.");
}
}


/**
* Helper method to create a question bank json file at the specified location. Default questions are copied.
* @param questionBankFilePath the path at which to create the file.
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/dukeacademy/commons/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Config {
private Level logLevel = Level.INFO;
private Path testOutputPath = Paths.get("DukeAcademy/tests");
private Path dataPath = Paths.get("DukeAcademy/data");

private Path loadPath = Paths.get("DukeAcademy/newQuestions");
public Level getLogLevel() {
return logLevel;
}
Expand All @@ -32,6 +32,10 @@ public Path getTestOutputPath() {
return testOutputPath;
}

public Path getLoadPath() {
return loadPath;
}

public void setTestOutputPath(Path testOutputPath) {
this.testOutputPath = testOutputPath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.dukeacademy.logic.commands.load;

import java.nio.file.Path;
import java.nio.file.Paths;

import com.dukeacademy.MainApp;
import com.dukeacademy.logic.commands.Command;
import com.dukeacademy.logic.commands.CommandFactory;
import com.dukeacademy.logic.commands.exceptions.InvalidCommandArgumentsException;
Expand Down Expand Up @@ -33,11 +33,12 @@ public String getCommandWord() {
public Command getCommand(String commandArguments)
throws InvalidCommandArgumentsException {
Path sampleQuestionsFilePath =
Paths.get(System.getProperty("user.home")).resolve("Desktop").resolve(commandArguments);
MainApp.getConfig().getLoadPath().resolve(commandArguments);
if (!sampleQuestionsFilePath.toFile().exists()) {
throw new InvalidCommandArgumentsException("We cannot find the "
+ "specified text file on your desktop.\n"
+ "Is it you type the file name wrongly?");
+ "specified text file titled \"" + commandArguments + "\" "
+ "inside the \"DukeAcademy/newQuestions\" directory.\n"
+ "Did you type the file name wrongly?");
}
return new LoadCommand(this.questionsLogic, sampleQuestionsFilePath);
}
Expand Down
39 changes: 39 additions & 0 deletions src/main/resources/NewProblems.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Question::
Title::
Apple
Description::
Test Apple
Difficulty::
EASY
Topics::
ARRAY, HASHTABLE
TestCase::
Input::
1
Output::
1
TestCase::
Input::
2
Output::
2

Question::
Title::
Banana
Description::
Test Banana
Difficulty::
MEDIUM
Topics::
LINKED_LIST, HASHTABLE
TestCase::
Input::
1
Output::
11
TestCase::
Input::
2
Output::
22
Loading

0 comments on commit 49d1691

Please sign in to comment.