Making a List
Making the list was the simplest part. I just created an ArrayList of type Pokemon and added different Pokemon objects to it. For right now I only added three: Charmander, Squirtle, and Bulbasaur. The three first generation starter pokemon. I made sure all of their attributes were accurate to the game.
Serializing the List
To serialize the list I had to find a way to make a new line in the Pokedex.csv file. This was easily the most time consuming part as I could not find a solution. I actually found out how to do it while in my MATH 250 class (I still took my notes, don’t worry). To create a new line I had to use System,lineSeparator() and then use StandardOpenOption.APPEND to append the new line in the file. After figuring out how to append and create a new line all I had to do was write down the logic and then my list would be added to my Pokedex. I created a for loop that iterated through the list and added each Pokemon object individually on a new line each time. This was it for the serialization part.
Deserializing into a New List
Now this is the real kicker. To deserialize I had to find a way to count the lines of the file and then read each line and convert the lines into new Pokemon objects and THEN add those to a new list. I found out that you could use Files.lines(Path path).count() in order to count the lines in a specific file. After finding the number of lines, I was easily able to iterate through the file to make new Pokemon objects and add them to my new list.
Checking if the Lists are Equal
To check if the lists were equal I made a simple Junit tests that uses the original as the expected list and the clone as the actual and then used assertEquals() to check the equality of the two lists. Howeever, during testing I couldn’t get the debugger to do what I wanted (I still need more practice using it) so instead I gave the orignal list a new Pokemon object Arceus after the new list was already serialized, and it displayed an error just how I wanted to.
I am currently working on different ways of sorting the lists by Overriding the compareTo() method. I attempted to sort by weight and height but my logic was wrong somehow. I’ll try to figure it out soon.
Articles and Forums
P.S. I really hate the smart indentation sometimes. I got frustrated for at least fifteen real minutes trying to write a huge boolean and the indentations not being where I wanted them to be.