Create a package like this first:
OUTPUT:
When user enters the wrong password:
When user enters the correct password:
Then in each class, write the following code.
A/N: Login.java is our main class and we run that to check the output of the package.
SOURCE CODE:
LogManager.java
public class LogManager {
String un,pwd;//Stores Sent username and password
public void tryLogin(String un, String pwd){
this.un=un;
this.pwd=pwd;
}
//Send value of password by object
public String getEnteredUn(){
return this.un;
}
public String getEnteredPwd(){
return this.pwd;
}
//Allow password
public void allowLogin(String user){
System.out.println(user+", you are authorized to use this method.");
}
}
Login.java
public class Login {
public static void main(String[] args) {
Scanner sc_un = new Scanner(System.in);
Scanner sc_pwd = new Scanner(System.in);
LogManager lm = new LogManager();
Validator valid = new Validator();
System.out.println("Enter username:");
String un = sc_un.nextLine();
System.out.println("Enter password");
String pwd = sc_pwd.nextLine();
System.out.println("\nPerforming login verification....\n");
lm.tryLogin(un,pwd);
valid.verify(lm);
}
}
Validator.java
public class Validator {
private final String un="siddhant", pwd="@admin";
public void verify(LogManager lm){
//Validator v = new Validator();
if(Objects.equals(pwd, lm.getEnteredPwd()) &&
Objects.equals(un, lm.getEnteredUn())){
lm.allowLogin(this.un);
}
else
{
System.out.println("You've entered the wrong username or password."
+ "\nPlease retry.");
}
}
}
/**
* Tips learned while coding this Class:
* 1. Never create an object of a class
* if you wan to quickly implement it later
* 2. Instead, use it inside a method or psvm
* 3. Objects declared like this can be expected
* to easily pass from one class to the next via
* their underlying methods.
*/
OUTPUT:
When user enters the wrong password:
When user enters the correct password:
0 comments
Post a Comment