ok so at this point we've got the basic UI working and I'm just going to bring that up on the screens just so
we can look at it again and its certainly not pretty is not going to win any awards
for the best UI but it is functional so it's probably time to
think about what a user will want to do in addition to viewing the to do list so
obviously at the moment they can click on and see the information see the to do list
items but obviously they want to add items to list themselves and of course if you refer
back to a few videos ago we hard coded a few to-do list items because of course
we need a data to test the user interface now if user wants to install this
application there wouldn't be any to do items and it wouldn't be very useful
for them and in addition when they add to do items and then close the
application they reasonably expected to to-do items to be there next time they
run the application so what we gonna do is quickly add some code that will load and store
to-do items to a flat files so we are gonna start with storing the items and then
run our application that will save the hard coded items that we've already coded into our
app if you think back to earlier in this section on JavaFX you probably recall
that the application class which our main class extends from has a stopped method
that we can override and that would be a good logical place to store data however
the controller will need the data to populate the list view and it also has
ready access to the list views contents which when we want to store them
so for experience sake and because we are doing this you know in a quick and dirty
way so we can get back to our user interface coding what
we are gonna do is added a singleton class that both our main class and the controller can access
in a nut shell we are gonna use a singleton when want there to be only one instance of a class
created over the entire run of the application
because of this the singleton class creates one instance of itself or
creates the one instance of itself and its got a private constructor to
ensure that no other class can create an instance now there's more to it than that
but the important thing for us
is that the singleton usually contains a static method that allows any class to
get the single instance and to call its methods and what this means is that our main
class and our controller will have an easy way to access our dad and that's really the
reason that we doing this so what we gonna do is we are gonna add the singleton
class to our data model package so lets go ahead and start typing some code so gonna
closed down our user interface and then come over here to our data model
package and I'm gonna right click that click on new and select Java class and I'm going to
enter...as the name of the class so what we want to do first is we want to create a new
instance of this class so.....
...
....and this is going to be the file name where ultimately the to do items are gonna
be saved so......
....
...ok so we also want to store our items so....
...
....which is traditional in singleton class to do that get single instance...
....
...
....and by setting the constructor to be private
it prevent anyone from being able to instantiate a new version of this class as an
object and so in our private constructor we are going to....
....
...and bearing in mind they are case sensitive as well so
make sure your typing it exactly like that so that is our private constructor so next
we want a method to return our to do list items so just do....
...
probably should have actually called the setter as well so we'll do that as well
for our to do list items okay so we can create them okay so that's that created and what we want to do now is create a method that will be used to load our
to do items from the file and I'm gonna write this code using the old try finally syntax and I
know we could have used a try with resources that you've learned earlier in this course but we're going to go
back to use to try finally and perhaps that can be an exercise for you later after you've
done this when you can actually convert this try finally to a try with resources so
let's go ahead and write the load to-do items method so....
...
.....
....and will talk a bit more
about that as we go through and we need to specify a path to the files so...
...
....
...
...so we are pointing to the location where the file is and we also want a string which
is going to contain the data for each line so lets move up so that we get a
bit more space so we can see what we are doing and we are gonna add a try here so....
...
....
....but within the try block itself we're going to
create the loop that goes through and retrieves the data so.....
....
ok so the data will use a string array for that so....
...
....
....I take that back short description is
of course the one on the left hand side
details is the one on the right hand side I was one step ahead of myself
the second one is the date string...
...okay and then what we want to do is convert the date to a format that we can
read it so...
.....the formater we set up in the
private constructor and then we would just create the to do item so at this point we've got the
three pieces of information the short description the details and the date
string we formatted the date so we want to add a to do so....
....
...
....so we built up an array list of our items and we are good to go so that's the
method that retrieves them load to do items so now we need to create one more
method and I'll just go through a couple of things and we will finish this video and what we'll do is we'll
come down here and we'll write the next method and the next one last method in
this class is...and that is going to be the
one that saves the data that's also gonna throw an IOException and for this
one
similar we need a path to point through where we're going to save it
....
...
....ok so we are going to iterate through the list of to do items
and save them one entry at a time to the text file
so we are going to create an iterator so....
...so it builds up an iterator that
we can then cycle through record by record entry by entry from our list of to do items
and retrieve one at a time....
testing to see whether there's another entry in the iterator we can use and if their
is this code is going to be executed so we'll...
...
....
...
....
...
....alright so
that should be our methods now so again looking back at the code we've got the load
to-do items that reads the to do items from a file so the store items method writes data
the to do items to a file and as I mentioned we are using the tab delimiter
to delimit each to do items value we wouldn't do this probably in a real application what we would do is
more than likely store the items as XML or perhaps even use the data base and
particularly if we thought there would be a lot of them because using a text file for large
amounts of record soon becomes very impractical but it's
gonna server our purpose quite well for this sample application
now the set to do items which we saw here on line 38 is temporary
we only needed when we run the application then closed it to save
the hard coded items and getting back to the singleton
because this is a singleton whenever any part of our application wants to access the
data it has to call to do data.getinstance.gettodoitems because
of course we set our constructor to be a private one meaning you can
instantiate a new object from this class by any other means essentially and we
are gonna add more methods to this class as we need them so I'm gonna stop the video here now and
in the next video what we'll do is we'll swing over to the main class and will override
the stop method and put some code in there to start using this to do data so I'll
see you in the next video