类代码
public class student {
// id name age adress
int id;
String name;
int age;
String adress;
public student() {
}
public student(int id, String name, int age, String adress) {
this.id = id;
this.name = name;
this.age = age;
this.adress = adress;
}
/**
* 获取
* @return id
*/
public int getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(int id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return adress
*/
public String getAdress() {
return adress;
}
/**
* 设置
* @param adress
*/
public void setAdress(String adress) {
this.adress = adress;
}
public String toString() {
return "student{id = " + id + ", name = " + name + ", age = " + age + ", adress = " + adress + "}";
}
}
public class User {
private String userName;
private String passWord;
private String userID;
private String phoneNumber;
public User() {
}
public User(String userName, String passWord, String userID, String phoneNumber) {
this.userName = userName;
this.passWord = passWord;
this.userID = userID;
this.phoneNumber = phoneNumber;
}
/**
* 获取
* @return userName
*/
public String getUserName() {
return userName;
}
/**
* 设置
* @param userName
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* 获取
* @return passWord
*/
public String getPassWord() {
return passWord;
}
/**
* 设置
* @param passWord
*/
public void setPassWord(String passWord) {
this.passWord = passWord;
}
/**
* 获取
* @return userID
*/
public String getUserID() {
return userID;
}
/**
* 设置
* @param userID
*/
public void setUserID(String userID) {
this.userID = userID;
}
/**
* 获取
* @return phoneNumber
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* 设置
* @param phoneNumber
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String toString() {
return "User{userName = " + userName + ", passWord = " + passWord + ", userID = " + userID + ", phoneNumber = " + phoneNumber + "}";
}
}
运行代码
import javafx.scene.SubScene;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class APP {
public static void main(String[] args) {
ArrayList<User> list = new ArrayList<User>();
System.out.println("欢迎来到学生管理系统");
looper:
while (true) {
System.out.println("请选择操作:1,登录,2注册,3忘记密码,4退出");
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
switch (option) {
case 1: {
login(list);
break;
}
case 2: {
register(list);
break;
}
case 3: {
forgetPassword(list);
break;
}
case 4: {
System.out.println("谢谢使用,再见");
System.exit(0);
}
default: {
System.out.println("输入有无,请重新输入");
break;
}
}
}
}
private static void forgetPassword(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.next();
boolean flag = contains(list, username);
if (true) {
System.out.println("当前用户还没注册");
return;
}
System.out.println("请输入手机号");
String Phonenumber = sc.next();
System.out.println("请输入身份证号");
String personID = sc.next();
int index = findIndex(username, list);
User user = list.get(index);
if (!(user.getUserName().equals(username) && user.getUserID().equals(personID))) {
System.out.println("身份证号或手机号输入有误,不能修改密码");
} else {
System.out.println("信息输入正确可修改密码");
}
// 执行到这里说明信息输入正确,可以重新设置密码
String password;
String againpassword;
while (true) {
System.out.println("请输入新的密码");
password = sc.next();
System.out.println("请在此输入密码");
againpassword = sc.next();
if (!(password.equals(againpassword))) {
System.out.println("两次密码输入不同重新输入");
continue;
} else {
break;
}
}
//新的密码添加到集合当中
user.setPassWord(password);
System.out.println("修改成功");
}
private static int findIndex(String username, ArrayList<User> list) {
for (int i = 0; i < list.size(); i++) {
User u = list.get(i);
if (u.getUserName().equals(username)) {
return i;
}
}
return -1;
}
private static void register(ArrayList<User> list) {
//用户名,密码,身份证号码,手机号码放到用户对象中
// 把用户对象添加到集合中
Scanner sc = new Scanner(System.in);
String username;
while (true) {
System.out.println("请输入用户名:");
username = sc.next();
//用户名唯一
boolean flag1 = checkUserName(username);
if (!flag1) {
System.out.println("当前格式不满足,请重新输入");
continue;
}
boolean flag2 = contains(list, username);
if (flag2) {
System.out.println("当前用户名" + username + "已存在,请重新输入");
} else {
System.out.println("当前用户名" + username + "可用");
break;
}
}
// 键盘录入密码
String againcode;
while (true) {
System.out.println("请输入要注册的密码");
String firstcode = sc.next();
System.out.println("请再次输入密码");
againcode = sc.next();
if (!firstcode.equals(againcode)) {
System.out.println("两次输入的密码不一样,再次输入");
continue;
} else {
System.out.println("两次输入的密码一致,轻疾卒输入");
break;
}
}
//输入身份证号码
String personCode;
while (true) {
System.out.println("请输入身份证号");
personCode = sc.next();
boolean flag3 = checkPersonId(personCode);
if (!flag3) {
System.out.println("当前输入的身份证号无效");
continue;
} else {
System.out.println("当前输入的身份证号有效,请继续输入");
break;
}
}
// 手机号验证
String PhoneNumber;
while (true) {
System.out.println("请输入手机号");
PhoneNumber = sc.next();
boolean flag4 = checkPhoneNumber(PhoneNumber);
if (!flag4) {
System.out.println("当前手机号无效");
continue;
} else {
System.out.println("当前手机号有效");
break;
}
}
// 用户名,密码等放到对象当中
User u = new User(username, againcode, personCode, PhoneNumber);
//添加到集合中
list.add(u);
System.out.println("注册成功");
for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i).getUserName()+list.get(i).getUserID()+list.get(i).getPassWord()+list.get(i).getPhoneNumber());
User user = new User();
user = list.get(i);
System.out.println(user.getUserName() + user.getPassWord() + user.getUserID() + user.getPhoneNumber());
}
}
private static boolean checkPhoneNumber(String PhoneNumber) {
int len = PhoneNumber.length();
if (len != 11) {
return false;
}
boolean flag = PhoneNumber.startsWith("0");
if (flag) {
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 personCode) {
int len = personCode.length();
boolean flag = personCode.startsWith("0");
if (flag) {
return false;
}
if ((len != 18)) {
return false;
}
for (int i = 0; i < personCode.length(); i++) {
char c = personCode.charAt(i);
if ((c <= 'z' && c >= 'a')) {
return false;
}
}
char c = personCode.charAt(personCode.length() - 1);
if (!((c < 'Z' && c > 'A') || (c < 'z' && c > 'a') || (c < '9' && c > '0'))) {
return false;
}
return true;
}
private static boolean contains(ArrayList<User> list, String username) {
for (int i = 0; i < list.size(); i++) {
User chekMame = list.get(i);
String rightname = chekMame.getUserName();
if (rightname.equals(username)) {
return true;
}
}
return false;
}
private static boolean checkUserName(String username) {
// 用户名长度必须在3~15位之间
if (username.length() > 15 || username.length() < 3) {
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;
}
}
// 代码能执行到这里说明长度满足,范围满足
//但是不能是纯数字//统计在用户名中,有多少字母就可以了。
int count = 0;
for (int i = 0; i < username.length(); i++) {
char c = username.charAt(i);
if ((c <= 'Z' && c >= 'A') || (c >= 'a' || c <= 'z')) {
count++;
break;
}
}
return count > 0;
}
// 生成验证码
public static String getcode() {
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();
Random r = new Random();
for (int i = 0; i < 4; i++) {
int index1 = r.nextInt(list.size());
char c = list.get(index1);
sb.append(c);
}
int number = r.nextInt(10);
int psition = r.nextInt(sb.length());
sb.insert(psition, number);//把一个随机数添加到随机的位置
String code = sb.toString();
return code;
}
private static void login(ArrayList<User> list) {
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.next();
boolean flag = contains(list, username);
if (!flag) {
System.out.println("当前用户名不存在,请先登录");
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 (rightcode.equalsIgnoreCase(code)) {//忽略大小写
System.out.println("验证码正确");
break;
} else {
System.out.println("验证码错误");
continue;
}
}
// 判断密码和用户名
User userinfo = new User(username, password, null, null);
boolean result = checkuserinfo(userinfo, list);
if (result) {
System.out.println("登录成功,欢迎来到学生管理系统");
startStudentSystem();
break;
} else {
System.out.println("用户名或密码错误");
if (i == 2) {
System.out.println("当前账号被锁定,请联系凯狠好,电话号码是15209030645");
//当前账号锁定之后结束方法
return;
} else {
System.out.println("还剩下" + (2 - i) + "次机会");
}
}
}
}
private static boolean checkuserinfo(User userinfo, ArrayList<User> list) {
for (int i = 0; i < list.size(); i++) {
if (userinfo.getUserName().equals(list.get(i).getUserName()) && userinfo.getPassWord().equals(list.get(i).getUserName())) {
return true;
}
}
return false;
}
public static void startStudentSystem() {
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.退出");
Scanner sc = new Scanner(System.in);
System.out.println("请重新输入你的选择:");
String choose = sc.next();
switch (choose) {
case "1": {
addStudent(list);
System.out.println("添加成功");
break;
}
case "2": {
deleteStudent(list);
break;
}
case "3": {
researchStudent(list);
break;
}
case "4": {
updateStudent(list);
break;
}
case "5": {
System.out.println("退出");
break loop;
}
default: {
System.out.println("输入有误,请重新输入:");
break;
}
}
}
}
// 添加学上
public static void addStudent(ArrayList<student> list) {
// 利用空参构造先加上学生对象
Scanner sc = new Scanner(System.in);
System.out.println("添加学生:");
student s = new student();
int id = 0;
while (true) {
System.out.println("请输入学生的id:");
id = sc.nextInt();
boolean falg = coantains(list, id);
if (falg) {
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 adress = sc.next();
s.setAdress(adress);
// 把该学生对象添加到集合
list.add(s);
}
// 删除学生
public static void deleteStudent(ArrayList<student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("删除学生:");
int id = 0;
System.out.println("请输入学生的id:");
id = sc.nextInt();
int index = getIndex(list, id);
if (index >= 0) {
student stu = list.get(index);
list.remove(index);
System.out.println("删除的是id为" + stu.id + "\t" + "的学生,删除成功");
} else {
System.out.println("该id不存在,删除失败");
}
}
// 修改学生
public static void updateStudent(ArrayList<student> list) {
System.out.println("请输入修改学生的id:");
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
int index = getIndex(list, id);
if (index < 0) {
System.out.println("该学生不存在");
return;
} else {
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 newadress = sc.next();
stu.setAdress(newadress);
System.out.println("修改成功");
}
}
// 查询学生
public static void researchStudent(ArrayList<student> list) {
System.out.println("查询学生:");
System.out.println("id" + "\t\t" + "姓名" + "\t" + "年龄" + "\t" + "家庭住址");
if (list.size() == 0) {
System.out.println("当前无学生信息");
// 结束方法
return;
} else {
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).getAdress());
}
}
}
// 比较id并若存在返回true,否则返回false
public static boolean coantains(ArrayList<student> list, int id) {
//循环遍历集合得到里面的每一个学生对象
// 拿到学生对象后,获取id并进行判断不存在false
// 存在,true
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == id) {
return true;
}
}
return false;
}
// 比较id并返回索引
public static int getIndex(ArrayList<student> list, int id) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId() == id) {
return i;
}
}
return -1;
}
}