共计 3972 个字符,预计需要花费 10 分钟才能阅读完成。
/**
- 模仿登录
- 【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();}
}
正文完