From 967f40d710eb07568b6753c6f02d6897f8728999 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 15 Mar 2014 20:04:47 +0100 Subject: Implement command input from file --- src/esieequest/Main.java | 13 ++++- src/esieequest/view/console/Console.java | 78 --------------------------- src/esieequest/view/console/package-info.java | 5 -- src/esieequest/view/text/Console.java | 25 +++++++++ src/esieequest/view/text/FileReader.java | 45 ++++++++++++++++ src/esieequest/view/text/TextInterface.java | 68 +++++++++++++++++++++++ src/esieequest/view/text/package-info.java | 5 ++ test/commands.txt | 2 + 8 files changed, 156 insertions(+), 85 deletions(-) delete mode 100644 src/esieequest/view/console/Console.java delete mode 100644 src/esieequest/view/console/package-info.java create mode 100644 src/esieequest/view/text/Console.java create mode 100644 src/esieequest/view/text/FileReader.java create mode 100644 src/esieequest/view/text/TextInterface.java create mode 100644 src/esieequest/view/text/package-info.java create mode 100644 test/commands.txt diff --git a/src/esieequest/Main.java b/src/esieequest/Main.java index 2ad11b1..59ac568 100755 --- a/src/esieequest/Main.java +++ b/src/esieequest/Main.java @@ -1,6 +1,7 @@ package esieequest; import java.util.Arrays; +import java.util.List; import javax.swing.JApplet; @@ -9,7 +10,8 @@ import esieequest.model.Game; import esieequest.view.View; import esieequest.view.app.Applet; import esieequest.view.app.Window; -import esieequest.view.console.Console; +import esieequest.view.text.Console; +import esieequest.view.text.FileReader; /** * The main class of the program. @@ -44,10 +46,17 @@ public class Main extends JApplet { * the command line arguments */ public static void main(final String[] args) { + final List arguments = Arrays.asList(args); final Game game = new Game(); View view; - if (Arrays.asList(args).contains("--nogui")) { + if (arguments.contains("--file")) { + if (arguments.size() < 2) { + System.out.println("Error: no input file specified."); + return; + } + view = new FileReader(arguments.get(1)); + } else if (arguments.contains("--console")) { view = new Console(); } else { view = new Window(); diff --git a/src/esieequest/view/console/Console.java b/src/esieequest/view/console/Console.java deleted file mode 100644 index 6faaced..0000000 --- a/src/esieequest/view/console/Console.java +++ /dev/null @@ -1,78 +0,0 @@ -package esieequest.view.console; - -import java.util.Scanner; - -import esieequest.controller.GameEngine; -import esieequest.model.Game; -import esieequest.view.View; - -/** - * The textual console view. - * - * @author Pacien TRAN-GIRARD - */ -public class Console implements View { - - private Game game; - private GameEngine gameEngine; - - private boolean running; - - /** - * The default constructor. - */ - public Console() { - this.running = false; - } - - /** - * Runs the input scanner. - */ - private void run() { - final Scanner scanner = new Scanner(System.in); - while (this.running) { - System.out.print("> "); - this.gameEngine.interpret(scanner.nextLine()); - } - scanner.close(); - } - - @Override - public void setModel(final Game game) { - this.game = game; - } - - @Override - public void setController(final GameEngine gameEngine) { - this.gameEngine = gameEngine; - } - - @Override - public void show() { - this.enable(); - } - - @Override - public void enable() { - if (!this.running) { - this.running = true; - this.run(); - } - } - - @Override - public void disable() { - this.running = false; - } - - @Override - public void refresh() { - this.echo(this.game.getLocationInfo()); - } - - @Override - public void echo(final String message) { - System.out.println(message); - } - -} diff --git a/src/esieequest/view/console/package-info.java b/src/esieequest/view/console/package-info.java deleted file mode 100644 index 24089ab..0000000 --- a/src/esieequest/view/console/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * The console view package. Contains the textual user interface and its associated input stream processor. - */ -package esieequest.view.console; - diff --git a/src/esieequest/view/text/Console.java b/src/esieequest/view/text/Console.java new file mode 100644 index 0000000..e7a71dd --- /dev/null +++ b/src/esieequest/view/text/Console.java @@ -0,0 +1,25 @@ +package esieequest.view.text; + +import java.util.Scanner; + +/** + * The textual console view. + * + * @author Pacien TRAN-GIRARD + */ +public class Console extends TextInterface { + + /** + * Runs the console input scanner. + */ + @Override + protected void run() { + final Scanner scanner = new Scanner(System.in); + while (this.running) { + System.out.print("> "); + this.gameEngine.interpret(scanner.nextLine()); + } + scanner.close(); + } + +} diff --git a/src/esieequest/view/text/FileReader.java b/src/esieequest/view/text/FileReader.java new file mode 100644 index 0000000..0bf3d3d --- /dev/null +++ b/src/esieequest/view/text/FileReader.java @@ -0,0 +1,45 @@ +package esieequest.view.text; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +/** + * The textual file reader view. + * + * @author Pacien TRAN-GIRARD + */ +public class FileReader extends TextInterface { + + private File file; + + /** + * The FileReader initialiser. + * + * @param filePath + * the path of the file to read + */ + public FileReader(final String filePath) { + super(); + this.file = new File(filePath); + } + + /** + * Runs the text file input scanner. + */ + @Override + protected void run() { + try { + final Scanner scanner = new Scanner(file); + while (this.running && scanner.hasNextLine()) { + String line = scanner.nextLine(); + System.out.println("> " + line); + this.gameEngine.interpret(line); + } + scanner.close(); + } catch (FileNotFoundException exception) { + System.out.println("Error: file not found."); + } + } + +} diff --git a/src/esieequest/view/text/TextInterface.java b/src/esieequest/view/text/TextInterface.java new file mode 100644 index 0000000..e2de893 --- /dev/null +++ b/src/esieequest/view/text/TextInterface.java @@ -0,0 +1,68 @@ +package esieequest.view.text; + +import java.util.Scanner; + +import esieequest.controller.GameEngine; +import esieequest.model.Game; +import esieequest.view.View; + +/** + * The textual view. + * + * @author Pacien TRAN-GIRARD + */ +abstract class TextInterface implements View { + + protected Game game; + protected GameEngine gameEngine; + + protected boolean running; + + /** + * The default constructor. + */ + public TextInterface() { + this.running = false; + } + + protected abstract void run(); + + @Override + public void setModel(final Game game) { + this.game = game; + } + + @Override + public void setController(final GameEngine gameEngine) { + this.gameEngine = gameEngine; + } + + @Override + public void show() { + this.enable(); + } + + @Override + public void enable() { + if (!this.running) { + this.running = true; + this.run(); + } + } + + @Override + public void disable() { + this.running = false; + } + + @Override + public void refresh() { + this.echo(this.game.getLocationInfo()); + } + + @Override + public void echo(final String message) { + System.out.println(message); + } + +} diff --git a/src/esieequest/view/text/package-info.java b/src/esieequest/view/text/package-info.java new file mode 100644 index 0000000..614ff9f --- /dev/null +++ b/src/esieequest/view/text/package-info.java @@ -0,0 +1,5 @@ +/** + * The console view package. Contains the textual user interface and its associated input stream processor. + */ +package esieequest.view.text; + diff --git a/test/commands.txt b/test/commands.txt new file mode 100644 index 0000000..73d07ea --- /dev/null +++ b/test/commands.txt @@ -0,0 +1,2 @@ +help +quit \ No newline at end of file -- cgit v1.2.3