package com.itheima.studentsysytem;
public class Student {
//id name age address
private String id;
private String name;
private int age;
private String address;
public String getId() {
return id;
}
public void student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.itheima.studentsysytem;
public class User {
private String username;
private String password;
private String personID;
private String phoneNum;
public User(){
}
public User(String username, String password, String personID, String phoneNum) {
this.username = username;//用户名
this.password = password;
this.personID = personID;
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;
}
public String getPersonID() {
return personID;
}
public void setPersonID(String personID) {
this.personID = personID;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
}
package com.itheima.studentsysytem;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
ArrayList<User> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎来到学生系统");
System.out.println("请选择操作: 1登陆 2注册 3忘记密码");
String choose = sc.next();
switch (choose){
case "1": login(list);break;
case "2": rigister(list);break;
case "3": forgetPassword(list);break;
case "4":
System.out.println("谢谢使用,再见");
System.exit(0);
default:
System.out.println("没有这个选项");
}
}
}
//忘记密码
private static void forgetPassword(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.next();
//判断用户是否存在
boolean flag = contain(list, username);
if(!flag){
System.out.println("用户名" + username + "未注册,请先注册再登入");
return;
}
//键盘录入
System.out.println("请输入身份证号码");
String personID = sc.next();
System.out.println("请输入手机号码");
String phoneNumber = sc.next();
//比较用户对象中的手机号码和身份证号码是否相同
int index = findIndex(list,username);
User user = list.get(index);
if(!(user.getPersonID().equals(personID) && user.getPhoneNum().equals(phoneNumber))) {
System.out.println("身份真号码或手机号号码输入有误,不能修改密码");
return;
}
while(true) {
System.out.println("请输入新的密码");
String password = sc.next();
System.out.println("请再输入新的密码");
String againPassword = sc.next();
if(password.equals(againPassword)){
System.out.println("两次密码一致");
break;
}
else{
System.out.println("两次输入密码不一致,请重新输入");
continue;
}
}
}
//是否有该用户名
private static int findIndex(ArrayList<User> list, String username) {
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if(user.getUsername().equals(username)){
return i;
}
}
return -1;
}
//注册
private static void rigister(ArrayList<User> list) {
// User u =
//用户名,密码,身份证号码,手机号码
//把用户对象添加到集合中
//1.键盘录入用户名
String username;
String password;
String personID;
String phoneNumber;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入用户名");
username = sc.next();
//先验证格式是否正确,再验证是否唯一
//因为在所有的数据都是在数据库中,如果我们要校验,需要使用到网络资源。
//用户名唯一
//用户名长度必须在3~15位之间
//只能是字母加数字的组合,但是不能是纯数字
boolean flag1 = checkUsername(username);
if(!flag1) {
System.out.println("用户名格式不满足条件,需要重新输入");
continue;
}
//用户名唯一
boolean flag2 = contain(list, username);
if(flag2){
System.out.println("用户名" + username + "已经存在,请重新输入");
}
else{
System.out.println("用户名" + username + "可用");
break;
}
}
//2.键盘录入密码
while (true) {
System.out.println("请输入要注册的密码");
password = sc.next();
System.out.println("请再次输入要注册的密码");
String againPassword = sc.next();
if(password.equals(againPassword)) {
System.out.println("两次密码一致,继续录入其他数据");
break;
}
else{
System.out.println("两次输入密码不一致,请重新输入");
continue;
}
}
//键盘录入身份证号码
while (true) {
System.out.println("请输入身份证号码");
personID = sc.next();
boolean flag = checkPersonID(personID);
if(flag){
System.out.println("身份证号码满足要求");
break;
}
else{
System.out.println("身份站号码输入有误,请重新输入");
}
}
//键盘入录手机号码
while (true) {
System.out.println("请输入手机号码");
phoneNumber = sc.next();
boolean flag = checkPhoneNumber(phoneNumber);
if(flag){
System.out.println("手机号码格式正确");
break;
}
else{
System.out.println("手机号码格式有误,请重新输入");
}
}
//把用户对象添加到集合当中
User u = new User(username,password,personID,phoneNumber);
list.add(u);
System.out.println("注册成功");
}
//是否有该电话号码
private static boolean checkPhoneNumber(String phoneNumber) {
//长度为11
if(phoneNumber.length() != 11){
return false;
}
//不能以0开头
if(phoneNumber.charAt(0) == 0){
return false;
}
//必须都是数字
for (int i = 0; i < phoneNumber.length(); i++) {
char c = phoneNumber.charAt(i);
if(!(c <= '9' && c >= '0')){
return false;
}
}
return true;
}
//是否有该学号
private static boolean checkPersonID(String personID) {
//长度18位
if(personID.length() != 18) {
return false;
}
//不能以0开头
// if(personID.charAt(0) == 0){
// return false;
// }
// boolean flag = personID.startsWith("0");
if (personID.startsWith("0")){
return false;
}
// 前17位必须是数字
for (int i = 0; i < personID.length() - 1; i++) {
char c = personID.charAt(i);
if(!(c >= '0' && c <= '9')) {
return false;
}
}
//最后一位可以是大写X也可以是小写下;
char endChar = personID.charAt(personID.length() - 1);
if ((endChar == 'X' || endChar == 'x') ||(endChar <= '9' && endChar >= '0')) {
return true;
}
else {
return false;
}
}
//用户名是否重复
private static boolean contain(ArrayList<User> list, String username) {
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
String rightUsername = user.getUsername();
if(rightUsername.equals(username)) {
return true;
}
}
return false;
}
private static boolean checkUsername(String username) {
int len = username.length();
//用户名长度必须在3~15位之间
if(len < 3 || len > 15) {
return false;
}
//只能是字母加数字的组合,但是不能是纯数字
for (int i = 0; i < username.length(); i++) {
char c = username.charAt(i);
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
return false;
}
}
int count = 0;
for (int i = 0; i < username.length(); i++) {
char c = username.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ) {
count++;
break;
}
}
return count > 0;
}
//登入
private static void login(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名");
String username = sc.next();
;
//判断用户是否存在
boolean flag = contain(list, username);
if(!flag){
System.out.println("用户名" + username + "未注册,请先注册再登入");
return;
}
System.out.println("请输入密码");
String password = sc.next();
while (true) {
String rightCode = getCode();
System.out.println("当前正确的验证码为" + rightCode);
System.out.println("请输入验证码");
String code = sc.next();
if(code.equalsIgnoreCase(rightCode)) {
break;
}
else{
System.out.println("验证码错误");
continue;
}
}
//可以把一些零散的数据,封装成一个对象,传递一个整体
User useInfo = new User(username,password,null,null);
boolean result = checkUserInfo(list,useInfo);
if(result){
System.out.println("登入成功,可以开始使用学生管理系统");
StudentSystem ss = new StudentSystem();
ss.startStudentSystem();
break;
}
else{
//System.out.println("登入失败,用户名或密码错误");
if(i == 2){
System.out.println("当前账号" + username + "被锁定");
return;
}
System.out.println("登入失败,用户名或密码错误, 还剩下" + (2 - i) + "次机会");
}
}
}
private static boolean checkUserInfo(ArrayList<User> list, User useInfo) {
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if (user.getUsername().equals(useInfo.getUsername()) && user.getPassword().equals(useInfo.getPassword())){
return true;
}
}
return false;
}
//生成一个验证码
private static String getCode(){
//长度为5 由4位大写字母或小写字母和一位数字组成,同一个字母可重复 数字可以出现在任意一个位置
//1.创建一个集合添加所有的大写字母和小写字母
ArrayList<Character> list = new ArrayList<>();
for (int i = 0; i < 26; i++) {
list.add((char)('a' + i));
list.add((char)('A' + i));
}
StringBuilder sb = new StringBuilder();
//2.要随机抽取4个字符
Random r = new Random();
for (int i = 0; i < 4; i++) {
//获取随机索引
int index = r.nextInt(list.size());
//利用随机索引获取字符
char c = list.get(index);
//把随机字符添加到sb当中
sb.append(c);
}
//3.把一个随机数字添加到末尾
int number = r.nextInt(10);
sb.append(number);
//4.如果我们要修改字符串中的内容
//先把字符串变成字符数组,在数组中修改,然后再创建一个新的数组
char[] arr = sb.toString().toCharArray();
//拿着最后一个索引,跟随机索引进行交换
int randomIndex = r.nextInt(arr.length);
//最大索引指向的元素,跟随机索引指向的元素交换
char temp = arr[randomIndex];
arr[randomIndex] = arr[arr.length - 1];
arr[arr.length - 1] = temp;
return new String(arr);
}
}