/
UI.java
46 lines (36 loc) · 1.15 KB
/
UI.java
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
44
45
46
import java.io.IOException;
import java.io.PrintStream;
public class UI {
protected static final String USAGE = "Usage: blah-blah <input> <output>";
private Display _display;
private Rot13StringLoader _stringFactory;
public UI(PrintStream out, Configuration configuration) {
this(new ConsoleDisplay(out), new Rot13StringFileSystemLoader(new FileSystem(configuration)));
}
public UI(Display display, Rot13StringLoader stringFactory) {
_display = display;
_stringFactory = stringFactory;
}
public void go(String[] args) {
try {
CommandLine commandLine = new CommandLine(args);
if (!commandLine.valid()) {
_display.write(USAGE);
return;
}
TransformableString string = _stringFactory.load(commandLine.inputFilename());
string.transform();
string.writeTo(_display);
string.saveAs(commandLine.outputFilename());
}
catch (IOException e) {
_display.write(e.getLocalizedMessage());
}
}
public static void main(String[] args) {
main(args, System.out, Configuration.production());
}
public static void main(String[] args, PrintStream out, Configuration configuration) {
new UI(out, configuration).go(args);
}
}