package com.skraylabs.poker;

import java.io.InputStream;

public class Application {

  static final String MSG_TOO_FEW_ARGS = "Too few arguments";
  static final String MSG_TOO_MANY_ARGS = "Too many arguments";
  static final String MSG_USAGE = "Usage: PokerCalculator filepath";
  static final String MSG_INVALID_INPUT = "Input is formatted incorrectly";
  static final String MSG_FILE_NOT_OPENED = "File [%s] could not be opened";
  static final int ERROR_CODE_BAD_ARGS = 1;
  static final int ERROR_INVALID_INPUT = 2;
  static final int ERROR_FILE_NOT_OPENED = 3;

  private static Application app;

  private String errorMessage;
  private String filepath;

  /**
   * Access the filepath where Application will attempt to read input from.
   *
   * @return relative filepath to input file
   */
  public String getFilepath() {
    return filepath;
  }

  public static void main(String... args) {
    app = new Application();
    app.execute(args);
  }

  /**
   * Execute application
   *
   * @param args should be exactly 1 string specifying the input filepath to read from.
   */
  public void execute(String... args) {
    if (!validate(args)) {
      System.out.println(errorMessage);
      System.out.println(MSG_USAGE);
      exit(ERROR_CODE_BAD_ARGS);
    } else {
      this.filepath = args[0];
      // TODO: process input from file
    }
  }

  /**
   * Terminate execution with a given error code.
   * @param errorCode nonzero for abnormal termination.
   */
  public void exit(int errorCode) {
    System.exit(errorCode);
  }

  /**
   * Validate the command line arguments. Will set errorMessage to an appropriate value if
   * validation fails.
   *
   * @param args command line arguments
   * @return true if valid, false otherwise
   */
  private boolean validate(String[] args) {
    boolean result = true;
    if (args.length < 1) {
      errorMessage = MSG_TOO_FEW_ARGS;
      result = false;
    } else if (args.length > 1) {
      errorMessage = MSG_TOO_MANY_ARGS;
      result = false;
    }
    return result;
  }

  /**
   * Helper method to create an input stream from which to read game state.
   *
   * @return input stream that can be processed for board and pocket cards.
   */
  InputStream createInputStream() {
    InputStream result = null;
    // TODO: implement me!
    return null;
  }

}

@@ -23,16 +23,181 @@
 poker;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0A
 import j
@@ -1449,71 +1449,585 @@
     
-this.filepath = args%5B0%5D;%0A      // TODO: process input from file
+// Create input stream from filepath%0A      this.filepath = args%5B0%5D;%0A      InputStream input = null;%0A      try %7B%0A         input = createInputStream();%0A      %7D catch (FileNotFoundException e) %7B%0A        errorMessage = String.format(MSG_FILE_NOT_OPENED, filepath);%0A        System.out.println(errorMessage);%0A        exit(ERROR_FILE_NOT_OPENED);%0A      %7D%0A%0A      // TODO: process input from file%0A%0A      // Close input stream%0A      if (input != null) %7B%0A        try %7B%0A          input.close();%0A        %7D catch (IOException e) %7B%0A          // Could not close input stream.%0A        %7D%0A      %7D
 %0A   
@@ -2884,16 +2884,100 @@
  cards.%0A
+   * @throws FileNotFoundException if %7B@link Application#filepath%7D cannot be found.%0A
    */%0A  
@@ -3008,16 +3008,44 @@
 tream() 
+throws FileNotFoundException
 %7B%0A    In
@@ -3104,19 +3104,159 @@
     
-return null
+if (!StringUtils.isBlank(filepath)) %7B%0A      File inputFile = new File(filepath);%0A      result = new FileInputStream(inputFile);%0A    %7D%0A    return result
 ;%0A  

Implement Application#createInputStream().