In: Computer Science
A successful new user registration requires the following valid data:
⦁ User name: it must start with a letter and consist
of only letters and digits without any space character. User name
is case insensitive. Different users must have different user
names.
⦁ Password: it must have at least six characters,
consist of only letters, digits, and special characters (@, #, $,
^, &), and at least one upper-case letter, one lower-case
letter, one digit, and one special character.
⦁ First name: it must consist of only letters
⦁ Last name: it must consist of only letters
⦁ Email address: it must be a valid email address
⦁ Phone number: it must include three-digit area code
and seven digit phone number
Describe comprehensive acceptance criteria for the “register new user” and “login” user stories which uses the following method names:
⦁ public String registerNewUser(String userName, String password, String reenteredPassword, String firstName, String lastName, String email, String phone)
⦁ public UserAccount login(String userName, String
password)
The acceptance criteria for “register new user” should consider
whether or not reenteredPassword matches password.
Implementation in JAVA;
code:
import java.util.HashMap;
import java.util.Scanner;
public class Username_password_Checker {
// map for Storing data
static HashMap<String , Username > map = new
HashMap<>();
// main method
public static void main(String[] args) {
Scanner s= new
Scanner(System.in);
// enter data from users
System.out.println("\nRegister 1st
user !!\n");
System.out.println("Enter username
: ");
String uname = s.next();
System.out.println("Enter Password
: ");
String pass = s.next();
System.out.println("Re-Rnter
Password : ");
String repass = s.next();
System.out.println("Enter Fname :
");
String fname = s.next();
System.out.println("Enter Lname :
");
String lname = s.next();
System.out.println("Enter Email :
");
String email = s.next();
System.out.println("Enter Phone :
");
String phone = s.next();
registernewuser(uname,pass,repass,fname,lname,email,phone);
System.out.println("\nRegister 2nd user !!\n");
System.out.println("Enter username
: ");
String uname1 = s.next();
System.out.println("Enter Password
: ");
String pass1 = s.next();
System.out.println("Re-Rnter
Password : ");
String repass1 = s.next();
System.out.println("Enter Fname :
");
String fname1 = s.next();
System.out.println("Enter Lname :
");
String lname1 = s.next();
System.out.println("Enter Email :
");
String email1 = s.next();
System.out.println("Enter Phone :
");
String phone1 = s.next();
registernewuser(uname1,pass1,repass1,fname1,lname1,email1,phone1);
System.out.println("\n Account
login check \n");
System.out.println("Enter username
: ");
String username = s.next();
System.out.println("Enter Password
: ");
String password = s.next();
account_login(username,password);
}
// checking account login
public static void account_login(String username,
String password) {
if(map.containsKey(username))
{
Username un =
map.get(username);
if(password.equals(un.password)) {
System.out.println("Username :
"+username);
System.out.println("First_name :
"+un.fname);
System.out.println("Last_Name :
"+un.lname);
System.out.println("Email : "+un.email);
System.out.println("Phone no. :
"+un.phone);
}
else {
System.out.println("Invalid Password!!!");
}
}
else {
System.out.println("Username not Registered ! ");
}
}
// register new user
public static void registernewuser(String username,
String password, String reentered_pass, String fname, String lname,
String email, String phone) {
Scanner s= new
Scanner(System.in);
if(! isvalid_username(username))
{
System.out.print("Invalid username!!!\nPlease enter Valid Username
: ");
username =
s.next();
if(!
isvalid_username(username)) {
System.out.print("Invalid username!!!\nPlease
enter Valid Username : ");
username = s.next();
}
}
if(map.containsKey(username))
{
System.out.println("User Already Registred!!");
return;
}
if(! isvalispassword(password)) {
System.out.print("Invalid password!!!\nPlease enter Valid password
: ");
password =
s.next();
if(!
isvalid_username(password)) {
System.out.print("Invalid password!!!\nPlease
enter Valid password : ");
password = s.next();
}
}
if(! isreenteredpass(password,reentered_pass)) {
System.out.print("Invalid password!!!\nPlease enter Valid password
: ");
reentered_pass =
s.next();
if(!
isreenteredpass(password,reentered_pass)) {
System.out.print("Invalid password!!!\nPlease
enter Valid password : ");
reentered_pass = s.next();
}
}
if(! isvalisname(fname)) {
System.out.print("Invalid First name!!!\nPlease enter Valid First
name : ");
fname =
s.next();
if(!
isvalisname(fname)) {
System.out.print("Invalid First name!!!\nPlease
enter Valid First name : ");
fname = s.next();
}
}
if(! isvalisname(lname)) {
System.out.print("Invalid last name!!!\nPlease enter Valid Last
name : ");
lname =
s.next();
if(!
isvalisname(lname)) {
System.out.print("Invalid Last name!!!\nPlease
enter Valid Last name : ");
lname = s.next();
}
}
if(! isValidmail(email)) {
System.out.print("Invalid email !!!\nPlease enter Valid email :
");
email =
s.next();
if(!
isValidmail(email)) {
System.out.print("Invalid email !!!\nPlease
enter Valid email : ");
email = s.next();
}
}
if(! isvalidphone(phone)) {
System.out.print("Invalid phone !!!\nPlease enter Valid phone :
");
phone =
s.next();
if(!
isvalidphone(phone)) {
System.out.print("Invalid phone !!!\nPlease
enter Valid phone : ");
phone = s.next();
}
}
Username un = new Username( password, reentered_pass, fname, lname,
email, phone);
map.put(username,un);
}
////////////////////////////////////////////////////////////////////////////////////////
// check validations
public static boolean isvalid_username(String
username) {
String us = username;
if(Character.isLetter(us.charAt(0))) {
for(int
i=1;i<us.length();i++) {
if(!Character.isLetterOrDigit(us.charAt(i)))
{
return false;
}
}
}
else {
return
false;
}
return true;
}
public static boolean isvalispassword(String pass)
{
int upper=0;
int lower=0;
int special=0;
int digit=0;
int letter=0;
if(pass.length()<6) {
return
false;
}
for(int i=0;i<pass.length();i++)
{
if(Character.isDigit(pass.charAt(i))) {
digit++;
}
else
if(Character.isLetter(pass.charAt(i))) {
letter++;
if(Character.isUpperCase(pass.charAt(i)))
{
upper++;
}
else if(Character.isLowerCase(pass.charAt(i)))
{
lower++;
}
}
else
if(pass.charAt(i)=='@' || pass.charAt(i)=='#' ||
pass.charAt(i)=='$'|| pass.charAt(i)=='^'
||pass.charAt(i)=='&')
{
special++;
}
else {
return false;
}
}
if(upper>0 && lower>0
&& special>0 && digit>0 &&
letter>0 ) {
return
true;
}
else {
return
false;
}
}
public static boolean isreenteredpass(String pass,
String re_pass) {
if(pass.equals(re_pass)) {
return
true;
}
else {
return
false;
}
}
public static boolean isvalisname(String name) {
for(int i=0;i<name.length();i++)
{
if(!
Character.isLetter(name.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isValidmail(String email)
{
String regex =
"^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
return email.matches(regex);
}
public static boolean isvalidphone(String phone)
{
if(phone.length()!=10) {
return
false;
}
for(int
i=0;i<phone.length();i++) {
if(!
Character.isDigit(phone.charAt(i))) {
return false;
}
}
return true;
}
}
//username class
class Username{
String password;
String reeneterdpass;
String fname;
String lname;
String email;
String phone;
// constructor
public Username(String password, String reeneterdpass,
String fname, String lname, String email, String phone) {
this.password = password;
this.reeneterdpass=
reeneterdpass;
this.fname= fname;
this.lname= lname;
this.email= email;
this.phone= phone;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
SAMPLE OUTPUT;
// PLEASE THUMBS-UP AND RATE POSITIVELY
If you have any doubt regarding this question please ask me in
commnets
// THANK YOU:-)