/**
- 模拟登录
- 【4.3】
- 带封装的登录模拟
- 模拟用户登录(键盘输入 用户名和密码)
a、用户名长度在4-8之间 lucy(必须含有大小写字母和数字及下划线,不允许出现其他字符,否则提示错误信息)
b、密码的长度在8 -12 12345678(必须含有大小写字母和数字及下划线,不允许出现其他字符,否则提示错误信息)
c、如果 name=”John_12” password=”john_123456”, 提示登录成功!否则失败!~~~~
- @author xiangtao.p
*
- @Date 2020年10月31日
*/
【类1】 模拟登录方法类 Simular_Login02
public class Simular_Login02 {
private static final String userNameOri = "John_12";// 系统正确用户名
private static final String passwordOri = "john_123456";// 系统正确密码
private String userName;
private String password;
private String msg = "";
boolean islegalName;// 用户名是否合法
boolean islegalPwd;// 密码是否合法
public Simular_Login02() {
super();
}
public Simular_Login02(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
// 初始化判断是否大写字母,数字以及 _
boolean capital = false;
boolean smallLetter = false;
boolean isNum = false;
boolean is_ = false;
boolean illegal = false;// 判断是否非法字符
islegalName = false;
// userName=
if (!(userName.length() >= 4 && userName.length() <= 8)) {
msg = "用户名长度不符合要求,请输入4-8长度之间的用户名!";
// System.out.println(msg);
} else {
for (int i = 0; i < userName.length(); i++) {
char everyLetter = userName.charAt(i);
if (everyLetter >= 'A' && everyLetter <= 'Z') {
capital = true;
} else if (everyLetter >= 'a' && everyLetter <= 'z') {
smallLetter = true;
} else if (everyLetter >= '0' && everyLetter <= '9') {
isNum = true;
} else if (everyLetter == '_') {
is_ = true;
} else {
illegal = true;
break;
}
}
if (illegal) {
msg = "输入非法字符,用户名必须包含 大小写字母,数字以及下划线_; 请重新输入!";
// System.out.println(msg);
} else if (!is_ || !capital || !smallLetter || !isNum) { // 有一个不满足条件,就提示错误信息
msg = "输入的非法字符,用户名必须包含 大小写字母,数字以及下划线_ ,请重新输入!";
// System.out.println(msg);
} else {
islegalName = true;
this.userName = userName;
}
}
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
// 初始化判断是否大写字母,数字以及 _
boolean capital = false;
boolean smallLetter = false;
boolean isNum = false;
boolean is_ = false;
boolean illegal = false;// 判断是否非法字符
islegalPwd = false;
// 2、----------------------判断密码----------------------------
// 2.1初始化
// 初始化判断是否大写字母,数字以及 _
capital = false;
isNum = false;
is_ = false;
illegal = false;// 判断是否非法字符
if ((password.length() <= 8 || password.length() >= 12)) {
msg = "密码长度不符合要求,请输入8-12长度之间的密码!";
// System.out.println(msg);
} else {
for (int i = 0; i < password.length(); i++) {
char everyLetter = password.charAt(i);
if (everyLetter >= 'A' && everyLetter <= 'Z' || everyLetter >= 'a' && everyLetter <= 'z') {
capital = true;
} else if (everyLetter >= '0' && everyLetter <= '9') {
isNum = true;
} else if (everyLetter == '_') {
is_ = true;
} else {
illegal = true;
break;
}
}
if (illegal) {
msg = "输入非法字符,密码必须包含 字母,数字以及下划线_; 请重新输入!";
// System.out.println(msg);
} else if (!is_ || !capital || !isNum) { // 有一个不满足条件,就提示错误信息
msg = "输入的非法字符,密码必须包含 字母,数字以及下划线_ ,请重新输入!";
// System.out.println(msg);
} else {
islegalPwd = true;
this.password = password;
}
}
}
/**
* @return the msg
*/
public String getMsg() {
return msg;
}
/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this.msg = msg;
}
// ------判断登录信息入口-------
public void login() {
this.setlogin(userName, password);
}
// ----------登录信息判断方法----------
public void setlogin(String userName, String password) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请输入用户名:");
userName = scan.next();
System.out.println("请输入密码:");
password = scan.next();
// 調用setUserName方法判斷用戶名是否合法
this.setUserName(userName);
if (islegalName) {
// 調用setPassword方法判斷密碼是否合法
this.setPassword(password);
if (islegalPwd) {
// 用戶名和密碼均合法,再判斷用戶名和密碼是否正確
// 3.-----------------------------验证用户名和密码是否正确[start]--------------------------------
if (userName.equals(userNameOri) && password.equals(passwordOri)) {
msg = "登录成功,欢迎使用!";
System.out.println(msg);
break;
} else {
msg = "用户名或者密码错误,请重新输入!";
System.out.println(msg);
continue;
}
// ------------------------------验证用户名和密码是否正确[end]--------------------------------
} else {
System.out.println(msg);// 输出密码不合法信息
}
} else {
System.out.println(msg);// 输出用户名不合法信息
}
}
}
}
下面是main测试类的
【类2】 测试 Simular_Login02这个类
public class Teat_SimularLogin {
/**
* @param args
*/
public static void main(String[] args) {
String userName = "";
String password = "";
//2、封装的方法
Simular_Login02 simular_Login02 = new Simular_Login02(userName, password);
simular_Login02.login();
}
}
本文含有隐藏内容,请 开通VIP 后查看