-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveCommand.java
More file actions
43 lines (38 loc) · 1.09 KB
/
SaveCommand.java
File metadata and controls
43 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package bork;
/**
* Handles the save command.
*
* @author Dr. Zeitz
*/
class SaveCommand extends Command {
private static String DEFAULT_SAVE_FILENAME = "bork";
private String saveFilename;
/**
* Constructs a new SaveCommand object with a file name to be executed.
*
* @param saveFilename the name of the file to save to
*/
SaveCommand(String saveFilename) {
if (saveFilename == null || saveFilename.length() == 0) {
this.saveFilename = DEFAULT_SAVE_FILENAME;
} else {
this.saveFilename = saveFilename;
}
}
/**
* Saves the current GameState to a file with the given file name.
*
* @return whether the save was successful or not
*/
public String execute() {
try {
GameState.instance().store(saveFilename);
return "Data saved to " + saveFilename +
GameState.SAVE_FILE_EXTENSION + ".\n";
} catch (Exception e) {
System.err.println("Couldn't save!");
e.printStackTrace();
return "";
}
}
}