We'll start with our boilerplate and because this programme will make use of an array, we'll tell C we need the collections library.
00:12
We'll start with the main programme.
00:14
The notebook will store 10 notes and as these are text, this will be a string identified as notebook, which is a new collection of strings, and there'll be 10 of them, hence the 10 in the square brackets.
00:28
Our notebook will just run continuously until the user closes the app.
00:33
So while true gives us an infinite loop, there are two things we want to do continually.
00:39
That's show the notebook.
00:40
So we'll write a subprogram for that in a moment, passing it the Notebook array and menu, which we will ask the user, what they want to do with the notebook.
00:51
As the notebook array is being used by all our subprograms, it would have been acceptable to use a global array instead of defining it in the main programme and then passing it to the subprograms.
01:03
But as we generally want to avoid global variables when we can, we'll do it this way.
01:08
You could easily make an argument for doing it in a different way, like many programmes.
01:14
Ok, let's define the subprogram to show the notebook on the screen.
01:18
We want to output the 10 notes in a user friendly way, so we'll use a for loop to iterate through each note.
01:25
For integer, index is zero loop.
01:29
When the index is less than the length of the notebook, that's how many notes are in it.
01:33
We could have said 10 here, but maybe we want to expand the notebook in the future index, which means add one to the index in each iteration.
01:45
Note that we didn't use for each here because we want to output the index or the number of the note and not just the note itself.
01:53
Output the index followed by a colon followed by the note stored in the notebook at that index, that's the Show Notebooks subprogram complete.
02:05
Next, let's define our menu.
02:08
Output the options to the user A for adding a note, C for clearing the notebook and O for ordering the notes in the notebook.
02:18
We'll put a colon on the screen as a prompt for the user and collect their choice in a variable identified as choice switch.
02:27
What part of the code executes next?
02:29
Depending on the user's choice in the case they entered A call a subprogram addnote to the notebook, then break out of the condition in the case they entered C call a subprogram, clear notes from the notebook and then break out of the condition in the case they Entered.
02:49
Oh, Call a subprogram, order notes with the notebook and break out of the condition.
02:56
Now we'll write a little helper function to collect a number of a note from the user and make sure it's a valid number.
03:04
Static int because it return a note number, an integer getnotenumber with no data passed in.
03:15
Initialise validinput to be false.
03:18
We haven't collected it yet.
03:20
Will initialise the Note number to be minus 1, so we have a variable to use later.
03:26
Then, while not valid input, we could also use while validinput does not equal true.
03:33
Here it's the same logic.
03:35
This is slightly easier to read.
03:37
Remember, an exclamation mark means not.
03:42
Notenumber becomes the integer of the input from the user.
03:47
Then if the note number is inside the range, that is if it's greater than or equal to 0 and it is less than 10, the input was valid, so valid input becomes true.
04:02
This is very simple validation.
04:04
It is just what is known as a range check.
04:06
It checks the input is in range.
04:09
It doesn't prevent an invalid character being entered.
04:11
We'll learn how to do that in the future.
04:15
Otherwise else output invalid input with a prompt to tell the user what was expected.
04:22
Return the note number entered.
04:25
Next, we'll define the AddNotesubprogram.
04:28
This takes the notebook as an array of strings as a parameter, the index becomes a number entered by the user and we'll call our getnotenumber function for that.
04:40
So creating a little helper function like this means that every time we want a number of a note from the user, we can call this function instead of duplicating code throughout the programme.
04:49
This is called creating a reusable programme component and is a key advantage of a sub programme.
04:57
Ask the user to enter the note and collect that in a string variable identified as note.
05:03
Now, to put the note into the notebook, we can say notebook, at the index becomes note.
05:12
Next, we'll write the subprogram to clear the notebook.
05:15
This is a dangerous thing for the user to do, as they will lose all their notes.
05:19
So a bit of verification from the user that this is actually what they want is a good idea.
05:25
We'll set their choice to be an empty string and then use a while loop to repeatedly ask them to enter Y or N until they give us one of those two inputs.
05:35
So this little bit of code prevents the programme continuing until the user enters either Y or N.
05:42
We could also have done this as a little helper function too.
05:47
If they entered Y, then we loop through all the indexes of our notebook with a for loop and assign the element of the notebook at that index to be empty.
05:58
We could also have done this with a foreach approach too.
06:02
We don't need to say what to do if the user entered n because we don't want anything to happen.
06:09
Finally, we write our order notes subprogram.
06:12
This uses a method array sort to sort the contents automatically.
06:18
This is an example of abstraction.
06:20
We don't know how the computer does the sorting, but we don't need to know either.
06:25
For those of you who are interested, it actually uses what is known as an intro sort of selecting the best algorithm for the data.
06:34
An insertion sort for our small array.
06:36
But it could have used a quicksort or a heap sort for larger data sets.
06:42
Okay, let's give the programme a run.
06:44
We can see it outputs the indexes 0 to 9 with blank notes.
06:49
We can add a note by entering a the note number five and the note we want to store.
06:56
We can see this has been added to index five.
07:00
We can continue adding more notes and test the functionality of the other options.