Esempio n. 1
0
  /**
   * Determines the command denoted by the line.
   *
   * @param line the text as typed in at the console.
   * @return the command represented by the line, or NONE if the line is empty.
   * @throws InvalidCommandException if no recognizable command was found on a non-empty line.
   */
  static Command getCommand(String line) throws InvalidCommandException {
    StringTokenizer tokenizer = new StringTokenizer(line);
    if (!tokenizer.hasMoreTokens()) {
      return NONE;
    }
    String command = tokenizer.nextToken();

    /* Check for a manifest command */
    for (Command c : Command.values()) {
      if (c.manifest && c.name().equalsIgnoreCase(command)) {
        if (!tokenizer.hasMoreTokens()) {
          return c;
        }
        /* Extra token. */
        throw new InvalidCommandException(
            "Unexpected argument: " + tokenizer.nextToken() + " for command: " + command);
      }
    }
    /* A stock update command, token following arg must be a price*/
    if (!tokenizer.hasMoreTokens()) {
      throw new InvalidCommandException("Unknown command: " + command + "\n" + StockQuotes.usage());
    }
    String price = tokenizer.nextToken();

    try {
      Float.parseFloat(price);
      if (tokenizer.hasMoreTokens()) {
        throw new InvalidCommandException("Extraneous argument: " + tokenizer.nextToken());
      }
    } catch (NumberFormatException e) {
      throw new InvalidCommandException("Stock price must be a numeric value, not: " + price);
    }

    return UPDATE;
  }
 public static void main(String[] argv) throws Exception {
   StockQuotes stockQuotes = new RouterDrivenStockQuotes(argv);
   stockQuotes.runExample();
 }