一些注意点:
1.唯一性的判断;
2.验证码的生成和验证,验证码规则(只能有一个数字,且位置随机);
3.用户名和密码输入有三次机会;
package com.heima.studentMS;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class userTest {
public static void main(String[] args) {
menu();
ArrayList<User> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while(true){
int select = sc.nextInt();
if (select == 1){
//登录
userLog(list);
menu();
}
if (select == 2){
//注册
userRegister(list);
menu();
}
if (select == 3){
//忘记密码
forgetPassword(list);
menu();
}
if (select == 4){
break;
}
}
}
//主界面
public static void menu(){
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作1登录 2注册 3忘记密码 4退出");
}
//用户登录
public static void userLog(ArrayList<User> list){
Scanner sc = new Scanner(System.in);
User u = new User();
System.out.println("请输入用户名:");
u.setName(sc.next());
System.out.println("请输入密码:");
u.setPassword(sc.next());
//判断输入验证码是否正确
boolean flag = codeJudge();
if (flag){
//验证码输入正确,判断用户名密码是否正确
userJudge(u,list);
}
}
//输入验证码判断
public static boolean codeJudge(){
Scanner sc = new Scanner(System.in);
//随机生成验证码
String verificationCode = getVerificationCode();
System.out.println("验证码:" + verificationCode);
System.out.println("请输入验证码:");
while (true){
String vCode = sc.next();
if (!verificationCode.equals(vCode)){
System.out.println("验证码错误,请重新输入");
}else {
System.out.println("验证码正确!");
break;
}
}
return true;
}
//随机生成验证码
public static String getVerificationCode(){
String[] arr1 = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r"
,"s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L"
,"M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
String[] arr2 = {"0","1","2","3","4","5","6","7"
,"8","9"};
Random r = new Random();
String result = "";
for (int i = 0; i < 4; i++){
int index = r.nextInt(arr1.length - 1);
result = result + arr1[index];
}
int index = r.nextInt(arr2.length - 1);
result = result + arr2[index];
//字符串变成数组后打乱
String fResult = toChar(result);
return fResult;
}
//字符串变成数组后打乱
public static String toChar(String str){
char[] arr = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
arr[i] = str.charAt(i);
}
Random r = new Random();
for (int i = 0; i < arr.length; i++) {
int index = r.nextInt(arr.length);
char c = arr[i];
arr[i] = arr[index];
arr[index] = c;
}
String result = "";
for (int i = 0; i < arr.length; i++) {
result = result + arr[i];
}
return result;
}
//判断用户名密码是否正确
public static void userJudge(User u,ArrayList<User> list){
Scanner sc = new Scanner(System.in);
int count = 3;//有三次判断机会
while(count != 0){
boolean flag = judgeActual(u,list);
if (flag){
break;
}else{
count--;
System.out.println("还剩余" + count + "次机会!");
System.out.println("请输入用户名:");
u.setName(sc.next());
System.out.println("请输入密码:");
u.setPassword(sc.next());
}
}
if (count == 0){
System.out.println("输入错误!三次机会用完!");
}
}
//判断
public static boolean judgeActual(User u,ArrayList<User> list){
for ( int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(u.getName()) && list.get(i).getPassword().equals(u.getPassword())){
System.out.println("登录成功!");
return true;
}
}
return false;
}
//用户注册
public static void userRegister(ArrayList<User> list){
Scanner sc = new Scanner(System.in);
User u = new User();
boolean flag1,flag2,flag3,flag4;
while (true) {
System.out.println("请输入用户名:");
String name = sc.next();
//并判断用户名是否合法
flag1 = inputName(list,name);
if (flag1){
u.setName(name);
break;
}
}
//密码
while (true){
System.out.println("请输入密码:");
String p1 = sc.next();
flag2 = passwordJudge(p1);
if (flag2){
u.setPassword(p1);
break;
}
}
while (true){
System.out.println("请输入身份证号:");
String id = sc.next();
flag3 = idJudge(id);
if (flag3){
u.setIdCard(id);
break;
}
}
while (true){
System.out.println("请输入手机号:");
String phone = sc.next();
flag4 = phoneJudge(phone);
if (flag4){
u.setPhoneNumber(phone);
break;
}
}
//全部合法
if (flag1 && flag2 && flag3 && flag4){
list.add(u);
System.out.println("注册成功!");
}else{
System.out.println("注册失败!");
}
}
//判断用户名是否合法
public static boolean inputName(ArrayList<User> list,String name){
//唯一性、长度、是否全是数字
boolean flag1 = uniqueness(list,name);
boolean flag2 = Length(name);
boolean flag3 = nameNumber(name);
if (flag1 && flag2 && flag3){
//全部合法--->用户名合法
return true;
}
else{
return false;
}
}
//唯一性
public static boolean uniqueness(ArrayList<User> list,String name){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(name)){
//用户名存在
System.out.println("用户名存在!");
return false;
}
}
return true;//用户名不存在,合法
}
//长度合法性
public static boolean Length(String name){
if (name.length() >= 3 && name.length() <=15){
return true;//长度合法
}else{
System.out.println("用户名长度不在范围内!");
return false;
}
}
//用户名是否全是数字
public static boolean nameNumber(String name){
int count = 0;//计数
for (int i = 0; i < name.length(); i++) {
if (name.charAt(i) >= '0' && name.charAt(i) <= '9'){
count++;
}
}
if (count == name.length()){
System.out.println("用户名全是数字");
return false;//全是数字
}else{
return true;//合法
}
}
//密码确认
public static boolean passwordJudge(String p1){
Scanner sc = new Scanner(System.in);
while (true){
System.out.println("请确认密码:");
String p2 = sc.next();
if (p2.equals(p1)){
System.out.println("密码设置成功");
return true;
}
else{
System.out.println("密码不一致,请重新输入!");
}
}
}
//身份证号合法性判断
public static boolean idJudge(String id){
//长度判断
boolean flag1 = idLength(id);
//开头判断
boolean flag2 = start(id);
//前17位必须为数字
boolean flag3 = numberAll(id);
//最后一位可以是数字,也可以是大写X或小写x
boolean flag4 = end(id);
if (flag1 && flag2 && flag3 && flag4){
return true;
}else{
return false;
}
}
//长度
public static boolean idLength(String id){
if (id.length() == 18){
return true;
}else{
System.out.println("身份证号长度不是18!");
return false;
}
}
//开头判断
public static boolean start(String id){
if (id.charAt(0) == '0'){
System.out.println("开头为0!");
return false;
}
else {
return true;
}
}
//前17位必须为数字
public static boolean numberAll(String id){
int count = 0;//计数
for (int i = 0; i < id.length() - 1; i++) {
if (id.charAt(i) >= '0' && id.charAt(i) <= '9'){
count++;
}
}
if (count == id.length() - 1){
return true;//前id.length() - 1位全为为数字
}
else {
System.out.println("不全是数字!");
return false;
}
}
//最后一位可以是数字,也可以是大写X或小写x
public static boolean end(String id){
char c = id.charAt(id.length() - 1);
if ((c >= '0' && c <= '9') || c == 'x' || c == 'X'){
return true;
}else{
System.out.println("身份证号最后一位不合法!");
return false;
}
}
//手机号判断
public static boolean phoneJudge(String phone){
//长度11位
boolean flag1 = phoneLength(phone);
//不能以0为开头
boolean flag2 = start(phone);
//必须都是数字
boolean flag3 = numberAll(phone);
if (flag1 && flag2 && flag3){
return true;
}else {
return false;
}
}
//长度
public static boolean phoneLength(String phone){
if (phone.length() == 11){
return true;//长度为11
}else {
System.out.println("手机号长度不是11!");
return false;
}
}
//忘记密码
public static void forgetPassword(ArrayList<User> list){
//键盘录入用户名,判断当前用户名是否存在,如不存在,直接结束方法,并提示:未注册
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.next();
//判断用户是否存在
boolean flag = judgeContains(name,list);
if (!flag){
System.out.println("未注册!");
}else{//键盘录入身份证号码和手机号码,判断当前用户的身份证号码和手机号码是否一致,
judgeIdPhone(list);
}
}
//判断用户是否存在
public static boolean judgeContains(String name,ArrayList<User> list){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().equals(name)){
//存在
return true;
}
}
return false;
}
//键盘录入身份证号码和手机号码,判断当前用户的身份证号码和手机号码是否一致,
public static void judgeIdPhone(ArrayList<User> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入身份证号:");
String id = sc.next();
System.out.println("请输入手机号:");
String phone = sc.next();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getIdCard().equals(id) && list.get(i).getPhoneNumber().equals(phone)){
System.out.println("信息正确,请进行密码修改!");
changePassword(list,id);
}else {
System.out.println("账号信息不匹配,修改失败!");
}
}
}
//密码修改
public static void changePassword(ArrayList<User> list,String id){
Scanner sc = new Scanner(System.in);
for (int i = 0; i < list.size(); i++) {
String userID = list.get(i).getIdCard();
if (userID.equals(id)){
System.out.println("请输入新密码:");
String newPassword = sc.next();
//密码确认
boolean flag = passwordJudge(newPassword);
if (flag){
list.get(i).setPassword(newPassword);
System.out.println("密码修改成功!");
}
break;
}
}
}
}