This programme will allow the user to keep a shopping list as a text file and sort it into alphabetical order.
00:09
We have our usual boilerplate and we'll need to use collections for storing the shopping list in memory and I O for reading and writing it to a file.
00:19
In our main programme, we will simply call the menu subprogram.
00:24
Let's write the code for that now.
00:27
Static void menu.
00:29
We'll hold the user's choice in a variable identified as choice and assign that to be an empty string to start.
00:36
There will be four options, with the fourth being to end the programme.
00:41
So while choice is not, four will allow the programme to keep going, presenting options to the user until they close the programme.
00:50
Remember, all inputs are strings, so this is qualified with double Quotes.
00:56
Output the four options to the user 1 add an item, 2 output the shopping list, 3 sort the list and 4 quit.
01:06
The user may make an invalid input when we collect it in a moment.
01:10
So we could use a while loop to keep asking until we get a valid input while choice not in the list Of Valid inputs 1, 2, 3, 3 or 4.
01:22
This isn't very scalable.
01:24
That means if the number of options increased, this would not be a good way to code this.
01:29
We'd want to do something less tedious.
01:32
One way could be to store the valid options in an array and cheque if the input is in the array.
01:38
You might also recognise that this while loop isn't really needed at all, since we're looping until the user inputs are 4 anyway and you'd be correct.
01:48
Prompt the user for their choice and store that in choice.
01:53
Switch from the user's choice.
01:55
If it's one, then call the add item subprogram and reset the choice.
02:01
If it's two, then call the output items subprogram and reset the choice.
02:05
And if it's three then call the sortitems subprogram and reset the choice.
02:12
That's the menu written.
02:13
And this code is a pretty standard algorithm.
02:16
You can use it in all situations where you want a user selection.
02:20
Coding it in a subprogram means we've created a reusable programme component.
02:25
We can copy and paste it into other programmes and it will work just fine.
02:30
Ok.
02:31
On, to adding an item.
02:33
Next, static void Add item.
02:36
The new item will become what is input by the user with a helpful prompt.
02:41
The items will be stored in a file so the user can close the programme, open it later, and continue writing their shopping list.
02:49
A stream writer provides a pipe file writer.
02:53
to the file shopping Txt with True as a second argument.
02:58
So we don't overwrite the data that's already there.
03:02
We can now, use filewriter, writeline to put the new item into the file and finally we close our file.
03:12
Next, we want a routine to output all the items.
03:15
Static void output items.
03:18
Now, if this is the first time the programme is run or the user has deleted the shopping list, the file might not exist, which would cause the programme to crash.
03:28
We can trap that using what is called exception handling.
03:32
Anything that can happen when the programme is running that could cause a problem is known as an exception.
03:38
This could be printing when there's no ink in the printer, writing a file to a drive that's full.
03:44
Lots of different things that should be foreseen when we write programmes.
03:48
We didn't cheque if this was likely to be an issue with the drive when we added the items.
03:53
And really we should have done so.
03:56
So let's do that this time.
03:58
Instead we want to try and open the shopping list file and if we can't, instead of crashing, do something else.
04:06
The command for that is try, followed by what we want to try.
04:10
So assigning the file pipe and reading the contents of the file.
04:15
Here we're going to take another new approach just to show you different ways of achieving the same thing.
04:21
We read the contents of the file into a variable called filecontent and then we're going to split it up using the end of line markers into an array identified as item.
04:33
Don't worry if this all looks a little complicated.
04:36
We could have read the text file in a line at a time and added each line to a list as we go.
04:42
Instead, this way isn't any better and could actually be more of a problem.
04:47
With large files then we handle our exceptions with the command catch.
04:54
There are lots of built in constants for different kinds of errors.
04:58
The one we want is filenotfoundexception for a file that cannot be found.
05:04
In that case, we want to make a blank file to assign the file pipe for writing to shopping Txt instead and close the file.
05:13
That will put an empty file on our drive.
05:18
Finally, sorting our, shopping list into alphabetical order.
05:22
Static void sort items.
05:25
You can't sort items directly in the file, so the process is to read the data in from the file into an array or list and then sort the array or list before outputting it back to the file.
05:36
For exams you need to know how sorting algorithms actually work, but we'll use array sort for that to take away the work.
05:45
Our file might not exist, so try to assign a pipe identified as file reader to the shopping.
05:52
Txt file read in the data.
05:55
If you can, close the file and then split it up into an array called items.
06:01
Then use the array sort method to sort the data in memory before writing it back out to the file, this time overwriting what was already there.
06:12
Catch an error if this happens because the file doesn't exist and, write an empty file instead.
06:20
That's the programme for our shopping list written.