So I am making a login form and it works right now but I was told I need to make
ID: 3877213 • Letter: S
Question
So I am making a login form and it works right now but I was told I need to make it into 2 classes a tester class and then the login form. In the tester class I need the scanner and I need to put the name and password. Also when I type the right name and password in it just states true and I cant find out how to change it into a sentence that says like login succusessful. I Also need to have a reset button and i dont know how to implement that only using the cousnil becauase we are not allowed to use a j frame.
What I need
- Make it into 2 classes a tester and a loginform
- put the scanner in the tester class
- put the name and password in the tester class
- Dont use a j frame
- i need a reset button
import java.util.Scanner;
public class LoginForm {
private String userName;
private String password;
private String button;
private String inputString;
private boolean inputState;
public LoginForm() {
this.userName = "admin";
this.password = "password";
this.inputState = true;
this.inputString = "";
}
public void input(String text) {
if (inputState) {
inputString += text;
inputState = false;
} else {
inputString += text;
inputState = true;
}
}
public void click(String button) {
this.button = button;
if (button.equals("Submit")) {
System.out.println(loggedIn());
} else {
userName = "";
password = "";
inputState = true; // if the user never provides password, inputState will remain false.
}
}
public boolean loggedIn() {
if (inputString.equals(userName + password)) {
return true;
} else {
userName = "";
password = "";
inputState = true;
return false;
}
}
public static void main(String[] args) {
LoginForm test = new LoginForm();
Scanner getInput = new Scanner(System.in);
System.out.println("Enter the username: ");
String username=getInput.next();
System.out.println("Enter the password: ");
String password=getInput.next();
test.input(username);
test.input(password);
test.click("Submit");
}
}
Explanation / Answer
public class LoginForm {
private String userName;
private String password;
private boolean inputState;
private String inputString;
//constructor provide initial user name password
public LoginForm() {
this.userName = "admin";
this.password = "password";
this.inputState = true;
this.inputString = "";
}
//store user enter username and password
public void input(String text) {
if (inputState) {
inputString += text;
}
}
public void click(String button) {
if (button.equalsIgnoreCase("Submit")) {
if (loggedIn())
System.out.println("user login successfully");
else
System.out.println("user name or password is incorrect.try again");
} else {
userName = "";
password = "";
inputState = true; // if the user never provides password, inputState will remain false.
}
}
//reset usearName or password if you to change only password then provide old username
public void resetPassword(String userName,String password){
this.userName = userName;
this.password = password;
}
//logged in
public boolean loggedIn() {
if (inputString.equals(userName + password)) {
inputState = false;
return true;
} else {
userName = "";
password = "";
inputState = true;
return false;
}
}
}
import java.util.Scanner;
public class LoginFormTester {
public static void main(String[] args) {
LoginForm test = new LoginForm();
Scanner getInput = new Scanner(System.in);
try{
System.out.println("Enter the username: ");
String username=getInput.next();
System.out.println("Enter the password: ");
String password=getInput.next();
test.input(username);
test.input(password);
test.click("Submit");
System.out.println("Enter 1 if you want to reset the password");
int i = getInput.nextInt();
if (i == 1){
System.out.println("enter new user name");
username=getInput.next();
System.out.println("Enter new password: ");
password=getInput.next();
test.resetPassword(username, password);
System.out.println("your username and password is change successfully");
}
}
catch(Exception e){
System.out.println("You enter invalid data.please try again");
}
}
}
sample output:
Enter the username:
admin
Enter the password:
password
user login successfully
Enter 1 if you want to reset the password
1
enter new user name
admin
Enter new password:
1234
your username and password is change successfully
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.