Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment4;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Random;
- public class PokemonZoo {
- private Random rand = new Random(10);
- private ArrayList<SkillMove>movesList;
- private ArrayList<Pokemon>pokemonList;
- public PokemonZoo() {
- this.movesList = new ArrayList<SkillMove>();
- this.pokemonList = new ArrayList<Pokemon>();
- }
- private int getRandomInt(int range) {
- return rand.nextInt(range);
- }
- public ArrayList<SkillMove> loadMoves() {
- try {
- FileReader fr = new FileReader("skillMove.txt"); //opens file specified by fileName
- BufferedReader br = new BufferedReader(fr);
- // declare and initialize a variable of type ArrayList
- //ArrayList<SkillMove> movesList = new ArrayList<SkillMove>();
- String currentLine = br.readLine();
- while (currentLine != null) { // stops reading after last line of file
- String[] str = currentLine.split("\t"); // use split() method to isolate each piece
- SkillMove newSkillMove = new SkillMove(str[0], str[1], Double.parseDouble(str[2]), Double.parseDouble(str[3])); // creates a new skillMove
- movesList.add(newSkillMove); // adds new move to list
- currentLine = br.readLine(); // reads next line
- }
- System.out.print(movesList);
- }catch (FileNotFoundException e1) {
- ArrayList<SkillMove> emptyList = new ArrayList<SkillMove>();
- return emptyList; // returns empty list
- }catch (IOException e2) {
- System.out.println(e2);
- }
- return movesList; //returns created movesList
- }
- public ArrayList<Pokemon> loadPokemon(){
- try {
- FileReader fr = new FileReader("pokemons.txt"); //opens file specified by fileName
- BufferedReader br = new BufferedReader(fr);
- // declare and initialize a var of type ArrayList
- ArrayList<Pokemon> pokemonList = new ArrayList<Pokemon>();
- String currentLine = br.readLine();
- while (currentLine != null) { // stops reading after last line of file
- String[] str = currentLine.split("\t"); // use split() method to isolate each piece
- ArrayList<SkillMove>pokeMoves = new ArrayList<SkillMove>();
- int i = 0;
- while (i< Integer.parseInt(str[3])) {
- int num = getRandomInt(movesList.size());
- pokeMoves.add(movesList.get(num));
- i++;
- }
- Pokemon newPokemon = new Pokemon(str[0], Double.parseDouble(str[1]), str[2], pokeMoves);
- pokemonList.add(newPokemon); // adds new move to list
- currentLine = br.readLine(); // reads next line
- }
- br.close();
- fr.close();
- System.out.print(pokemonList);
- }catch (FileNotFoundException e3) {
- ArrayList<Pokemon> emptyList = new ArrayList<Pokemon>();
- return emptyList; // returns empty list
- }catch (IOException e2) {
- System.out.println(e2);
- }
- return pokemonList; //returns created movesList
- }
- public static void main(String[] args) {
- PokemonZoo zoo = new PokemonZoo();
- System.out.print(zoo.loadPokemon());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement