创建学生管理系统,先定义一个类,代码如下:
package com.jiang.StudentSystem;
public class Student {
//id,name,age,address
private String id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
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.jiang.StudentSystem;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystem {
public static void startStudentSystem() {
ArrayList list = 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.退出");
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的选择:");
String choose = sc.next();
switch (choose) {
case "1" -> addStudent(list);
case "2" -> deleteStudent(list);
case "3" -> updateStudent(list);
case "4" -> queryStudent(list);
case "5" -> {
System.out.println("退出");
break loop;
}
default -> System.out.println("没有这个选项");
}
}
}
//添加学生
public static void addStudent(ArrayList list) {
Student s = new Student();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入学生的id:");
String id = sc.next();
boolean flag = contains(list, id);
if (flag) {
System.out.println("id已经存在,请重新录入");
} else {
s.setId(id);
break;
}
}
System.out.println("请输入学生的姓名:");
String name = sc.next();
s.setName(name);
System.out.println("请输入学生的年龄:");
int age = sc.nextInt();
s.setAge(age);
System.out.println("请输入学生的地址:");
String address = sc.next();
s.setAddress(address);
//把学生添加到集合当中
list.add(s);
System.out.println("学生信息添加成功");
}
//删除学生
public static void deleteStudent(ArrayList list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的id:");
String id = sc.next();
int index = getIndex(list, id);
if (index >= 0) {
list.remove(index);
System.out.println("id为:" + id + "的学生删除成功");
} else {
System.out.println("id不存在,删除失败");
}
}
//修改学生
public static void updateStudent(ArrayList list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的学生id");
String id = sc.next();
int index = getIndex(list, id);
if (index == -1) {
System.out.println("要修改的id" + id + "不存在,请重新输入");
return;
}
//获取要修改的对象
Student stu = list.get(index);
//输入其他信息并修改
System.out.println("请输入要修改的学生姓名");
String newName= sc.next();
stu.setName(newName);
System.out.println("请输入要修改学生的年龄");
int newAge = sc.nextInt();
stu.setAge(newAge);
System.out.println("请输入要修改的学生的地址");
String newAddress = sc.next();
stu.setAddress(newAddress);
System.out.println("学生信息修改成功");
}
//查询学生
public static void queryStudent(ArrayList list) {
if (list.size() == 0) {
System.out.println("当前无学生信息,请添加后再查询");
return;
}
System.out.println("id\t\t姓名\t年龄\t住址");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
}
}
//判断id在集合中是否存在
public static boolean contains(ArrayList list, String id) {
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
String sid = stu.getId();
if (sid.equals(id)) {
return true;
}
}
return false;
}
//通过id获取索引的方法
public static int getIndex(ArrayList list, String id) {
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
String sid = stu.getId();
if (sid.equals(id)) {
return i;
}
}
return -1;
}
}
再创建一个学生管理系统登录界面,代码如下:
package com.jiang.StudentSystem;
public class User {
private String username;
private String passWord;
private String personID;
private String phoneNumber;
public User() {
}
public User(String username, String passWord, String personID, String phoneNumber) {
this.username = username;
this.passWord = passWord;
this.personID = personID;
this.phoneNumber = phoneNumber;
}
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 getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
phoneNumber = phoneNumber;
}
}
创建学生管理系统登录注册页面(此代码已调用学生管理系统),代码如下:
package com.jiang.StudentSystem;
import jdk.swing.interop.SwingInterOpUtils;
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 list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作1登录 2注册 3忘记密码 4退出");
String choose = sc.next();
switch (choose) {
case "1" -> login(list);
case "2" -> register(list);
case "3" -> forgetPassword(list);
case "4" -> {
System.out.println("谢谢使用,再见");
//退出系统设置
System.exit(0);
}
default -> System.out.println("没有这个选项");
}
}
}
private static void forgetPassword(ArrayList list) {
System.out.println("忘记密码");
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名");
String userName = sc.next();
boolean flag=contains(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.getPhoneNumber().equals(phoneNumber))){
System.out.println("身份证号码或者手机号码有误,不能修改密码");
return;
}
//当代码执行到这里,表示所有的数据全部验证成功,直接修改即可
String newPassWord;
while (true) {
System.out.println("请输入新的密码");
newPassWord = sc.next();
System.out.println("请再次输入密码");
String againNewPassWord = sc.next();
if (newPassWord.equals(againNewPassWord)){
System.out.println("两次密码输入一致");
break;
}else{
System.out.println("两次密码输入不一致,请重新输入");
continue;
}
}
//直接修改密码
user.setPassWord(newPassWord);
System.out.println("密码修改成功");
}
private static int findIndex(ArrayList 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 register(ArrayList list) {
System.out.println("注册");
//键盘录入用户名
Scanner sc = new Scanner(System.in);
String userName;
while (true) {
System.out.println("请输入用户名");
userName = sc.next();
//用户名要求:
//1.用户名长度必须在3~15位之间
//2.只能是字母加数字的组合,但是不能是纯数字
boolean flag1 = checkUsername(userName);
if (!flag1) {
System.out.println("用户名格式不满足条件,需要重新输入");
continue;
}
//3.唯一性
boolean flag2 = contains(list, userName);
if (flag2) {
System.out.println("用户名" + userName + "已存在,请重新录入");
} else {
System.out.println("用户名" + userName + "可用");
break;
}
}
//密码键盘输入两次,两次一致才可以进行注册
String passWord;
while (true) {
System.out.println("请输入要注册的密码");
passWord = sc.next();
System.out.println("请再次输入要注册的密码");
String againPassWord = sc.next();
if (!passWord.equals(againPassWord)) {
System.out.println("两次输入的密码不一致,请重新输入");
} else {
System.out.println("两次密码一致,请继续录入其他信息");
break;
}
}
//键盘录入身份证号码
String personID;
while (true) {
System.out.println("请输入身份证号码");
personID = sc.next();
boolean flag3 = checkPersonID(personID);
if (flag3) {
System.out.println("身份证号码满足要求");
break;
} else {
System.out.println("身份证号码输入有误,请重新输入");
continue;
}
}
//键盘录入手机号码
String phoneNumber;
while (true) {
System.out.println("请输入手机号码");
phoneNumber = sc.next();
boolean flag4 = checkPhoneNumber(phoneNumber);
if (flag4) {
System.out.println("手机号码格式正确");
break;
} else {
System.out.println("手机号码格式错误,请重新输入");
continue;
}
}
//用户名,密码,身份证号码,手机号码放到用户对象中
User u = new User(userName, passWord, personID, phoneNumber);
//把用户对象添加到集合中
list.add(u);
System.out.println("注册成功");
//遍历
printList(list);
}
private static void printList(ArrayList list) {
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
System.out.println(user.getUsername() + "," + user.getPassWord() + "," + user.getPersonID() + "," + user.getPhoneNumber());
}
}
//遍历集合
private static boolean checkPhoneNumber(String phoneNumber) {
//长度必须为11位
if (phoneNumber.length() != 11) {
return false;
}
//不能以0开头
if (phoneNumber.startsWith("0")) {
return false;
}
//必须为数字
for (int i = 0; i < phoneNumber.length(); i++) {
char c = phoneNumber.charAt(i);
if (c < '0' && c > '9') {
return false;
}
}
return true;
}
private static boolean checkPersonID(String personID) {
//长度为18
if (personID.length() != 18) {
return false;
}
//不能以0开头
if (personID.startsWith("0")) {
//如果以0开头,直接false
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或小写x
char c = personID.charAt(personID.length() - 1);
if ((c >= '0' && c <= '9') || (c == 'X' || c == 'x')) {
return true;
} else {
return false;
}
}
private static boolean contains(ArrayList 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) {
//用户名长度必须在3~15位之间
int len = userName.length();
if ((len < 3) || (len > 15)) {
return false;
}
//当代码执行到这里,表示用户名的长度是符合要求的。
//继续校验:只能是字母加数字的组合
//循环得到username里面的每一个字符,如果有一个字符不是字母或者数字,那么就返回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;
}
}
//当代码执行到这里,表示什么?
//用户名满足两个要求:1长度满足 2内容也满足(字母+数字)
//但是不能是纯数字
//统计在用户名中,有多少字母就可以了。
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++;
}
}
return count > 0;
}
private static void login(ArrayList list) {
System.out.println("登录");
//键盘录入用户名
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名");
String userName = sc.next();
//判断用户名是否注册
boolean flag = contains(list, userName);
if (!flag) {
System.out.println("用户名" + userName + "未注册,请先注册");
continue;
}
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)) {
System.out.println("验证码正确");
break;
} else {
System.out.println("验证码有误,请重新输入");
continue;
}
}
//验证用户名和密码是否正确
//集合中是否包含用户名和密码
//定义一个方法验证用户名和密码是否正确
//封装思想的应用:
//我们可以把一些零散的数据,封装成一个对象
//以后传递参数的时候,只要传递一个整体就可以了,不需要管这些零散的数据
User userInfo = new User(userName, password, null, null);
boolean result = checkUserInfo(list, userInfo);
if (result) {
System.out.println("登陆成功,可以开始使用学生管理系统了");
//创建对象调用方法,启动学生管理系统
StudentSystem ss=new StudentSystem();
ss.startStudentSystem();
break;
} else {
System.out.println("登陆失败,用户名或密码错误");
if (i == 2) {
System.out.println("当前账号" + userName + "被锁定,请联系安徽新华学院客服:05553-459867");
//当前账号被锁定,直接结束
return;
} else {
System.out.println("用户名或密码错误,还剩下" + (2 - i) + "次机会");
}
}
}
}
private static boolean checkUserInfo(ArrayList list, User userInfo) {
//遍历集合,判断用户是否存在,如果存在登录成功,如果不存在登录失败
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
if (user.getUsername().equals(userInfo.getUsername()) && user.getPassWord().equals(userInfo.getPassWord())) {
return true;
}
}
return false;
}
private static String getCode() {
//1.创建一个集合,添加所有大小写字母
ArrayList list = new ArrayList<>();
for (int i = 0; i < 26; i++) {
list.add((char) ('a' + i));
list.add((char) ('A' + i));
}
StringBuilder sb = new StringBuilder();
//随机获取四个字母
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);
}
//把一个随机数添加到末尾
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);
}
}
代码最终运行结果如下;