/* BaseballGameClient.java ** Java application that is for the purpose of testing the BaseballGame ** instance class. ** If a command line argument (which jGrasp refers to as a "run argument") ** is provided, it is taken to be the name of the file from which input is ** to be read. If no command line argument is provided, input is read from ** standard input (i.e., the keyboard). ** The input data is expected to begin with an integer indicating the ** intended length of the game, in innings, and is to be followed, on ** subsequent lines, by a sequence of r's and o's, with an 'r' indicating ** the occurrence of a "run scored" event and an 'o' indicating the ** occurrence of an "out is made" event. An example of valid input ** (describing an incomplete 9-inning game) is ** +---------+ ** |9 | ** |ooro | ** |ooroorro | ** |oo | ** +---------+ */ import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class BaseballGameClient { private static Scanner input; private static boolean echo; private static boolean keepGoing = true; public static void main(String[] args) throws FileNotFoundException { if (args.length > 0) { // use args[0] as name of input file echo = true; input = new Scanner(new File(args[0])); } else { // read from standard input (keyboard) echo = false; input = new Scanner(System.in); } int innings = getIntFromScanner("Enter duration of game in # innings:"); BaseballGame game = new BaseballGame(innings); System.out.print("New " + game.inningsRequired() + "-inning game... "); System.out.println(game); String operations; do { System.out.print("\nEnter event code(s) (o/r/q):"); operations = input.nextLine(); if (echo) { System.out.println(operations); } performOperations(game, operations); } while (keepGoing && operations.length() != 0 && !game.isOver()); if (game.isOver()) { System.out.print("\n*** Game won by "); if (game.winningTeam() == BaseballGame.HOME) { System.out.print("home"); } else if (game.winningTeam() == BaseballGame.VISITOR) { System.out.print("visitor"); } else { System.out.print(" THIS SHOULD NEVER BE PRINTED!!"); } System.out.println(" by a score of HOME:" + game.runsScored(BaseballGame.HOME) + " VISITOR:" + game.runsScored(BaseballGame.VISITOR)); } else { System.out.println("\n*** Game not over"); } System.out.println("Goodbye."); } /* Performs upon the given baseball game the operations indicated ** by the operation codes ('o' or 'O' to record an out; 'r' or 'R' ** to record a run being scored; 'q' or 'Q' to quit) in the given ** string. After each operation is applied, the state of the game ** is displayed. */ private static void performOperations(BaseballGame g, String operations) { final int N = operations.length(); for (int i=0; i != N && keepGoing; i++) { char opCode = operations.charAt(i); if (opCode == 'o' || opCode == 'O') { System.out.print("Recording an out ... "); g.recordOut(); } else if (opCode == 'r' || opCode == 'R') { System.out.print("Recording a run ... "); g.recordRun(); } else if (opCode == 'q' || opCode == 'Q') { System.out.print("Quitting ... "); keepGoing = false; } else { System.out.print(opCode + " is not a valid operation code ... "); } System.out.println(g); } } /* Prints the specified string, reads an integer from the 'input' Scanner, ** and returns the result. */ private static int getIntFromScanner(String prompt) { System.out.print(prompt); int result = Integer.parseInt(input.nextLine()); if (echo) { System.out.println(result); } return result; } }