Use a while loop Use multiple loop controlling conditions Use a boolean method Use the increment operator
Question:
- Use a while loop
- Use multiple loop controlling conditions
- Use a boolean method
- Use the increment operator
- Extra credit: Reuse earlier code and call two methods from main
This is another password program. In this case, your code is simply going to ask for a username and password, and then check the input against four users. The program will give the user three tries to input the correct username-password combination.
There will be four acceptable user-password combinations:
- alpha - alpha1
- beta - beta1
- gamma - gamma1
- delta - delta1
If the user types in one of the correct username-password combinations, then the program will output: "Login successful." Here are a couple of example runs (but your code needs to work for all four user-password combinations):
Username: beta Type the current password: beta1 Login successful. Username: delta Type your current password: delta1 Login successful.
If the user types the wrong username-password combination, then the program will output "Username or password incorrect" and give them another chance. Your program should give them three chances. The first two times the program will also output "Try again," but on the third unsuccessful try it will output "Too many attempts" and end.
Here is an example of what your program should output when there are three unsuccessful login attempts:
Username: mary Type the current password: secret Username or password incorrect. Try again. Username: alpha Type the current password: supersecret Username or password incorrect. Try again. Username: angela Type the current password: beta1 Username or password incorrect. Too many attempts.
Here are two examples of initial failed logins followed by a successful login:
Username: alpha Type the current password: beta1 Username or password incorrect. Try again. Username: alpha Type the current password: alpha1 Login successful.
Username: alpha Type the current password: beta Username or password incorrect. Try again. Username: mary Type the current password: beta1 Username or password incorrect. Try again. Username: gamma Type the current password: gamma1 Login successful.
In order to make this happen, ask for the username and password inside a while loop in the main method. Do the checks for a correct combination, and remain in the loop as long as the combinations are not correct - but only for three iterations. Use the increment operator to count the number of attempts. It is going to be tricky to print out the right messages for different situations.
Instead of checking all the username-password combinations in the main method, write a method called userPasswordMatch that takes a username and password as input. Send the username and password values from the main method to userPasswordMatch. If the combination matches one of the four acceptable user-password combinations, userPasswordMatch should return true, otherwise it should return false. (A reason to do this is to make it possible to add new users without touching the main method.) (Hint: You can use the return value of userPasswordMatch to control the while loop.) Don't do unnecessary tests, i.e. if a test is successful, don't continue to do more tests.
If the user successfully logs in, then let them change their password like last week. If they successfully log in, then ask them twice for a new password. Work last week's code into this week's program, and reuse the passwordChecker method from last week (fix it if necessary) using the same criteria for acceptable passwords.