- 使用 IO 完成一个包含增删改查的具有完整逻辑的管理系统,实现数据持久化,以学生管理系统为例:
- 登录注册(可添加登录验证码、修改密码等等),数据存储在 user.txt 文件中
- 录入学生(存储学生的相关信息),数据存储在 data.txt 中
- 删除学生
- 修改学生信息
- 查找学生,此项扩展性较强,例如:
- 根据学号查找
- 根据专业查找
- 根据学校、专业、性别等多条件查询,不限制思路,自行添加
- 加分项,数据分析
- 以学生为例,统计学生的分布情况,每个专业多少人,每个专业学生考试通过率多高等,根据自己能力,自由发挥,不做硬性要求。
- 代码要求要有一定的容错性,例如:
- 查找一个学生,如果该学生不存在,应有相应的提示,以用户的角度全面考虑问题,确保系统能够稳定运行;其次正确使用异常处理,对异常进行相应的捕获和理。
所有的操作都在控制台进行,JDK 版本统一 1.8,代码编码统一用 UTF-8,命名风格采用驼峰式命名,变量、类的命名能够见名知意,不可使用拼音、数字命名,添加相应的注释,代码条理清晰,代码可分为多个模块。
import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable{
private String name;//姓名
private char sex;//性别
private String school;//学校
private String subject;//专业
public String studentid;//学号
public Student() {
}
public Student(String name, char sex, String school, String subject, String studentid) {
this.name = name;
this.sex = sex;
this.school = school;
this.subject = subject;
this.studentid = studentid;
}
public Student(String name, char sex, String subject, String studentid) {
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getStudentid() {
return studentid;
}
public void setStudentid(String studentid) {
this.studentid = studentid;
}
@Override
public boolean equals(Object o) {
//自反性
if (this == o) return true;
//任何对象不等于null,比较是否为同一类型
if (!(o instanceof Student)) return false;
//强制类型转换
Student student = (Student) o;
//比较属性值
return Objects.equals(getSchool(),student.getSchool()) &&
Objects.equals(getStudentid(), student.getStudentid()) &&
Objects.equals(getSex(), student.getSex());
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex=" + sex +
", school='" + school + '\'' +
", subject='" + subject + '\'' +
", studentid='" + studentid + '\'' +
'}';
}
}
import java.io.Serializable;
public class users implements Serializable {
private String name;//姓名
private String password;//密码
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public String toString() {
return "users{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public users() {
}
public users(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import java.io.*;
import java.util.*;
public class StudentSystem {
//负责提供所有的业务需求,比如展示系统的欢迎页,注册登录等
//存储所用的学生信息
public static Map<String, String> map = new HashMap<>();
public static ArrayList<Student> students = new ArrayList<>();
Scanner sc = new Scanner(System.in);
//设计方法start,负责展示欢迎页面
public void start() {
while (true) {
System.out.println("-------欢迎进入学生管理系统-------");
System.out.println("1、用户登录");
System.out.println("2、用户注册");
System.out.println("请选择执行操作");
String commend=sc.next();
// int commend = sc.nextInt();
switch (commend) {
case "1":
login();
//学生登录
break;
case "2":
//学生注册
creataccount();
break;
default:
System.out.println("你的输入有错误,请重新输入");
}
}
}
private void login() {
System.out.println("=======登录=======");
//判断系统中是否存在账户对象,如果存在,才能登录,否则,直接结束该操作
if (map.size() == 0) {
System.out.println("当前系统中无任何用户,请先注册后再登录");
return;
}
System.out.println("系统中存在用户对象,可以进行登录操作了");
System.out.println("请输入你的用户名");
String name = sc.next();
if (!(map.containsKey(name))) {
System.out.println("你输入的用户名不存在,请重新输入");
} else {
while (true) {
String code6 = creatcode();
System.out.println(code6);
while (true) {
System.out.println("请输入验证码");
String code = sc.next();
if (code.equals(code6)) {
while (true) {
System.out.println("验证成功,请输入你的密码");
String password = sc.next();
if (password.equals(map.get(name))) {
System.out.println("恭喜你,登录成功");
//展示登录后的操作页面
showStudentCommend();
return;//跳出当前方法
} else {
System.out.println("您的密码输入有误,请重新输入");
}
}
} else {
}
}
}
}
}
private void showStudentCommend() {
//登录后的操作页面
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("6、修改密码");
System.out.println("7、退出");
System.out.println("8、注销账户");
String commend=sc.next();
switch (commend) {
case "1":
//1、添加学生并录入学生信息
addStudent(sc);
list();
break;
case "2":
//2、删除学生信息
deleteStudent(sc);
break;
case "3":
//3、修改学生信息
updateStudent(sc);
list();
break;
case "4":
findStudent(sc);
//4、查询学生信息
break;
case "5":
//5、数据分析
dataAnalyse();
break;
case "6":
updatepasswod();
//6、修改密码
return;
case "7":
//7、退出
loadUsers();
loadStudents();
System.out.println("退出成功");
return;
case "8":
//8、注销账户
if (deleteuser(sc)) {
loadUsers();
loadStudents();
return;
}
break;
default:
System.out.println("您当前选择的操作是不存在的,请确认");
}
}
}
private void updatepasswod() {
System.out.println("=====进行密码修改操作====");
System.out.println("请您输入您的用户名");
String name = sc.next();
//判断输入的密码是否正确
if (map.containsKey(name)) {
while (true) {
System.out.println("请您输入您的密码");
String password = sc.next();
if (password.equals(map.get(name))) {
//认证成功
while (true) {
System.out.println("请您输入您的新密码");
String newpassword = sc.next();
System.out.println("请您输入您的确认密码");
String okpassword = sc.next();
if (newpassword.equals(okpassword)) {
map.replace(name, password, newpassword);
saveUsers();
loadUsers();
System.out.println("密码修改成功");
return;
//可以真正修改密码了
} else {
System.out.println("您输入的两次密码不一致,请重新输入");
}
}
} else {
System.out.println("密码输入错误,请重新输入");
}
}
} else {
System.out.println("您的输入有误");
}
}
private boolean deleteuser(Scanner sc) {
System.out.println("======进行销户操作=====");
//问问用户是否确定要销户
System.out.println("您确定要销户吗?y/n");
String commend = sc.next();
switch (commend) {
case "y":
//确实要销户
System.out.println("请你输入您的账户名称和账户密码");
String name = sc.next();
String password = sc.next();
map.remove(name, password);
System.out.println("销户成功");
return true;
default:
System.out.println("好的,您的账户保留");
return false;
}
}
private void list() {
System.out.println("=====展示录入的学生信息=====");
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i));
}
}
private void addStudent(Scanner sc) {
System.out.println("========录入学生信息========");
Student student = new Student();
System.out.print("请输入姓名");
String name = sc.next();
student.setName(name);
while (true) {
System.out.print("请输入性别");
char sex = sc.next().charAt(0);
if (sex == '女' || sex == '男') {
student.setSex(sex);
break;
} else {
System.out.println("您的输入有误,只能输入男或者女");
}
}
System.out.println("请输入学校名称");
String school = sc.next();
student.setSchool(school);
System.out.print("请输入专业 ");
String subject = sc.next();
student.setSubject(subject);
System.out.print(" 请输入学号");
String studentid = sc.next();
student.setStudentid(studentid);
students.add(student);
saveStudents();
loadStudents();
System.out.println("添加成功");
}
private void deleteStudent(Scanner sc) {
System.out.println("======删除学生信息=======");
System.out.print("请输入要删除的学生的学号");
String studentid = sc.next();
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getStudentid().equals(studentid)){
iterator.remove();
saveStudents();
loadStudents();
System.out.println("" + "删除成功");
return;
}
}
System.out.println("删除失败");
}
private void updateStudent(Scanner sc) {
System.out.println("=====修改学生信息====");
System.out.print("请输入你要修改的学生的名字和学号");
String Name= sc.next();
String studentId = sc.next();
for (Student student : students) {
if (student.getStudentid().equals(studentId)&&student.getName().equals(Name))
System.out.println("你可以进行修改");
System.out.print("请输入修改后的学生名字");
String name = sc.next();
student.setName(name);
while (true) {
System.out.print("请输入修改后的学生性别");
char sex = (sc.next()).charAt(0);
if (sex == '女' || sex == '男') {
student.setSex(sex);
break;
} else {
System.out.println("您的输入有误,只能输入男或者女");
}
}
System.out.print("请输入修改后的学生学校");
String school = sc.next();
student.setSchool(school);
System.out.print("请输入修改后的学生专业");
String subject = sc.next();
student.setSubject(subject);
System.out.print("请输入修改后的学生学号");
String studentid = sc.next();
student.setStudentid(studentid);
saveStudents();
loadStudents();
System.out.println("学生信息修改成功");
return;
}
System.out.println("修改失败");
}
private void findStudent(Scanner sc) {
System.out.println("=====查找学生信息====");
// 实现查找学生的逻辑
// 可以根据学号、专业、学校等条件进行查找
System.out.print("请输入你要查找的学生的学号和专业");
String studentId = sc.next();
String subject = sc.next();
for (Student student : students) {
if (student.getStudentid().equals(studentId) || student.getSubject().equals(subject))
System.out.println("学生名字:"+student.getName()+"\n学生性别:"+student.getSex()+"\n学生学校:"+student.getSchool()
+"\n学生专业:"+student.getSubject()+"\n学生学号"+student.getStudentid());
return;
}
System.out.println("未查找到相关信息");
}
private void dataAnalyse() {
System.out.println("=======数据分析=======");
//统计学生的分布情况,每个专业多少人
//首先要判断一共有多少种专业
Map<String, Integer> Subjects = new HashMap<>();//键为学科,值为个数
//下边过程统计有多少专业,每个专业有多少人
for (Student student : students) {
if (!Subjects.containsKey(student.getSubject())) {
Subjects.put(student.getSubject(), 1);
} else {
Subjects.replace(student.getSubject(), Subjects.get(student.getSubject()) + 1);
}
}
System.out.println("一共有" + Subjects.size() + "种专业");
for (String subject : Subjects.keySet()) {
System.out.println(subject + "一共有" + Subjects.get(subject) + "个人");
}
}
//展示当前登录的账户信息
public static void loadUsers() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream
("D:\\code\\Second examination\\src\\com\\itheima\\StudentManage\\user.txt"))) {
map= (Map<String, String>) ois.readObject();
System.out.println(map);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void saveUsers() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream
("D:\\code\\Second examination\\src\\com\\itheima\\StudentManage\\user.txt"))) {
oos.writeObject(map);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void loadStudents() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream
("D:\\code\\Second examination\\src\\com\\itheima\\StudentManage\\data.txt"))) {
students = (ArrayList<Student>) ois.readObject();
System.out.println(students);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void saveStudents() {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream
("D:\\code\\Second examination\\src\\com\\itheima\\StudentManage\\data.txt"));
oos.writeObject(students);
} catch (Exception e) {
e.printStackTrace();
}
}
private void creataccount( ) {
System.out.println("======注册=====");
//1、创建一个学生用户对象,用来封装学生的用户信息
users user = new users();
//需要用户输入自己的开户信息,以便赋值给学生用户对象
System.out.println("请你输入你的账户名称");
String name = sc.next();
user.setName(name);
while (true) {
System.out.println("请输入您的密码");
String password = sc.next();
user.setPassword(password);
System.out.println("请输入你的确认密码");
String okpassword = sc.next();
if (password.equals(okpassword)) {
user.setPassword(password);
break;
} else {
System.out.println("您的密码输入有误,请重新输入");
}
}
map.put(user.getName(),user.getPassword());
saveUsers();
System.out.println("恭喜你,"+ user.getName()+"你注册成功了");
}
//需要系统为这个学生用户生成一个验证码(共8位,可以是数字,字母)
private String creatcode(){
System.out.println("=====生成验证码====");
String code="";
Random r=new Random();
for (int i = 0; i <8; i++) {
int a=r.nextInt(3);
switch (a){
case 0:
int data=r.nextInt(10);
code+=data;
break;
case 1:
char data1=(char)(r.nextInt(26)+65);
code+=data1;
break;
case 2:
char data2=(char)(r.nextInt(26)+97);
code+=data2;
break;
}
}
return code;
}
}
public class Test {
//测试类,负责对我们的系统进行测试
public static void main(String[] args) {
//创建学生管理系统对象
StudentSystem s=new StudentSystem();
//调用start方法
s.start();
}
}