登录
java
复制代码
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
* @author gyf
* @ClassName System
* @Date 2024/8/4 15:31
* @Version V1.0
* @Description :
*/
public class Login {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<User> list = new ArrayList<>();
while (true) {
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作:1登录 2注册 3忘记密码 4退出");
System.out.println("请输入你的选择:");
String choose = scanner.next();
switch (choose) {
case "1" -> login(list);
case "2" -> register(list);
case "3" -> forgetpd(list);
case "4" -> {
System.out.println("退出");
System.exit(0);
}
default -> System.out.println("你的输入不正确,请重新输入:");
}
}
}
// 登录
private static void login(ArrayList<User> list) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入要登录的用户名");
String username = scanner.next();
// 判断用户名是否存在
boolean exist = contains(list, username);
if (!exist) {
System.out.println("您输入的用户名不存在,请先注册");
return;
}
// 判断输入密码
System.out.println("请输入登录的密码:");
String pw = scanner.next();
while (true) {
String code = generateCode();
System.out.println("请输入验证码:" + code);
String inputcode = scanner.next();
// 验证码忽略大小写
if ((inputcode.equalsIgnoreCase(code))) {
System.out.println("验证码正确");
break;
} else {
System.out.println("验证码输入不正确,请重新输入");
continue;
}
}
// 当需要判断的参数比较多的时候 可以初始化一个对象 将对象传进去 然后通过对象获取属性进行判断
boolean flag = chaeck(list, username, pw);
if (flag) {
System.out.println("用户名和密码正确,登陆成功");
// 直接通过类调用方法
StudentSystem.startStudentSystem();
break;
} else {
System.out.println("用户名或密码错误,请重新输入");
System.out.println("你还有的机会次数:" + (2 - i));
if (i == 2) {
System.out.println("账户被锁定");
return;
}
}
}
}
private static boolean chaeck(ArrayList<User> list, String username, String pw) {
for (int i = 0; i < list.size(); i++) {
String name = list.get(i).getUsername();
String pwd = list.get(i).getPassword();
if (name.equals(username) && pwd.equals(pw)) {
return true;
}
}
return false;
}
// 忘记密码
private static void forgetpd(ArrayList<User> list) {
// 1,键盘录入用户名,判断当前用户名是否存在,如不存在,直接结束方法,并提示:未注册
//键盘录入身份证号码和手机号码2.
//3,判断当前用户的身份证号码和手机号码是否一致,
//如果一致,则提示输入密码,进行修改。
//如果不一致,则提示:账号信息不匹配,修改失败。
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要登录的用户名");
String username = scanner.next();
// 判断用户名是否存在
boolean exist = contains(list, username);
if (!exist) {
System.out.println("您输入的用户名不存在,未注册");
return;
}
System.out.println("请输入身份证号码:");
String personID = scanner.next();
System.out.println("请输入手机号码:");
String phonenumber = scanner.next();
// 只需要判断正确性即可
boolean flag = checkIDnumber(list, username, personID, phonenumber);
if (flag) {
System.out.println("身份证和手机号输入正确");
} else {
System.out.println("身份证号和手机号输入有误");
return;
}
String pw1;
while (true) {
System.out.println("请第一次输入新的密码");
pw1 = scanner.next();
System.out.println("请第二次输入新的密码");
String pw2 = scanner.next();
if (pw2.equals(pw1)) {
System.out.println("密码输入一致");
break;
} else {
System.out.println("密码不一致");
continue;
}
}
// 直接修改 先获取当前用户的对象
int index = getIndex(list, username);
User user = list.get(index);
user.setPassword(pw1);
System.out.println("密码重置成功");
showUsers(list);
}
private static int getIndex(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 boolean checkIDnumber(ArrayList<User> list, String username, String personID, String phonenumber) {
// 首先通过username 获取当前的对象 然后判断手机号和身份证号是否一致
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
String name = user.getUsername();
String ID = user.getPersonID();
String ohnumber = user.getPhonenumber();
if (username.equals(name)) {
// 找到当前用户
if (!(phonenumber.equals(ohnumber) && personID.equalsIgnoreCase(ID))) {
return false;
}
}
}
return true;
}
//注册
private static void register(ArrayList<User> list) {
Scanner scanner = new Scanner(System.in);
User user = new User();
// 输入用户名
while (true) {
// 分别对输入的信息做验证 先验证格式再验证是否唯一
System.out.println("请输入用户名");
String username = scanner.next();
// 先验证格式
boolean flag = checkname(username);
if (!flag) {
System.out.println("格式不正确,需要重新输入");
continue;
}
// 唯一性:保证用户名唯一
boolean only = contains(list, username);
if (only) {
System.out.println(username + ":内容不唯一,需要重新输入");
continue;
}
System.out.println("您输入的用户名符合要求");
user.setUsername(username);
break;
}
// 两次输入密码一直才可以
while (true) {
System.out.println("第一次输入密码");
String password1 = scanner.next();
System.out.println("第二次输入密码");
String password2 = scanner.next();
if (password1.equals(password2)) {
System.out.println("密码输入一致,符合要求");
user.setPassword(password1);
break;
} else {
System.out.println("密码输入不一致,请重新输入");
continue;
}
}
// 输入身份证号
while (true) {
System.out.println("请输入身份证号");
String personid = scanner.next();
boolean flag2 = checkPersonid(personid);
if (!flag2) {
System.out.println("输入的格式内容不正确,请重新输入");
continue;
} else {
System.out.println("身份证号码输入满足要求");
user.setPersonID(personid);
break;
}
}
// 输入手机号码
while (true) {
System.out.println("请输入手机号");
String phonenumber = scanner.next();
boolean flag3 = checkPhonenumber(phonenumber);
if (flag3) {
System.out.println("输入的手机号正确");
user.setPhonenumber(phonenumber);
break;
} else {
System.out.println("输入的手机号格式不对,重新输入");
continue;
}
}
// 用户对象添加到集合当中
list.add(user);
System.out.println("用户信息添加成功!");
showUsers(list);
}
// 遍历用户
private static void showUsers(ArrayList<User> list) {
System.out.println("用户名" + "\t\t" + "密码" + "\t\t" + "身份证号" + "\t\t" + "手机号");
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) {
// 必须是十一位,第一位不为0,全部是数字
int len = phonenumber.length();
if (len != 11) {
return false;
}
// 首位不为0
char first = phonenumber.charAt(0);
if (first == '0') {
return false;
}
// 全是数字
for (int i = 0; i < phonenumber.length(); i++) {
char c = phonenumber.charAt(i);
if (!(c >= '0' && c <= '9')) {
return true;
}
}
return true;
}
// 验证身份证号
private static boolean checkPersonid(String personid) {
// 输入身份中号,要求18位,不能以0开头,前17位必须是数字,最后一位可以是数字也可以是X或x
// 验证位数
int length = personid.length();
if (length != 18) {
return false;
}
// 判断第一位
// boolean flag = personid.startsWith("0"); // 以0开头
if (personid.charAt(0) == '0') {
return false;
}
//验证内容前17位置的内容
for (int i = 1; i < length - 1; i++) {
char c = personid.charAt(i);
if (!(c >= '0' && c <= '9')) {
return false;
}
}
// 判断最后一位内容
char c = personid.charAt(length - 1);
if (c == 'X' || c == 'x' || (c >= '0' && c <= '9')) {
return true;
} else {
return false;
}
}
// 判断用户名是否唯一
private static boolean contains(ArrayList<User> list, String username) {
for (int i = 0; i < list.size(); i++) {
if (username.equals(list.get(i).getUsername())) {
return true;
}
}
return false;
}
// 用户名格式验证
private static boolean checkname(String username) {
// 格式:长度在3-15位,只能是字母和数字的组合,但又不能是纯数字。
int length = username.length();
int cout = 0;
if (length < 3 || length > 15) {
return false;
}
for (int i = 0; i < username.length(); i++) {
char c = username.charAt(i);
// 不是字母数字组合的话 返回false
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
return false;
}
// 统计字母的个数,若存在一个则直接返回true;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
cout++;
break;
}
}
return cout > 0;
}
// 生成一个验证码
private static String generateCode() {
Random random = new Random();
StringBuilder sb = new StringBuilder();
ArrayList<Character> list = new ArrayList<>();
for (int i = 0; i < 26; i++) {
list.add((char) ('a' + i));
list.add((char) ('A' + i));
}
// 随机抽取四个字符拼接
for (int i = 0; i < 4; i++) {
int num1 = random.nextInt(list.size());
sb.append(list.get(num1));
}
// 产生一个0-9的随机数字
int num2 = random.nextInt(10);
sb.append(num2);
char[] chars = sb.toString().toCharArray();
int randomindex = random.nextInt(chars.length);
// 最大索引位置元素和随机索引交换
char temp = chars[randomindex];
chars[randomindex] = chars[chars.length - 1];
chars[chars.length - 1] = temp;
return new String(chars);
}
}
java
复制代码
/**
* @author gyf
* @ClassName Uswe
* @Date 2024/8/4 15:47
* @Version V1.0
* @Description :
*/
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) {
this.phonenumber = phonenumber;
}
}
学生系统
java
复制代码
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author gyf
* @ClassName StudentSystem
* @Date 2024/8/4 10:12
* @Version V1.0
* @Description :
*/
public class StudentSystem {
public static void startStudentSystem() {
Scanner scanner = new Scanner(System.in);
// 创建集合 用于存储学生信息
ArrayList<Student> 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:退出");
System.out.println("请输入您的选择:(输入数字)");
String choose = scanner.next();
switch (choose) {
case "1" -> add(list);
case "2" -> delete(list);
case "3" -> update(list);
case "4" -> query(list);
case "5" -> {
System.out.println("退出");
break loop;
// System.exit(0); // 停止虚拟机运行
}
default -> System.out.println("你输入的选择不正确");
}
}
}
// 添加学生
public static void add(ArrayList<Student> list) {
// 添加的时候保证id 一致
Scanner scanner = new Scanner(System.in);
String id = null;
while (true) {
System.out.println("请输入添加的学生id:");
id = scanner.next();
boolean flag = contain(list, id);
if (flag) {
System.out.println("该id已经存在,重新输入");
} else {
break;
}
}
System.out.println("请输入添加的学生姓名:");
String name = scanner.next();
System.out.println("请输入添加的学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入添加的学生地址:");
String address = scanner.next();
Student student = new Student(id, name, age, address);
list.add(student);
System.out.println("添加学生信息成功");
}
// 删除学生
public static void delete(ArrayList<Student> list) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入删除的学生id:");
String id = scanner.next();
int index = getIndex(list, id);
if (index == -1) {
System.out.println("您要删除的id不存在,即将回到初始菜单...");
} else {
// 首先获得索引 然后在删除
list.remove(index);
System.out.println("删除成功");
}
}
// 修改学生
public static void update(ArrayList<Student> list) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入修改的学生id:");
String id = scanner.next();
int index = getIndex(list, id);
if (index == -1) {
System.out.println("您要修改的id不存在,即将回到初始菜单...");
return;
}
// 获取要修改的学生对象
Student student = list.get(index);
System.out.println("请输入添加的学生姓名:");
String name = scanner.next();
student.setName(name);
System.out.println("请输入添加的学生年龄:");
int age = scanner.nextInt();
student.setAge(age);
System.out.println("请输入添加的学生地址:");
String address = scanner.next();
student.setAddress(address);
System.out.println("学生信息修改成功");
}
// 查询学生
public static void query(ArrayList<Student> list) {
if (list.size() == 0) {
System.out.println("没有学生信息,请添加后再查询");
return;
}
System.out.println("学生信息的个数为:" + list.size());
System.out.println("id\t姓名\t年龄\t家庭住址");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getId() + "\t" + list.get(i).getName()
+ "\t" + list.get(i).getAge() + "\t" + list.get(i).getAddress());
}
}
// 判断 id是否在集合中存在
public static boolean contain(ArrayList<Student> list, String id) {
/* for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)){
return true;
}
}
return false;*/
// 代码复用
return getIndex(list, id) >= 0;
}
// 获取id的索引
public static int getIndex(ArrayList<Student> list, String id) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
return i;
}
}
return -1;
}
}
java
复制代码
/**
* @author gyf
* @ClassName Student
* @Date 2024/8/4 10:10
* @Version V1.0
* @Description :
*/
public class Student {
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;
}
}