学生管理系统(JAVA后台简约版)
文章目录
- 前言
- 一、学生管理系统
- 二、代码编写步骤
- 1.代码填写提示。
- 2.读入数据
- 总结
前言
最近在学习JAVA,学习能力较弱,所以现在在这里做一个学习记录,供自己日后出现相同问题可以方便解决,同时也希望可以给到大家一些帮助,毕竟这个行业太卷了
一、学生管理系统
学生管理系统应该是一个最为简单的小系统,而且学习JAVA的过程中,学生管理系统应该是最为先接触的一个小项目,我也只是分享一下我自己个人的项目。
二、代码编写步骤
一,写代码前最好是先分析这个项目,首先可以做一个数据流图,分析一下整个项目的一个流程是怎么样的。
就好比学生管理系统来说:
第一步:我们知道现在是要编写一个学生管理系统,那对于这个系统而言,就会有使用对象,那么这个就是我们这个管理系统的用户
这个火柴人就是我们管理系统的用户了
第二步,那这个用户就需要对这个管理系统做一个登录打开,或者说注册再登录的东西(前端还没有学,不会写前端页面,Java的登录注册也还不会,就也没有写,抱歉)
第三步,1.这个用户对自己的个人信息进行一个信息的录入,其中包括学号,姓名,年龄,生日…(我就写了这些,其他的大家有需求可以自己添加补入);2.学生管理系统的一个查询所有学生信息;3.学生管理系统对学生的修改信息功能;4.学生管理系统对学生信息的删除功能;这里就是一个Controller层
第四步,那这个学生新增信息时,将学号输入之后,要判断这个学生的学号是否正确或者说是否重复了,那么就要验证一个学生信息的合法性,合法就进入下一个状态,否则就返回上一步,重新输入学号;以及在删除学生的时候也要对输入的学生学号进行判断,看这个学生是不是在这个数据库中,如果不合法就返回,重新输入学号,合法就进入下一步,删除学生信息
第五步,对这个学生管理系统还要有查询功能,对学生信息的查看,产生一个列表出来
第六步,修改学生信息(我没写这个功能)
(这里就是一个Service层)
第七步,就是对上面几步的请求做一个数据的处理,新增就添加数据,删除就删除数据,修改就修改数据,查看就读取数据。
(这个是数据的Dao层
1.代码填写提示。
我们写代码的过程中,最好要从后往前写,从Dao层开始编写,然后到Service层,再到最后的Controller层,当然主菜单页面可以在前期先写一个样式,最后所有写完了,再对主菜单页面做一个修改,如果是有前端那接入前端页面就好了,这样子在写的过程中不会出现大量的报错;而且写的过程中,写完一个模块,可以用git存储一下,写完一个,存储一个,不会因为某些突发状况导致数据丢失代码丢失等等情况,而且用git也可以方便修改。
2.读入数据
代码如下(示例):
主菜单页面
然后对学生属性的添加
public class Student {private String id;private String name;private String age;private String birthday;public Student() {}public Student(String id, String name, String age, String birthday) {this.id = id;this.name = name;this.age = age;this.birthday = birthday;}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 String getAge() {return age;}public void setAge(String age) {this.age = age;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;}@Overridepublic String toString() {return id +"\t\t\t" + name +"\t\t" + age +"\t" + birthday ;} }```这个是Dao层```java public class StudentDao {/*** 添加学生信息类* @param student 输入学生信息* @return true -- 成功;false -- 失败*/public boolean addStudent(Student student){boolean write = FileUtil.write(student);return write;}/*** 查询所有学生类* @return 输出学生列表*/public ArrayList<Student> findAllStudent(){return FileUtil.read();}/*** 删除学生信息类* @param index 删除学生索引*/public void deleteStudent(int index){FileUtil.remove(index);} }这里是Service层
public class StudentService {private StudentDao studentDao = new StudentDao();/*** 添加学生类* @param student 添加学生信息* @return true--成功,false--失败*/public boolean addStudent(Student student){ArrayList<Student> allStudent = studentDao.findAllStudent();for (int i = 0; i < allStudent.size(); i++) {if (student.getId().equals(allStudent.get(i).getId())){return false;}return true;}return studentDao.addStudent(student);}/*** 查询所有学生信息列表类* @return 学生信息*/public ArrayList<Student> findAllStudent(){return studentDao.findAllStudent();}/*** 删除学生信息类* @param id 通过id转换为索引删除学生*/public void deleteStudent(String id){int index = findIndexByid(id);studentDao.deleteStudent(index);}/*** 删除学生信息索引* @param id 通过id找到学生* @return 返回学生索引*/public int findIndexByid(String id){ArrayList<Student> list = studentDao.findAllStudent();for (int i = 0; i < list.size(); i++) {Student student = list.get(i);if (student.getId().equals(id)){return i;}}return -1;}}这里是Controller层
public class StudentController {//定义一个新的StudentService的私有类在StudentController中使用private StudentService studentService = new StudentService();//键盘录入private Scanner scanner = new Scanner(System.in);/*** 添加学生信息类* @param student 添加学生信息*/public void addStudent(){System.out.println("请输入学生学号");String id = scanner.next();System.out.println("请输入学生姓名");String name = scanner.next();System.out.println("请输入学生年龄");String age = scanner.next();System.out.println("请输入学生生日");String birthday = scanner.next();Student stu = new Student(id,name,age,birthday);if (studentService.addStudent(stu)){System.out.println("添加成功");}else {System.out.println("学号重复,无法添加");}}/*** 查询所有学生信息*/public void findAllStudent(){ArrayList<Student> student = studentService.findAllStudent();if (student.size() == 0){System.out.println("暂无数据");return;}System.out.println("学号"+"\t\t\t"+"姓名"+"\t\t"+"年龄"+"\t"+"生日");for (int i = 0; i < student.size(); i++) {System.out.println(student.get(i));}}/*** 删除学生信息类*/public void deleteStudent(){System.out.println("请输入要删除的学生学号");String id = scanner.next();studentService.deleteStudent(id);System.out.println("删除成功");} }其中还使用到了一个工具包
public class FileUtil {/*** 读取数据文件** @return 学生信息列表*/public static ArrayList<Student> read() {ArrayList<Student> list = new ArrayList<>();BufferedReader reader = null;try {reader = new BufferedReader(new FileReader("./db.txt"));String line;while ((line = reader.readLine()) != null) {String[] split = line.split(",");list.add(new Student(split[0], split[1], split[2], split[3]));}} catch (Exception e) {e.printStackTrace();} finally {try {if (reader != null) {reader.close();}} catch (IOException e) {e.printStackTrace();}}return list;}/*** 写入数据文件-一次写入多个学生信息** @param list 学生信息列表*/public static boolean write(ArrayList<Student> list, boolean flag) {FileOutputStream fos = null;try {fos = new FileOutputStream("./db.txt", flag);for (Student student : list) {String str = student.getId() + "," + student.getName() + "," + student.getAge() + "," + student.getBirthday() + "\n";fos.write(str.getBytes(StandardCharsets.UTF_8));}return true;} catch (IOException e) {e.printStackTrace();} finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}return false;}public static boolean write(ArrayList<Student> list) {return write(list, true);}/*** 写入数据文件 - 一次只写入一个学生信息** @param stu 单个学生对象*/public static boolean write(Student stu, boolean flag) {ArrayList<Student> list = new ArrayList<>();list.add(stu);return write(list, flag);}public static boolean write(Student stu) {return write(stu, true);}public static void remove(int index) {ArrayList<Student> list = read();list.remove(index);write(list, false);} }总结
以上全部就是我的整个学生管理系统的代码编写以及个人对项目分析和代码编写路线的全部概述,新人入门,Java牛仔子,希望有大佬看到,有什么不足的可以对小弟进行一个提点,点拨一些小牛仔子,万分感谢,感激不尽。谢谢
总结
以上是生活随笔为你收集整理的学生管理系统(JAVA后台简约版)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: vba调用excel内置函数
- 下一篇: 用Java实现一个学生管理系统(附源码)