Weak password Exception
Write a program to implement and handle a custom exception for password validation.
Constraints:
A password is said to be strong if it satisfies the following criteria
i) It should be a minimum of 8 characters and a maximum of 20 characters.
ii) It should contain at least one digit.
iii)It should contain at least one special character (non-numeric, non-alphabetic).
iv)It should contain at least one letter.
If the password fails any one of the criteria, it is considered as weak.
Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.
Consider a class called User with the following private attributes.
Attributes | Datatype |
name | String |
mobile | String |
username | String |
password | String |
The methods for appropriate getters, setters and constructors are given in the template code.
Override the toString() method to display the user detail in User class
Consider a class called UserBO and define the following methods.
Method & Description
static void validate(User u)
This method throws WeakPasswordNumber exception if the Password is weak.
Consider a driver class called Main. In the Main method, obtain inputs from the user. Validate the password and if there is an exception, handle the exception.
Pass the exception message as "Your password is weak".
The link to download the template code is given below
Code Template
Sample Input and Output:
Refer to sample Input and Output for formatting specifications.
[All text in bold corresponds to the input and rest corresponds to the output]
Sample Input and Output 1:
Enter the user details
John Doe,9876543210,john,johndoe
WeakPasswordException: Your password is weak
Sample Input and Output 2:
Enter the user details
Jane doe,9876543210,Jane,Janedoe@123
Name:Jane doe
Mobile:9876543210
Username:Jane
Password:Janedoe@123
public class User {
private String name;
private String mobile;
private String username;
private String password;
public User(String name, String mobile, String username, String password) {
this.name = name;
this.mobile = mobile;
this.username = username;
this.password = password;
}
public User()
{
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString(){
String output ="Name:"+getName()+"\nMobile:"+getMobile()+"\nUsername:"+getUsername()+"\nPassword:"+getPassword();
return output;
}
}
public class UserBO {
public static void validate(User u) throws WeakPasswordException{
//Your code here
int c=0;
if(!(u.getPassword().length()<21 && u.getPassword().length()>7))
throw new WeakPasswordException(" Your password is weak");
if((u.getPassword().matches("[^a-zA-Z0-9]+")))
throw new WeakPasswordException(" Your password is weak");
if((u.getPassword().matches("[^0-9]+")))
throw new WeakPasswordException(" Your password is weak");
if((u.getPassword().matches("[^a-zA-Z]+")))
throw new WeakPasswordException(" Your password is weak");
}
}
public class WeakPasswordException extends Exception{
public WeakPasswordException(String msg){
super(msg);
}
//Your code here
}
import java.util.*;
public class Main {
public static void main(String[] args)throws Exception {
//Your code here
Scanner sc = new Scanner(System.in);
System.out.println("Enter the user details");
String str = sc.nextLine();
String[] input = str.split(",");
User u = new User(input[0],input[1],input[2],input[3]);
try{
UserBO.validate(u);
System.out.println(u.toString());
}catch(WeakPasswordException e){
System.out.println(e);
}
}
}
MOTIVATION :
“The way we spend our time defines who we are ”
Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.
VISITE MORE BLOGS