PM sent.
did you agree with my suggestion of adding everything to a List and having a method to save its contents?
Yes. See new code. input is an ArrayList of Strings. writeInputToFile(String)
does that. When testing it, I had a quick call to ask if they wanted to save and if so, to what filename. Something like this at the bottom of main.
if(ui.isYes("Do you want to save the input to a filename?))
ui.writeInputToFile(ui.getString("Enter filename: ").trim());
In the process, I thought that it would be good to be able to to turn on and off logging so that "yes" and the filename was not included, so I added the quick option to turn it off and on. And the option to clear it. Both are one-liners. The JavaDoc comments are longer than the code. The "try" section of getString is now this (logging variable is a boolean).
answer = br.readLine();
if(logging)
input.add(answer); // logging input
See below for new functions. Constructors remain the same (ie your original ones, the one taking a boolean and dealing with logging and/or output has been removed and instead used as a method, not a constructor). Change or remove as needed.
/**
* Setter for logging variable
* <BR> (Intended for running repeatable test cases)
*
* @param on if true, start/continue logging. if false, stop / do not log.
*/
public void setlogging(boolean on)
{
logging = on;
}
/**
* Clears the input log.
* <BR> (Intended for running repeatable test cases)
*
*/
public void clearLogging()
{
input = new ArrayList<>();
}
/**
* Writes all input to a file for later use test cases.
* <BR> (Intended for running repeatable test cases)
*
* @param filename a file to write to
* @throws RuntimeException an unrecoverable error occurred
*/
public void writeInputToFile(String filename)
{
File file = new File(filename);
System.out.println("Writing input to file " + file.getAbsolutePath());
try {
PrintStream outputFile = new PrintStream(file);
for(int i = 0; i < input.size(); i++) {
outputFile.println(input.get(i));
}
outputFile.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}