某用戶先註冊,註冊信息包括用戶名、密碼、個人格言。
然後使用註冊的信息登錄,如果登錄成功,則顯示個人信息;
否則顯示“登錄失敗”。
代碼示例:
package week5homeworks;
/**
*
* @author LYB
*用戶類
*用於定義姓名、密碼、手機號等的定義
*/
class User {
private String username;
private String password;
private String phonenum;
public User(String username, String password, String phonenum) {
super();
this.username = username;
this.password = password;
this.phonenum = phonenum;
}
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((username == null) ? 0 : username.hashCode());
result = prime * result + ((phonenum == null) ? 0 : phonenum.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
}
package week5homeworks;
/**
*
* @author LYB
* 邏輯方法判斷類
*用於判斷姓名、手機號、密碼驗證的三個方法
*/
public class Logic {
public Logic(){
}
public static boolean method1(String username){
if(username.matches("\\w{8}")){
System.out.println("恭喜,用戶名符合要求");
return true;
}else{
System.out.println("不符合要求,請重新輸入8位數字或字母組成的姓名!");
}
return false;
}
public static boolean method2(String phonenum){
if(phonenum.matches("1\\d{10}")){
System.out.println("恭喜,手機號碼符合要求");
}else{
System.out.println("不符合要求,請重新輸入以1開頭11位的數字!");
}
return false;
}
public static boolean method3(String password){
if(password.matches("\\w+{16}")){
System.out.println("恭喜,密碼符合要求");
}else{
System.out.println("不符合要求,請輸入由字母和數字組成的16位以內的字符!");
System.out.println("請重新輸入密碼:");
}
return false;
}
}
package week5homeworks;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author LYB
*學生信息管理登陸系統
*/
public class StudentManage {
List<User> userList = new ArrayList<>();
List<Logic> logicList = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
StudentManage smManage = new StudentManage();
while (true) {
System.out.println("--------請選擇操作--------\n\t1.用戶登錄\n\t2.用戶註冊\n\t3.退出");
System.out.println("-------------------------");
System.out.println("輸入:");
String opString = sc.next();
switch (opString) {
case "1":
smManage.login();
break;
case "2":
smManage.regist();
break;
case "3":
System.exit(0);
break;
default:
System.out.println("輸入錯誤");
break;
}
}
}
// user login
public void login() {
boolean flag1;
boolean flag2;
boolean flag3;
System.out.println("--------用戶登錄--------");
System.out.println("請輸入用戶名:");
String username = sc.next();
flag1 = Logic.method1(username);
// if(username.matches("\\w{8}")){
// System.out.println("恭喜,用戶名符合要求");
// }else{
// System.out.println("不符合要求,請重新輸入8位數字或字母組成的姓名!");
// System.out.println("請重新輸入用戶名:");
// String username1 = sc.next();
// }
System.out.println("請輸入註冊手機號:");
String phonenum = sc.next();
flag2 = Logic.method2(phonenum);
// if(phonenum.matches("1\\d{10}")){
// System.out.println("恭喜,手機號碼符合要求");
// }else{
// System.out.println("不符合要求,請重新輸入以1開頭11位的數字!");
// System.out.println("請重新輸入註冊手機號:");
// String phonenum1 = sc.next();
// }
System.out.println("請輸入密碼:");
String password = sc.next();
flag3 = Logic.method3(password);
// if(password.matches("\\w+{16}")){
// System.out.println("恭喜,密碼符合要求");
// }else{
// System.out.println("不符合要求,請輸入由字母和數字組成的16位以內的字符!");
// System.out.println("請重新輸入密碼:");
// String password1 = sc.next();
// }
if (flag1 && flag2 && flag3 &&userList.contains(new User(username, password,phonenum))) {
System.out.println("登錄成功");
} else {
System.out.println("用戶名、手機號或密碼錯誤,登錄失敗!");
}
}
// user regist
public void regist() {
System.out.println("--------用戶註冊--------");
boolean flag = true;
boolean flag4;
boolean flag5;
boolean flag6;
String username = "";
while (flag) {
System.out.println("請輸入用戶名:");
username = sc.next();
flag4 = Logic.method1(username);
// if(username.matches("\\w{8}")){
// System.out.println("恭喜,用戶名符合要求");
// }else{
// System.out.println("不符合要求,請重新輸入8位數字或字母組成的姓名!");
// System.out.println("請重新輸入用戶名:");
// String username1 = sc.next();
// }
if (userList.size() > 0) {
for (User user : userList) {
if (user.getUsername().equals(username)) {
flag = true;
System.out.println("Tips:用戶名已存在,請重新輸入!");
break;
} else {
flag = false;
}
}
}
else{
flag = false;
}
}
flag4 = Logic.method1(username);
System.out.println("請輸入手機號:");
String phonenum = sc.next();
flag5 = Logic.method2(phonenum);
// if(phonenum.matches("1\\d{10}")){
// System.out.println("恭喜,手機號碼符合要求");
// }else{
// System.out.println("不符合要求,請重新輸入以1開頭11位的數字!");
// System.out.println("請重新輸入手機號:");
// String phonenum1 = sc.next();
// }
System.out.println("請輸入密碼:");
String password = sc.next();
flag6 = Logic.method3(password);
// if(password.matches("\\w+{16}")){
// System.out.println("恭喜,密碼符合要求");
// }else{
// System.out.println("不符合要求,請輸入由字母和數字組成的16位以內的字符!");
// System.out.println("請重新輸入密碼:");
// String password1 = sc.next();
// }
if(flag4 && flag5 && flag6){
System.out.println("恭喜註冊成功!");
userList.add(new User(username, phonenum,password ));
}
}
}