http://www.mediafire.com/?08d650uxvksce6r (.jar file)
http://www.mediafire.com/?l94qb25bdvhz2q4 (.java file)
I decided to finally finish my little side project yesterday, “Last Slice Decider.” It was largely finished from the last time I worked on it, which was in August.
All I needed to do were three things: successfully loop through three names, write a method to save the loop to a file, and a method to load the file the next time the program is launched.
For my first task, I tinkered with several different loops before finding the best one. I decided to create a member integer variable named “order” and I set its value to 0. Then I created a method called “listPerson()” and created a switch statement with cases numbered 0-2. Each statement used the setText() method of JLabel to display a different name. After each statement, I wrote the brake statement to prevent the switch statement from fulfilling more than one case requirement. I wrote order++ outside the loop so the switch statement will always go to the next case the next time the method is called. Finally, I wrote an if-then statement that sets “order” to 0 if it’s value is equal to 3, successfully going back to the beginning of the loop.
After successfully creating my loop, I decided to look up the best way to load and save data to a file. I found a good tutorial on object serialization and decided to implement it in my program. I made all three password fields transient to prevent the passwords from being saved in the data file. The rest is pretty much self-explanatory. I wrote a method called “saveData()” that creates a file named “pizza.data” and an ObjectOutputStream. The writeObject() method of the ObjectOutputStream is then called and “order” is saved to the pizza.data file. This method is called every time the “listPerson()” method is called (ie, the user clicks the “Yes” button), after all of the switch and if-then statements are executed. Next, I made a method called “loadData()” which does the exact opposite. It loads the pizza.data file and inputs it into an ObjectInputStream, and casts “order” to an Integer object before being read from the file. This method is called at main, which ensures that the data is always up to date.
Don’t worry if you have no idea what the hell I just said, it’s a simple program with a relatively complex user interface. Just download the program and try it out. I know it has no use for you, but it has a lot of use for me. :P All three passwords are “t,” this was just a test when I was debugging my application. My brothers and I will have three completely different and (hopefully) complex passwords when we use this program. I have also included the source code with this post (everything is heavily commented) so feel free to poke around with it.
- aguiney posted this