This programme will be a simple routine to validate the details on a bank card.
00:07
Typically when the numbers on the card are entered into an online form, some basic checking before the data is sent to the server ensures fewer failed transactions.
00:17
A bank card has 16 digit numbers, but this may be entered with a space after each set of four digits, meaning that either 16 or 19 characters might be entered and both would be valid.
00:31
Let's start with the boilerplate code.
00:39
The first function we'll write is one to validate the number.
00:42
This will return true if it's valid or false if not.
00:47
So static bool that means boolean true or false validatenumber.
00:54
This will require some data.
00:56
So open bracket and then the data we will give it.
00:59
That's the number on the card.
01:01
Close bracket.
01:02
The number will actually be a string because it may contain spaces between each of the four digits.
01:10
There are lots of ways we could write this programme, but we'll assume the number was entered correctly and then attempt to prove it wasn't.
01:18
You could of course write the programme the other way around.
01:21
So bool valid becomes true.
01:25
An integer for the number of characters becomes the length of the number on the card.
01:30
We need to know this to cheque if the number of digits was correct.
01:35
If the number of characters is equal to 16 or the number of characters is equal to 19, then we're good so far.
01:46
But we need to cheque if each character is actually a number or a space.
01:50
Because.
01:50
Because it could be a letter or symbol and that would be invalid to cheque each character, we can use a for loop for each character in number on card.
02:02
If not character isdigit.
02:06
This means if the individual character is not a number and character is not a space, then valid becomes false.
02:16
So to recap, the number on card is a string that will be passed into the function.
02:21
The character is an individual character or number from the number on card.
02:26
This is also a string, but we can use a string method called isdigit to cheque if it could be cast to a number.
02:33
If it can, then it's a digit.
02:37
That's our simple cheque done.
02:39
So if the number of characters on the number on card is not 16 or 19, then then it must be an invalid card.
02:47
So else valid becomes false.
02:51
Then we can return valid which will be either true for a valid card or false for an invalid card.
03:00
Next we'll write a simple subprogram to collect the 16 digit number from the user.
03:05
Static void inputcarddetails.
03:11
This subprogram, does not require data because it's collecting data, so open and close brackets.
03:18
This time we will assume the card is invalid so that we can keep asking the user to enter the number until a valid number is entered.
03:26
So validcard becomes false, while not validcard prompt the user to enter their number and store this in a variable called numberinput.
03:39
We should probably have declared this outside of the loop rather than defining its data type continually in the loop.
03:44
But it doesn't matter either way.
03:48
Then validcard becomes the result or return value from our validatenumber function and we need to pass it the number input.
03:57
So open bracket number input.
04:02
Notice here how we have a subprogram calling another subprogram.
04:07
The code in InputCarddetails calls the ValidNumber subprogram.
04:12
Breaking the problem down into smaller chunks and building it back up again makes the overall problem easier to solve.
04:19
You just need to take one step at a time.
04:23
Okay, that's the subprograms written so the main programme.
04:26
Now we just need to call our InputCardDetails subprogram.
04:30
And we know that must return a valid card number before it completes.
04:35
So we can finish with a simple output to tell the user the card was accepted, give the programme a run and cheque that it works for a variety of situations.