用集合写的学生管理系统,学习java写的练习
Login为首页面,main方法从这里开始
Start主菜单
Student学生类
User用户类
Login类如下:
package com.liang.studentGuanli;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Login {
public static void main(String[] args) {
ArrayList<User> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (true) {
//主页面
System.out.print("""
--------------------------------------------------------------------
欢迎来到学生管理系统
请选择操作1登录 2注册 3忘记密码:
--------------------------------------------------------------------
""");
String num = sc.next();
//选择项功能
switch (num) {
case "1" -> {
//登录
String s = InsertVerify();
LonginUser(list,s);
}
case "2" -> {
//注册
insertUser(list);
}
case "3" -> {
//忘记密码
OblivionPassword(list);
}
}
}
}
//注册功能
public static void insertUser(ArrayList<User> list){
Scanner sc = new Scanner(System.in);
User user = new User();
System.out.print("请设置用户名(用户名长度必须在3~15位之间,只能是字母加数字的组合,但是不能是纯数字):");
String username=sc.next();
int flag=0;
for (int i = 0; i < list.size(); i++) {
User user1 = list.get(i);
if(user1.getUsername().equals(username)){//判断用户名是否存在
System.out.println("用户名已存在!!");
flag=1;
break;
}
}
boolean b = UsernameVerify(username);
if(b&&flag==0){
user.setUsername(username);
}else{
System.out.println("用户名不符合要求");
return;
}
System.out.print("请设置密码:");
String password=sc.next();
System.out.print("请再次输入密码:");
String password1=sc.next();
boolean b1 = passwordVerify(password, password1);
if(b1) {
user.setPassword(password);
} else {
System.out.println("输入的密码两次不一致");
return;
}
System.out.println("请输入身份证号(长度为18位,不能以零开头,前十七位必须数字,最后一位可以是数字也可以是大写X或x):");
String identify=sc.next();
if (identifyVerify(identify)) {
user.setIdentifyNumber(identify);
}else {
System.out.println("身份证号不符合格式");
return;
}
System.out.println("请输入手机号码:");
String telephone=sc.next();
if (telephoneVerify(telephone)) {
user.setTelephone(telephone);
}else {
System.out.println("输入手机号码的不正确");
return;
}
list.add(user);
System.out.println("注册成功");
}
//登录功能
public static void LonginUser(ArrayList<User> list,String s){
Scanner sc = new Scanner(System.in);
for (int k=0;k<3;k++) {
System.out.println("请输入用户名:");
String username = sc.next();
int flag=0;
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if (user.getUsername().equals(username)) {
flag = 1;
break;
}
}
if(flag==0){
System.out.println("用户名未注册,请先注册");
return;
}
System.out.println("请输入密码:");
String password = sc.next();
System.out.println("验证码为:"+s);
System.out.println("请输入验证码:");
String verify = sc.next();
while (!verify.equals(s)){//循环判断验证码是否输入正确
System.out.println("验证码错误,请重新输入:");
verify = sc.next();
}
if(VerifyUser(username,password,list)){//密码和用户名正确进入主菜单
Start start = new Start();
break;
}else {
System.out.println("用户名或密码错误,你还有"+(2-k)+"次机会");
}
}
}
//忘记密码
public static void OblivionPassword(ArrayList<User> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.next();
int flag=0;
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if(user.getUsername().equals(username)){//判断用户名是否已注册
flag=1;
}
}
if(flag==0) {
System.out.println("该用户名未注册");
return;
}
System.out.println("请输入身份证:");
String identify = sc.next();
System.out.println("请输入电话号:");
String telephone = sc.next();
int t_flag=0;
//判断身份证和电话号是否匹配
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if(user.getIdentifyNumber().equals(identify)&&user.getTelephone().equals(telephone)){
t_flag=1;
}
}
//匹配则修改密码
if(t_flag==1){
System.out.println("请输入密码:");
String password = sc.next();
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if(user.getUsername().equals(username)){
list.set(i,new User(password));
System.out.println("密码修改成功");
break;
}
}
}else {
System.out.println("账号信息不匹配,修改失败");
}
}
//生成验证码
public static String InsertVerify(){
Random random = new Random();
String English="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTUVWXYZ";//记录大小写字母
String num="0123456789";//记录数字
char[] charArray1 = num.toCharArray();//转换为字符数组·
char[] charArray = English.toCharArray();
StringBuilder sb = new StringBuilder();//StringBuilder对象
for (int i = 0; i < 4; i++) {//首先添加随机生成的字母
int n = random.nextInt(charArray.length);
sb.append(charArray[n]);
}
String s = sb.toString();
char[] arr=new char[5];//记录验证码
int m = random.nextInt(5);//生成数字在验证码中的位置下标
int k=0;//用来记录 s 字符串的下标
for (int i = 0; i < 5; i++) {//假设 m 是中间值把小于它的放前面大于它的放后面,下标
if(m==i){//相等就直接放进数组中
arr[i]=charArray1[random.nextInt(10)];
}else if(m>i){//把小于 m 的下标字母放入数组中
arr[i]=s.charAt(k);
}else {//把大于 m 的下标字母放入数组中
arr[i]=s.charAt(k);
}
if(k==i){//防止s 字符串下标溢出异常
k=k-1;
}
k++;
}
return new String(arr);
}
//验证用户名是否符号要求
public static boolean UsernameVerify(String username){
if(username.isEmpty()){
return false;
}
int numFlag=0;
int t_flag=0;
if(username.length()>=3&&username.length()<=15){//设置指定范围
for (int i = 0; i < username.length(); i++) {
if(username.charAt(i)>='0'&&username.charAt(i)<='9'){//存在数字
numFlag=1;
}
if(username.charAt(i)>='A'&&username.charAt(i)<='z'){//存在字母
t_flag=1;
}
}
if(numFlag==1&&t_flag==1){//同时满足才行,只能是字母和数字的组合
return true;
}else {
// System.out.println("您设置的用户名不符合");
return false;
}
}else {
// System.out.println("用户名不符合要求");
return false;
}
}
//验证密码是否符合要求
public static boolean passwordVerify(String password,String password1){
if(password.isEmpty()||password1.isEmpty()){
return false;
}
if(password.equals(password1)){
return true;
}else {
return false;
}
}
//验证身份证号是否符合要求
public static boolean identifyVerify(String identify){
if(identify.length()==18&&identify.charAt(0)!='0'){//身份证号必须是18位,首位不能为零
int flag=0;
int flag1=0;
for (int i = 0; i < identify.length()-1; i++) {
if(identify.charAt(i)<'0'&&identify.charAt(i)>'9'){//前面十七位必须是数字
flag=1;
}
}
//十八为只能是数字或X或x
if(identify.charAt(identify.length()-1)>='0'&&identify.charAt(identify.length()-1)<='9'||identify.charAt(identify.length()-1)=='x'||identify.charAt(identify.length()-1)=='X'){
flag1=1;
}
if(flag==0&&flag1==1){
return true;
}else {
return false;
}
}else {
return false;
}
}
//验证电话号码是否符合
public static boolean telephoneVerify(String telephone){
if(telephone.length()==11&&telephone.charAt(0)!='0'){
int flag=0;
for (int i = 0; i < telephone.length(); i++) {
if (telephone.charAt(i)<'0'||telephone.charAt(i)>'9'){
flag=1;
}
}
if(flag==0){
return true;
}else {
return false;
}
}else {
return false;
}
}
//验证用户名和密码是否正确
public static boolean VerifyUser(String username,String password,ArrayList<User> list){
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
return true;
}
}
return false;
}
}
Start类如下:
package com.liang.studentGuanli;
import java.util.ArrayList;
import java.util.Scanner;
public class Start {
public Start(){
Scanner sc = new Scanner(System.in);
ArrayList<Student> list = new ArrayList<>();
loop:while(true){
System.out.print("""
-----------------------欢迎来到学生管理系统------------------------
1:添加学生
2:删除学生
3:修改学生
4:查询学生
5:退出
请输入您的选择:
""");
// int num=sc.nextInt();
String num=sc.next();//提高容错率,避免用户输入其他字符导致程序报错
switch(num){
case "1":addStudent(list);break;
case "2":StudentRemove(list);break;
case "3":SetStudent(list);break;
case "4":FindAllStudent(list);break;
case "5":break loop;
default:
System.out.println("未找到你要执行的业务");
}
// if(num==5){
// break;
// }
}
}
//添加学生信息
public static void addStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生id:");
int id=sc.nextInt();
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
if(s.getId()==id){
System.out.println("id已存在,请重新输入id:");
id=sc.nextInt();
i=0;
}
}
System.out.println("请输入学生姓名:");
String name=sc.next();
System.out.println("请输入学生年龄:");
int age=sc.nextInt();
System.out.println("请输入学生家庭住址:");
String address=sc.next();
Student student = new Student(id,name,age,address);
list.add(student);
System.out.println("学生信息录入成功");
}
//删除学生信息
public static void StudentRemove(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学生id:");
int id=sc.nextInt();
int flag=0;
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
int Sid = student.getId();
if(id==Sid){
list.remove(i);
flag=1;
System.out.println("id为"+id+"删除成功");
break;
}
}
if(flag==0){
System.out.println("id不存在,删除失败");
}
}
//修改学生信息
public static void SetStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要需要改的学生id:");
int id = sc.nextInt();
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
int Sid= student.getId();
if(Sid==id){
System.out.println("请输入修改姓名:");
String name = sc.next();
System.out.println("请输入修改年龄:");
int age = sc.nextInt();
System.out.println("请输入修改地址:");
String address = sc.next();
list.set(i, new Student(id,name,age,address));
System.out.println("修改成功");
break;
}else {
System.out.println("学生"+id+"不存在,请重新输入:");
id = sc.nextInt();
i=0;
}
}
}
//查询学生信息
public static void FindAllStudent(ArrayList<Student> list){
if(list.isEmpty()){
System.out.println("当前无学生信息,请添加后再查询");
} else{
System.out.println("id"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"家庭住址");
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getAddress());
}
}
}
public static boolean FindID(ArrayList<Student> list,int id){
for (int i = 0; i < list.size(); i++) {
if(id==list.get(i).getId()){
return false;
}
}
return true;
}
}
Student类:
package com.liang.studentGuanli;
public class Student {
private int id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(int id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int 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;
}
}
User类:
package com.liang.studentGuanli;
public class User {
private String username;
private String password;
private String identifyNumber;
private String telephone;
public User() {
}
public User(String username, String password, String identifyNumber, String telephone) {
this.username = username;
this.password = password;
this.identifyNumber = identifyNumber;
this.telephone = telephone;
}
public User(String password){
this.password=password;
}
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 getIdentifyNumber() {
return identifyNumber;
}
public void setIdentifyNumber(String identifyNumber) {
this.identifyNumber = identifyNumber;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}