采取控制台的方式去书写学生管理系统。
"-------------欢迎来到黑马学生管理系统----------------"
"1:添加学生"
"2:删除学生"
"3:修改学生"
"4:查询学生"
"5:退出"
"请输入您的选择:"
属性:id、姓名、年龄、家庭住址
键盘录入每一个学生信息并添加,需要满足以下要求:
键盘录入要删除的学生id,需要满足以下要求:
键盘录入要修改的学生id,需要满足以下要求
打印所有的学生信息,需要满足以下要求
id 姓名 年龄 家庭住址
heima001 张三 23 南京
heima002 李四 24 北京
heima003 王五 25 广州
heima004 赵六 26 深圳
package day11;
public class Student {
private String id;
private String name;
private int age;
private String position;
public Student() {
}
public Student(String id, String name, int age, String position) {
this.id = id;
this.name = name;
this.age = age;
this.position = position;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String 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 position
*/
public String getPosition() {
return position;
}
/**
* 设置
* @param position
*/
public void setPosition(String position) {
this.position = position;
}
public String toString() {
return "Student{id = " + id + ", name = " + name + ", age = " + age + ", position = " + position + "}";
}
}
package day11;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Scanner sc = new Scanner(System.in);
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.print("请输入您的选择: ");
String choose = sc.next();
switch (choose) {
case "1":
addStudent(students);
break;
case "2":
deleteStudent(students);
break;
case "3":
updateStudent(students);
break;
case "4":
queryStudent(students);
break;
case "5":
System.out.println("谢谢光临");
System.exit(0);
default:
System.out.println("没有这个选项");
break;
}
}
}
public static void addStudent(ArrayList<Student> students) {
Scanner sc = new Scanner(System.in);
Student newStu = new Student();
// 判断id是否存在
String id = null;
while (true) {
System.out.println("请输入学生ID");
id = sc.next();
boolean flag = contains(students, id);
if (flag) {
System.out.println("id已存在,请重新输入");
} else {
newStu.setId(id);
break;
}
}
System.out.println("请输入学生姓名");
String name = sc.next();
newStu.setName(name);
System.out.println("请输入学生年龄");
int age = sc.nextInt();
newStu.setAge(age);
System.out.println("请输入学生家庭住址");
String position = sc.next();
newStu.setPosition(position);
students.add(newStu);
System.out.println("学生信息添加成功");
}
public static void deleteStudent(ArrayList<Student> students) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的id");
String id = sc.next();
int index = getIndex(students, id);
if (index >= 0) {
students.remove(index);
System.out.println("id为" + id + "的学生删除成功");
} else {
System.out.println("要删除的id:" + id + "不存在,请重新输入");
}
}
public static void updateStudent(ArrayList<Student> students) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改学生的id");
String id = sc.next();
int index = getIndex(students, id);
if (index == -1) {
System.out.println("要修改的id:" + id + "不存在,请重新输入");
return;
}
Student stu = students.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 newPosition = sc.next();
stu.setPosition(newPosition);
System.out.println("学生信息修改成功");
}
public static void queryStudent(ArrayList<Student> students) {
if (students.size() == 0) {
System.out.println("当前无学生信息,请添加后再查询");
return;
}
System.out.println("id\t\t姓名\t年龄\t家庭地址");
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i).getId() + "\t" + students.get(i).getName() + "\t" + students.get(i).getAge() + "\t" + students.get(i).getPosition());
}
}
/**
* 判断id是否存在
*
* @param students 学生集合
* @param id 学生id
* @return true为存在,false为不存在
*/
public static boolean contains(ArrayList<Student> students, String id) {
return getIndex(students, id) >= 0;
}
/**
* 通过id获得索引
*
* @param students 学生集合
* @param id 学生id
* @return 存在则返回索引值,不存在返回-1
*/
public static int getIndex(ArrayList<Student> students, String id) {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
String sid = student.getId();
if (sid.equals(id)) {
return i;
}
}
return -1;
}
}
为学生管理系统书写一个登陆、注册、忘记密码的功能。
只有用户登录成功之后,才能进入到学生管理系统中进行增删改查操作。
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作1登录 2注册 3忘记密码");
属性:用户名、密码、身份证号码、手机号码
用户名需要满足以下要求:
密码键盘输入两次,两次一致才可以进行注册。
身份证号码需要满足以下要求:
手机号需要满足以下要求:
package day11;
public class Student {
private String id;
private String name;
private int age;
private String position;
public Student() {
}
public Student(String id, String name, int age, String position) {
this.id = id;
this.name = name;
this.age = age;
this.position = position;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String 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 position
*/
public String getPosition() {
return position;
}
/**
* 设置
* @param position
*/
public void setPosition(String position) {
this.position = position;
}
public String toString() {
return "Student{id = " + id + ", name = " + name + ", age = " + age + ", position = " + position + "}";
}
}
package day11;
public class User {
private String username;
private String pd;
private String userId;
private String phoneNumber;
public User() {
}
public User(String username, String pd, String userId, String phoneNumber) {
this.username = username;
this.pd = pd;
this.userId = userId;
this.phoneNumber = phoneNumber;
}
/**
* 获取
* @return username
*/
public String getUsername() {
return username;
}
/**
* 设置
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
* @return pd
*/
public String getPd() {
return pd;
}
/**
* 设置
* @param pd
*/
public void setPd(String pd) {
this.pd = pd;
}
/**
* 获取
* @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 + ", pd = " + pd + ", userId = " + userId + ", phoneNumber = " + phoneNumber + "}";
}
}
package day11;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystem {
public static void startStudentSystem() {
ArrayList<Student> students = new ArrayList<>();
Scanner sc = new Scanner(System.in);
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.print("请输入您的选择: ");
String choose = sc.next();
switch (choose) {
case "1":
addStudent(students);
break;
case "2":
deleteStudent(students);
break;
case "3":
updateStudent(students);
break;
case "4":
queryStudent(students);
break;
case "5":
System.out.println("谢谢光临");
break loop;
// System.exit(0);
default:
System.out.println("没有这个选项");
break;
}
}
}
public static void addStudent(ArrayList<Student> students) {
Scanner sc = new Scanner(System.in);
Student newStu = new Student();
// 判断id是否存在
String id = null;
while (true) {
System.out.println("请输入学生ID");
id = sc.next();
boolean flag = contains(students, id);
if (flag) {
System.out.println("id已存在,请重新输入");
} else {
newStu.setId(id);
break;
}
}
System.out.println("请输入学生姓名");
String name = sc.next();
newStu.setName(name);
System.out.println("请输入学生年龄");
int age = sc.nextInt();
newStu.setAge(age);
System.out.println("请输入学生家庭住址");
String position = sc.next();
newStu.setPosition(position);
students.add(newStu);
System.out.println("学生信息添加成功");
}
public static void deleteStudent(ArrayList<Student> students) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的id");
String id = sc.next();
int index = getIndex(students, id);
if (index >= 0) {
students.remove(index);
System.out.println("id为" + id + "的学生删除成功");
} else {
System.out.println("要删除的id:" + id + "不存在,请重新输入");
}
}
public static void updateStudent(ArrayList<Student> students) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改学生的id");
String id = sc.next();
int index = getIndex(students, id);
if (index == -1) {
System.out.println("要修改的id:" + id + "不存在,请重新输入");
return;
}
Student stu = students.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 newPosition = sc.next();
stu.setPosition(newPosition);
System.out.println("学生信息修改成功");
}
public static void queryStudent(ArrayList<Student> students) {
if (students.size() == 0) {
System.out.println("当前无学生信息,请添加后再查询");
return;
}
System.out.println("id\t\t姓名\t年龄\t家庭地址");
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i).getId() + "\t" + students.get(i).getName() + "\t" + students.get(i).getAge() + "\t" + students.get(i).getPosition());
}
}
/**
* 判断id是否存在
*
* @param students 学生集合
* @param id 学生id
* @return true为存在,false为不存在
*/
public static boolean contains(ArrayList<Student> students, String id) {
return getIndex(students, id) >= 0;
}
/**
* 通过id获得索引
*
* @param students 学生集合
* @param id 学生id
* @return 存在则返回索引值,不存在返回-1
*/
public static int getIndex(ArrayList<Student> students, String id) {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
String sid = student.getId();
if (sid.equals(id)) {
return i;
}
}
return -1;
}
}
package day11;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<User> users = new ArrayList<>();
while (true) {
System.out.println("欢迎来到学生管理系统");
System.out.println("请选择操作 1、登录 2、注册 3、忘记密码 按其他键退出");
String choose = sc.next();
switch (choose) {
case "1":
login(users);
break;
case "2":
register(users);
break;
case "3":
forgetPd(users);
break;
default:
System.out.println("谢谢使用,再见");
System.exit(0);
break;
}
}
}
/**
* 忘记密码
*
* @param users 用户集合
*/
private static void forgetPd(ArrayList<User> users) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.next();
// 判断用户名是否注册
boolean flag = contains(users, username);
if (!flag) {
System.out.println("当前用户" + username + "未注册,请先注册");
}
System.out.println("请输入身份证号码");
String userId = sc.next();
System.out.println("请输入手机号码");
String phoneNumber = sc.next();
int index = findIndex(users, username);
User user = users.get(index);
if (!(user.getUserId().equalsIgnoreCase(userId) && user.getPhoneNumber().equals(phoneNumber))) {
System.out.println("身份证号码或手机号码输入有误,不能修改密码");
return;
}
String pd;
while (true) {
System.out.println("请输入新的密码");
pd = sc.next();
System.out.println("请再次输入新的密码");
String againPd = sc.next();
if (pd.equals(againPd)) {
System.out.println("两次密码一致");
break;
} else {
System.out.println("两次密码不一致,请重新输入");
}
}
user.setPd(pd);
System.out.println("密码修改成功");
}
/**
* 查找用户名在用户集合中的索引值
*
* @param users 用户集合
* @param username 用户名
* @return 存在返回索引值,不存在返回-1
*/
private static int findIndex(ArrayList<User> users, String username) {
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
if (user.getUsername().equals(username)) {
return i;
}
}
return -1;
}
/**
* 注册方法
*
* @param users 用户集合
*/
private static void register(ArrayList<User> users) {
Scanner sc = new Scanner(System.in);
String username;
String pd;
String userId;
String phoneNumber;
// 键盘录入用户名
while (true) {
System.out.println("请输入用户名");
username = sc.next();
// 验证用户名
boolean usernameFlag = checkUsername(username);
if (!usernameFlag) {
// 格式不满足,重新输入
System.out.println("用户名格式不满足条件,需要重新输入");
continue;
}
// 验证唯一性
boolean uniqueUsername = contains(users, username);
if (uniqueUsername) {
System.out.println("用户名" + username + "已存在,请重新输入");
} else {
System.out.println("用户名" + username + "可用");
break;
}
}
// 键盘录入密码
while (true) {
System.out.println("请输入要注册的密码");
pd = sc.next();
System.out.println("请再次输入要注册的密码");
String againPd = sc.next();
if (pd.equals(againPd)) {
System.out.println("两次密码输入一致,继续录入其他数据");
break;
} else {
System.out.println("两次密码输入不一致,请重新输入");
}
}
// 键盘录入身份证号码
while (true) {
System.out.println("请输入身份证号");
userId = sc.next();
boolean userIdFlag = checkUserId(userId);
if (userIdFlag) {
System.out.println("身份证号码格式满足要求");
break;
} else {
System.out.println("身份证号码格式有误,请重新输入");
}
}
// 键盘录入手机号码
while (true) {
System.out.println("请输入手机号码");
phoneNumber = sc.next();
boolean phoneNumberFlag = checkPhoneNumber(phoneNumber);
if (phoneNumberFlag) {
System.out.println("手机号码格式正确");
break;
} else {
System.out.println("手机号码输入有误,请重新输入");
}
}
User newUser = new User(username, pd, userId, phoneNumber);
users.add(newUser);
System.out.println(username + "注册成功");
}
/**
* 验证手机号格式
*
* @param phoneNumber 手机号
* @return 符合规范返回true,否则返回false
*/
private static boolean checkPhoneNumber(String phoneNumber) {
if (phoneNumber.length() != 11) {
return false;
}
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;
}
/**
* 验证身份证号码格式
*
* @param userId 身份证号码
* @return 符合规范返回true,否则返回false
*/
private static boolean checkUserId(String userId) {
if (userId.length() != 18) {
return false;
}
if (userId.startsWith("0")) {
return false;
}
// 前17位必须是数字
for (int i = 0; i < userId.length() - 1; i++) {
char c = userId.charAt(i);
if (!(c >= '0' && c <= '9')) {
return false;
}
}
// 最后一位
char endChar = userId.charAt(userId.length() - 1);
return (endChar >= '0' && endChar <= '9') || (endChar == 'x') || (endChar == 'X');
}
/**
* 查询用户名是否存在
*
* @param users 用户集合
* @param username 用户名
* @return 存在返回true,不存在返回false
*/
private static boolean contains(ArrayList<User> users, String username) {
return findIndex(users, username) >= 0;
}
/**
* 验证用户名格式
*
* @param username 用户名
* @return 符合规范返回true,否则返回false
*/
private static boolean checkUsername(String username) {
// 用户名长度在3~15位
int len = username.length();
if (len < 3 || len > 15) {
return 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;
}
}
// 统计在用户名中,有多少个字母和数字
int charCount = 0;
int numCount = 0;
for (int i = 0; i < username.length(); i++) {
char c = username.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
charCount++;
}
if (c >= '0' && c <= '9') {
numCount++;
}
}
// 只能是数字+字母
if ((charCount + numCount) != username.length()) {
return false;
}
// 不能是纯数字
return numCount != username.length();
}
/**
* 登录方法
*
* @param users 用户集合
*/
private static void login(ArrayList<User> users) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.next();
boolean usernameFlag = contains(users, username);
if (!usernameFlag) {
System.out.println("用户名" + username + "未注册,请先注册再登录");
return;
}
// 只有3次机会
for (int i = 0; true; i++) {
System.out.println("请输入密码");
String pd = 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("验证码错误");
}
}
User userInfo = new User(username, pd, null, null);
boolean checkUserInfo = checkUserInfo(users, userInfo);
if (checkUserInfo) {
System.out.println("登录成功,可以使用学生管理系统");
StudentSystem ss = new StudentSystem();
ss.startStudentSystem();
break;
} else {
if (i == 2) {
System.out.println("当前账号" + username + "被锁定");
return;
} else {
System.out.println("登录失败,用户名或密码错误,还剩下" + (2 - i) + "次机会");
}
}
}
}
/**
* 验证账号密码是否匹配
*
* @param users 用户集合
* @param userInfo 用户信息
* @return 匹配返回true,不匹配返回false
*/
private static boolean checkUserInfo(ArrayList<User> users, User userInfo) {
for (User user : users) {
if (user.getUsername().equals(userInfo.getUsername()) && user.getPd().equals(userInfo.getPd())) {
return true;
}
}
return false;
}
/**
* 获取验证码
*
* @return 验证码
*/
public static String getCode() {
ArrayList<Character> charList = new ArrayList<>();
char[] chars = new char[5];
Random random = new Random();
for (int i = 0; i < 26; i++) {
charList.add((char) ('a' + i));
charList.add((char) ('A' + i));
}
for (int i = 0; i < 5; i++) {
int index = random.nextInt(charList.size());
chars[i] = charList.get(index);
}
chars[random.nextInt(chars.length)] = (char) (random.nextInt(10) + 48);
return new String(chars);
}
}