コード例 #1
0
ファイル: TextCommandDAO.java プロジェクト: airhop/cs340zoo
  /**
   * Takes all commands from the database
   *
   * @param gameId game to get the commands for
   * @return list of all commands
   */
  @Override
  public List<ICommand> readAllCommands(int gameId) {
    List<ICommand> commands = new ArrayList<>();
    try {
      File file = new File(fileName + gameId + ".txt");
      if (!file.exists()) return commands;

      GsonBuilder gson = new GsonBuilder();
      BufferedReader br = new BufferedReader(new FileReader(file));
      String line = br.readLine();
      while (line != null) {
        Scanner scan = new Scanner(line);
        String type = scan.next();
        String newCommand = scan.nextLine();
        CommandType ct = CommandType.convert(type);
        commands.add(CommandType.getClass(ct, newCommand));
        line = br.readLine();
      }
      br.close();

    } catch (IOException e) {
      System.out.println("Error in TextCommandDAO readAllPlayers");
    }
    return commands;
  }