Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. package assignment4;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Random;
  9.  
  10.  
  11. public class PokemonZoo {
  12. private Random rand = new Random(10);
  13. private ArrayList<SkillMove>movesList;
  14. private ArrayList<Pokemon>pokemonList;
  15.  
  16. public PokemonZoo() {
  17. this.movesList = new ArrayList<SkillMove>();
  18. this.pokemonList = new ArrayList<Pokemon>();
  19. }
  20.  
  21. private int getRandomInt(int range) {
  22. return rand.nextInt(range);
  23. }
  24.  
  25.  
  26. public ArrayList<SkillMove> loadMoves() {
  27.  
  28. try {
  29. FileReader fr = new FileReader("skillMove.txt"); //opens file specified by fileName
  30. BufferedReader br = new BufferedReader(fr);
  31.  
  32. // declare and initialize a variable of type ArrayList
  33. //ArrayList<SkillMove> movesList = new ArrayList<SkillMove>();
  34.  
  35. String currentLine = br.readLine();
  36.  
  37. while (currentLine != null) { // stops reading after last line of file
  38. String[] str = currentLine.split("\t"); // use split() method to isolate each piece
  39. SkillMove newSkillMove = new SkillMove(str[0], str[1], Double.parseDouble(str[2]), Double.parseDouble(str[3])); // creates a new skillMove
  40.  
  41. movesList.add(newSkillMove); // adds new move to list
  42. currentLine = br.readLine(); // reads next line
  43. }
  44.  
  45. System.out.print(movesList);
  46.  
  47.  
  48.  
  49. }catch (FileNotFoundException e1) {
  50. ArrayList<SkillMove> emptyList = new ArrayList<SkillMove>();
  51. return emptyList; // returns empty list
  52. }catch (IOException e2) {
  53. System.out.println(e2);
  54. }
  55. return movesList; //returns created movesList
  56. }
  57.  
  58. public ArrayList<Pokemon> loadPokemon(){
  59.  
  60. try {
  61. FileReader fr = new FileReader("pokemons.txt"); //opens file specified by fileName
  62. BufferedReader br = new BufferedReader(fr);
  63.  
  64. // declare and initialize a var of type ArrayList
  65. ArrayList<Pokemon> pokemonList = new ArrayList<Pokemon>();
  66.  
  67. String currentLine = br.readLine();
  68.  
  69. while (currentLine != null) { // stops reading after last line of file
  70. String[] str = currentLine.split("\t"); // use split() method to isolate each piece
  71.  
  72.  
  73. ArrayList<SkillMove>pokeMoves = new ArrayList<SkillMove>();
  74. int i = 0;
  75. while (i< Integer.parseInt(str[3])) {
  76. int num = getRandomInt(movesList.size());
  77.  
  78. pokeMoves.add(movesList.get(num));
  79. i++;
  80. }
  81.  
  82. Pokemon newPokemon = new Pokemon(str[0], Double.parseDouble(str[1]), str[2], pokeMoves);
  83.  
  84.  
  85. pokemonList.add(newPokemon); // adds new move to list
  86. currentLine = br.readLine(); // reads next line
  87. }
  88.  
  89. br.close();
  90. fr.close();
  91.  
  92. System.out.print(pokemonList);
  93.  
  94.  
  95. }catch (FileNotFoundException e3) {
  96. ArrayList<Pokemon> emptyList = new ArrayList<Pokemon>();
  97. return emptyList; // returns empty list
  98. }catch (IOException e2) {
  99. System.out.println(e2);
  100. }
  101. return pokemonList; //returns created movesList
  102.  
  103. }
  104.  
  105. public static void main(String[] args) {
  106. PokemonZoo zoo = new PokemonZoo();
  107. System.out.print(zoo.loadPokemon());
  108.  
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement