Your first task is to create a method called CreateAccount. This method will ask
ID: 3684111 • Letter: Y
Question
Your first task is to create a method called CreateAccount. This method will ask a user to input their username and their password. Assume password is valid if it’s a number between 500 and 600. If the password is incorrect, the system should ask the user to input another password (assume this can be done forever). The method will return the credentials of the user (username and password) as a concatenated string separated by comma. For example, if the username is “mary” and the password is “503”, the CreateAccount method will return “mary,503”. 1. Write the pseudocode for the method CreateAccount. 2. Write the java code for the method CreateAccount. 3. ssume the user enters first attempt, "Sophia" and 100 second attempt "Rebecca" and 200, and third attempt, "Janson" and 502. Trace how your system would work for these three inputs. Remember, your code should work well for different usernames and passwords. 4. Note: For this method you will have to work on converting a variable from and integer to string in Java. No need to worry about this in your pseudocode.Explanation / Answer
1. pseudocode for the method CreateAccount
class Account
main()
string credentials = CreateAccount();
output "Credentials: " + credentials;
public string CreateAccount()
// declaration
string userName = "";
int password = 0;
output "Enter the Username:";
input userName ;
do {
output"Enter the Password:";
input password ;
if (password >= 500 and password <= 600) {
break;
} else {
output "Error! Incorrect Password, Try Again";
}
} while (true);
return userName + "," + password;
2. java code for the method CreateAccount
import java.util.Scanner;
public class Account {
/**
* method to read user name and password and validate the password whether
* it is in the range 500 to 600
*/
public static String CreateAccount() {
// declaration
Scanner scanner = null;
String userName = "";
int password = 0;
try {
scanner = new Scanner(System.in);
System.out.print("Enter the Username:");
userName = scanner.next();
do {
System.out.print("Enter the Password:");
password = scanner.nextInt();
if (password >= 500 && password <= 600) {
break;
} else {
System.out.println("Error! Incorrect Password, Try Again");
}
} while (true);
} catch (Exception e) {
// TODO: handle exception
} finally {
scanner.close();
}
return userName + "," + password;
}
/**
* @param args
*/
public static void main(String[] args) {
String credentials = CreateAccount();
System.out.println("Credentials: " + credentials);
}
}
OUTPUT:
Enter the Username:Sophia
Enter the Password:100
Error! Incorrect Password, Try Again
Enter the Password:200
Error! Incorrect Password, Try Again
Enter the Password:502
Credentials: Sophia,502
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.