学生管理系统升级版实现管理员新增注册、登录、忘记密码功能
学生的信息的增删改查也有,欢迎提问
package file;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class stuGTest {
public static void main(String[] args) {
ArrayList<stuG> list=new ArrayList<>();
Scanner sc=new Scanner(System.in);
while (true) {
System.out.println("欢迎来到学生管理界面");
System.out.println("请选择操作1.登录 2.注册 3.忘记密码");
String cin=sc.next();
switch(cin){
case"1"-> denglu(list);
case"2"-> zhuce(list);
case"3"-> wangji(list);
case "4"->{
System.out.println("谢谢使用");
System.exit(0);
}
default -> System.out.println("输入错误,没有这个选项");
}
}
}
private static void wangji(ArrayList<stuG> list) {
//1,键盘录入用户名,判断当前用户名是否存在,如不存在,直接结束方法,并提示:未注册
//
//2,键盘录入身份证号码和手机号码
//
//3,判断当前用户的身份证号码和手机号码是否一致,
//
//如果一致,则提示输入密码,进行修改。
//
// 如果不一致,则提示:账号信息不匹配,修改失败。
while (true) {
Scanner sc=new Scanner("请输入忘记的账号:");
String name = sc.next();
boolean flag=comparename(list,name);
if(!flag){
System.out.println("该账户未注册");
break;
}
else {
System.out.println("请输入身份证号码:");
String id = sc.next();
boolean compareid=compareid(list,id);
if(compareid)
{
System.out.println("请输入手机号:");
String phone = sc.next();
boolean ph=com(list,phone);
if(ph){
System.out.println("请输入修改的密码:");
String newpassword = sc.next();
stuG s=new stuG();
s.setPassword(newpassword);
list.add(s);
System.out.println("修改成功");
}
else{
System.out.println("修改失败");
}
}
System.out.println("身份证错误");
}
}
}
private static boolean com(ArrayList<stuG> list, String phone) {
for (int i = 0; i < list.size(); i++) {
stuG g = list.get(i);
String phone1 = g.getPhone();
if(g.equals(phone1))
{
return true;
}
}
return false;
}
private static boolean compareid(ArrayList<stuG> list,String id) {
for (int i = 0; i < list.size(); i++) {
stuG g = list.get(i);
String idcard = g.getIdcard();
if(id.equals(idcard))
{
return true;
}
}
return false;
}
private static void zhuce(ArrayList<stuG> list) {
Scanner sc=new Scanner(System.in);
String name;
String password;
String id;
String iphone;
while (true) {
System.out.println("请输入用户名:");
name= sc.next();
boolean flag=checkname(name);
if(flag){
boolean flag1=contain(list,name);
if(flag1){
System.out.println("格式正确");
break;
}
}
else {
System.out.println("格式不正确,请重新输入");
}
}
while (true) {
System.out.println("请输入密码:");
password = sc.next();
System.out.println("请再次输入密码:");
String newpassword = sc.next();
if((password.equals(newpassword)))
{
System.out.println("两次密码输入正确");
break;
}
else {
System.out.println("两次密码输入不一样");
}
}
while (true) {
System.out.println("请输入身份证号码:");
id = sc.next();
boolean flag=checkid(id);
if(flag){
System.out.println("身份证输入正确");
break;
}
else {
System.out.println("身份证格式不正确");
}
}
while (true) {
System.out.println("请输入手机号:");
iphone = sc.next();
boolean flag=checkiphone(iphone);
if(flag){
System.out.println("输入的手机号正确");
break;
}
else {
System.out.println("手机号码格式错误");
}
}
stuG stu=new stuG(name,password,id,iphone);
list.add(stu);
System.out.println("用户注册成功");
}
private static boolean checkiphone(String iphone) {
int len = iphone.length();
char c = iphone.charAt(0);
if(len!=11&&c=='0')
return false;
for (int i = 0; i < iphone.length(); i++) {
char c1 = iphone.charAt(i);
if(!(c1>='0'&&c1<='9'))
return false;
}
return true;
}
private static boolean checkid(String id) {
char ending = id.charAt(id.length() - 1);
if((ending>='0'&&ending<='9')||ending=='x'||ending=='X')
{
return true;
}
if((!(id.length()==18))||(id.charAt(0)=='0'))
return false;
for (int i = 0; i < id.length()-1; i++) {
char c = id.charAt(i);
if(!(c>='0'&&c<='9')){
return false;
}
}
return true;
}
private static boolean contain(ArrayList<stuG> list,String name) {
for (int i = 0; i < list.size(); i++) {
if(name.equals(list))
{
return false;
}
}
return true;
}
private static boolean checkname(String name) {
int count=0;
int len=name.length();
if(len<3||len>15){
return false;
}
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9')))
{
return false;
}
}
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
count++;
break;
}
}
return count>0;
}
private static void denglu(ArrayList<stuG> list) {
Scanner sc=new Scanner(System.in);
int time=3;
while (time>0) {
System.out.println("请输入用户名:");
String inputname = sc.next();
boolean name=comparename(list,inputname);
if(name){
System.out.println("请输入密码:");
break;
}
else {
System.out.println("该账户没有注册,请先注册在使用");
time--;
break;
}
}
while (time>0) {
String inputpassword = sc.next();
boolean password=comparepassword(list,inputpassword);
if(password){
System.out.println("请输入验证码:");
break;
}
else {
System.out.println("密码错误,请重新输入");
time--;
}
}
String generatedCode = yzm();
while (true) {
String userCode = sc.next();
if (userCode.equals(generatedCode)) {
System.out.println("验证码正确");
System.out.println("登陆成功,欢迎进入学生管理系统");
ArrayList<student> list1=new ArrayList<>();
loop: while (true){
System.out.println("------------欢迎来到学生管理界面------------");
System.out.println("1:添加学生");
System.out.println("2:删除学生");
System.out.println("3:修改学生");
System.out.println("4:查询学生");
System.out.println("5:退出");
System.out.println("请输入你的选择:");
Scanner sc1=new Scanner(System.in);
String choose=sc.next();
switch(choose){
case "1"-> add(list1);
case "2"-> delete(list1);
case "3"-> update(list1);
case "4"-> query(list1);
case "5"-> {
System.out.println("退出");
// break loop;//设置标号
System.exit(0);
}
default -> System.out.println("没有这个选项");
}
break;
}
}
else {
System.out.println("验证码错误");
}
}
}
private static String yzm() {
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuilder yz = new StringBuilder();
Random re = new Random();
for (int i = 0; i < 5; i++) {
int index = re.nextInt(characters.length());
yz.append(characters.charAt(index));
}
System.out.println("验证码:" + yz.toString()); // 打印验证码
return yz.toString();
}
private static boolean comparepassword(ArrayList<stuG> list,String inputpassword) {
for (int i = 0; i < list.size(); i++) {
stuG g = list.get(i);
if(g.getPassword().equals(inputpassword))
return true;
}
return false;
}
private static boolean comparename(ArrayList<stuG> list,String inputname) {
for (int i = 0; i < list.size(); i++) {
stuG g = list.get(i);
if(g.getUsername().equals(inputname))
return true;
}
return false;
}
public static void add(ArrayList<student> list1){
Scanner sc=new Scanner(System.in);
student stu = new student();
String id = null;
while (true) {
System.out.println("请输入学生id:");
id = sc.next();
boolean flag=contains(list1,id);
if(flag){
stu.setId(id);
break;
}else System.out.println("id不能重复");
}
System.out.println("请输入学生的姓名:");
String name=sc.next();
stu.setName(name);
System.out.println("请输入学生的年龄:");
int age=sc.nextInt();
stu.setAge(age);
System.out.println("请输入学生的家庭住址:");
String address=sc.next();
stu.setAddress(address);
list1.add(stu);
System.out.println("添加成功");
}
public static void delete(ArrayList<student> list1){
Scanner sc=new Scanner(System.in);
System.out.println("请输入删除的学生id:");
String id=sc.next();
int number=index(list1,id);
if(number>=0){
list1.remove(number);
System.out.println("学生id为"+number+"删除成功");
}
else {
System.out.println("id不存在,删除失败");
}
}
public static void update(ArrayList<student> list1){
Scanner sc=new Scanner(System.in);
System.out.println("输入修改的学生id:");
String id = sc.next();
int index = index(list1, id);
if(index<0){
System.out.println("不存在该学生信息,请重新输入:");
return;
}
else{
student stu=list1.get(index);
System.out.println("请输入修改后的学生姓名:");
String name=sc.next();
System.out.println("请输入修改后的学生年龄:");
int age=sc.nextInt();
System.out.println("请输入修改后的学生家庭住址:");
String adress=sc.next();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setAddress(adress);
}
}
public static void query(ArrayList<student> list1){
if(list1.size()==0){
System.out.println("当前没有学生信息,请添加后在查询");
return;
}
System.out.println("id"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"家庭住址");
for (int i = 0; i < list1.size(); i++) {
student student = list1.get(i);
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAddress());
}
}
public static boolean contains(ArrayList<student> list1,String id){
// for (int i = 0; i < list.size(); i++) {
// student stu=list.get(i);
// if(stu.getId().equals(id))
// {
// return false;
// }
// }
// return true;
return index(list1,id)<0;
}
public static int index(ArrayList<student> list1,String id){
for (int i = 0; i < list1.size(); i++) {
student stu = list1.get(i);
if(stu.getId().equals(id))
{
return i;
}
}
return -1;
}
}