Leave a rating/review
Booleans and if statements and expressions are really powerful, which is why I’ve got an awesome challenge for you to practice them! :]
In the starter project, you’ll find your challenge. You have to create and validate user registration data which consists of an email, a username and a password.
There are certain set of rules described in the project to verify if the data is valid or not. You have to use an if expression to define a message for the user, depending on each of the rules the data has to follow.
Once you assign an appropriate message, you have to print if any of the rules have been broken, or if the data is valid, and the registration has been successful!
That’s it!!!
You’ll use the knowledge we’ve covered so far in this part to solve this challenge. So take your time and rewatch episodes for concepts if you have to.
Now pause the video, and solve the challenges :]
Then once you’re done, play the video, to compare your solution with mine. Good luck! :]
Challenge :
Check if the user data for registering an account is valid.
Data is valid if:
1. Properties are not empty.
2. Username has at least 6 characters.
3. Password has at least 10 characters.
4. The email contains a '@' and a ‘.`.
Hint: use the `contains()` function on a String to check if a value is contained in it.
Hint: use the `isEmpty()` function on a String to check if the string is empty.
Example: email.isEmpty()
Use `if` as an expression, to assign a respective error message if any of the cases fail! Then print it out.
The messages are prepared for you in the project.
First off, you have to create three values to hold the data like so:
val email = "filip@mail.com"
val password = "password123"
val username = "filip.babic"
These describe the registration data.
Then, create a message constant, and assign to it the following expression:
val message = if(email.isEmpty() || password.isEmpty() || username.isEmpty()) {
} else if (username.length < 6) {
}
The isEmpty() function used here returns true if the character sequence is empty.
These cover the first two rules, that properties can’t be empty, and the username has to be at least 6 characters long.
Next, lets cover the third rule:
val message = if(email.isEmpty() || password.isEmpty() || username.isEmpty()) {
} else if (username.length < 6) {
} else if(password.length < 10) {
}
The password needs to be at least 10 characters and this will ensure that. Now for the fourth rule, enter the following:
val message = if(email.isEmpty() || password.isEmpty() || username.isEmpty()) {
} else if (username.length < 6) {
} else if(password.length < 10) {
} else if (!email.contains("@") || !email.contains(".")) {
}
These are all the failing cases, ending with the email format being wrong.
Finally, add the successful case as the else block :
val message = if(email.isEmpty() || password.isEmpty() || username.isEmpty()) {
} else if (username.length < 6) {
} else if(password.length < 10) {
} else if (!email.contains("@") || !email.contains(".")) {
} else {
}
Now add all the messages for each of the conditions:
val message = if (email.isEmpty() || password.isEmpty() || username.isEmpty()) {
"You must fill in your data!"
} else if (username.length < 6) {
"Username needs to be at least 6 characters long!"
} else if (password.length < 10) {
"Password needs to be at least 10 characters long!"
} else if (!email.contains("@") || !email.contains(".")) {
"Invalid email format."
} else {
"Successful registration!"
}
If all the if and else if cases fail, and the code reaches the else block,
It means that all the values follow the four rules for valid data and that the user can be registered.
Run to project to see if that’s the case with your data.
We dont have any message printed out and this is because i forgot to add a print statement. I’ll add that now like so:
println(message)
Then run the project once again.
Cool!!! The success message is printed out.
To make sure one of the negative cases works, change the password to be just "password",
and see if you get a password error message, by running the project.
val email = "filip@mail.com"
val password = "password"
val username = "filip.babic"
The case works as you can see an error message is printed out!