手把手教你完成(Java)师生信息管理系统
对阶段一学到的知识进行应用,完成练手小项目。同时,也可以当做学校的课设来做。项目已上传 CSDN ,可以按需下载。
一、成果展示
- 添加学生(查看学生)
-
删除学生
-
添加老师(查看老师)
-
删除老师
二、实现步骤
就不写1、2、3、4了,按着顺序一步一来就好。为了避免出错可以模仿。
新建 project ,项目命名为 school-information-sysytem
创建模块,命名为school-edu-info-manager
,选择自己电脑上配置好的 JDK,我电脑上安装的是12版本
创建包com.school.edu.info.manager
分别创建如下包,并在其下分别创建表中所示类
包 | 存储的类 | 作用 |
---|---|---|
com.itheima.edu.info.manager.domain | Student.java Teacher.java | 封装学生信息 封装老师信息 |
com.itheima.edu.info.manager.dao | StudentDao.java TeacherDao.java | 访问存储数据的数组,进行赠删改查(库管) |
com.itheima.edu.info.manager.service | StudentService.java TeacherService.java | 业务的逻辑处理(业务员) |
com.itheima.edu.info.manager.controller | StudentController.java TeacherController.java | 和用户打交道(客服接待) |
com.itheima.edu.info.manager.entry | InfoManagerEntry.java | 程序的入口类,提供一个main方法 |
环境搭建完成,将代码直接分别复制到类里面即可。(包名称不同的话,给包重命名就ok啦!)
1. damain包
Student.java
package com.school.edu.info.manager.damain;
public class Student {
private String id;
private String name;
private String age;
private String birthday;
public Student() {
}
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;
}
}
Teacher.java
package com.school.edu.info.manager.damain;
public class Teacher {
private String id;
private String name;
private String age;
private String birthday;
public Teacher() {
}
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;
}
}
2. controller包
StudentController.java
package com.school.edu.info.manager.controller;
import com.school.edu.info.manager.damain.Student;
import com.school.edu.info.manager.service.StudentService;
import java.util.Scanner;
public class StudentController {
private StudentService studentService = new StudentService();
Scanner sc = new Scanner(System.in);
// 开启学生管理系统, 并展示学生管理系统菜单
public void start() {
StudentLoop:
while (true) {
System.out.println("--------欢迎来到 <学生> 管理系统--------");
System.out.println("请输入您的选择: 1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出");
String choice = sc.next();
switch (choice) {
case "1":
//System.out.println("添加");
addStudent();
break;
case "2":
// System.out.println("删除");
deleteStudentById();
break;
case "3":
// System.out.println("修改");
uppDateStudent();
break;
case "4":
// System.out.println("查看");
findAllStudent();
break;
case "5":
System.out.println("感谢您使用学生管理系统, 再见!");
break StudentLoop;
default:
System.out.println("您的输入有误,请重新输入");
break;
}
}
}
public void uppDateStudent() {
String uppdateId = inputStudentId();
Student newstu = inputStudentInfo(uppdateId);
studentService.uppDateStudent(uppdateId, newstu);
System.out.println("修改成功!");
}
public void deleteStudentById() {
String delId = inputStudentId();
studentService.deleteStudentById(delId);
System.out.println("删除成功!");
}
// 查看学生方法
public void findAllStudent() {
// 1. 调用业务员中的获取方法, 得到学生的对象数组
Student[] stus = studentService.findAllStudent();
// 2. 判断数组的内存地址, 是否为null
if (stus == null) {
System.out.println("查无信息, 请添加后重试");
}
System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
// 3. 遍历数组, 获取学生信息并打印在控制台
for (int i = 0; i < stus.length; i++) {
Student stu = stus[i];
if (stu != null) {
System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getBirthday());
}
}
}
// 添加学生方法
public void addStudent() {
String id;
while (true) {
System.out.println("请输入学生id:");
id = sc.next();
boolean flag = studentService.isExist(id);
if (flag) {
System.out.println("学号已被占用, 请重新输入");
} else {
break;
}
}
Student stu = inputStudentInfo(id);
// 3. 将学生对象,传递给StudentService(业务员)中的addStudent方法
boolean result = studentService.addStudent(stu);
if (result) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
// 4. 根据返回的boolean类型结果, 在控制台打印成功\失败
}
public String inputStudentId() {
String Id;
while (true) {
System.out.println("请输入学生id:");
Id = sc.next();
boolean exist = studentService.isExist(Id);
if (!exist) {
System.out.println("您输入的id不存在,请重新输入:");
} else {
break;
}
}
return Id;
}
public Student inputStudentInfo(String id) {
System.out.println("请输入学生姓名:");
String name = sc.next();
System.out.println("请输入学生年龄:");
String age = sc.next();
System.out.println("请输入学生生日:");
String birthday = sc.next();
// 2. 将学生信息封装为学生对象
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setBirthday(birthday);
return stu;
}
}
TeacherController.java
package com.school.edu.info.manager.controller;
import com.school.edu.info.manager.damain.Teacher;
import com.school.edu.info.manager.service.TeacherService;
import java.util.PrimitiveIterator;
import java.util.Scanner;
public class TeacherController {
private Scanner sc = new Scanner(System.in);
private TeacherService teacherService = new TeacherService();
public void start() {
teacherLoop:
while (true) {
System.out.println("--------欢迎来到 <老师> 管理系统--------");
System.out.println("请输入您的选择: 1.添加老师 2.删除老师 3.修改老师 4.查看老师 5.退出");
String choice = sc.next();
switch (choice) {
case "1":
// System.out.println("添加老师");
addTeacher();
break;
case "2":
// System.out.println("删除老师");
deleteTeacherById();
break;
case "3":
// System.out.println("修改老师");
updateTeacher();
break;
case "4":
// System.out.println("查看老师");
findAllTeacher();
break;
case "5":
System.out.println("感谢您使用老师管理系统, 再见!");
break teacherLoop;
default:
System.out.println("您的输入有误, 请重新输入");
break;
}
}
}
public void updateTeacher() {
String id = inputTeacherId();
Teacher newTeacher = inputTeacherInfo(id);
// 调用业务员的修改方法
teacherService.updateTeacher(id,newTeacher);
System.out.println("修改成功");
}
public void deleteTeacherById() {
String id = inputTeacherId();
// 2. 调用业务员中的删除方法, 根据id, 删除老师
teacherService.deleteTeacherById(id);
// 3. 提示删除成功
System.out.println("删除成功");
}
public void findAllTeacher() {
// 1. 从业务员中, 获取老师对象数组
Teacher[] teachers = teacherService.findAllTeacher();
// 2. 判断数组中是否有元素
if (teachers == null) {
System.out.println("查无信息, 请添加后重试");
return;
}
// 3. 遍历数组, 取出元素, 并打印在控制台
System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
for (int i = 0; i < teachers.length; i++) {
Teacher t = teachers[i];
if (t != null) {
System.out.println(t.getId() + "\t\t" + t.getName() + "\t\t" + t.getAge() + "\t\t" + t.getBirthday());
}
}
}
public void addTeacher() {
String id;
while (true) {
// 1. 接收不存在的老师id
System.out.println("请输入老师id:");
id = sc.next();
// 2. 判断id是否存在
boolean exists = teacherService.isExists(id);
if (exists) {
System.out.println("id已被占用, 请重新输入:");
} else {
break;
}
}
Teacher t = inputTeacherInfo(id);
// 5. 将封装好的老师对象, 传递给TeacherService继续完成添加操作
boolean result = teacherService.addTeacher(t);
if (result) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
}
// 录入老师id
public String inputTeacherId(){
String id;
while(true){
System.out.println("请输入id");
id = sc.next();
boolean exists = teacherService.isExists(id);
if(!exists){
System.out.println("您输入的id不存在, 请重新输入:");
}else{
break;
}
}
return id;
}
// 录入老师信息, 封装为老师对象
public Teacher inputTeacherInfo(String id){
System.out.println("请输入老师姓名:");
String name = sc.next();
System.out.println("请输入老师年龄:");
String age = sc.next();
System.out.println("请输入老师生日:");
String birthday = sc.next();
Teacher t = new Teacher();
t.setId(id);
t.setName(name);
t.setAge(age);
t.setBirthday(birthday);
return t;
}
}
3. service包
StudentService.java
package com.school.edu.info.manager.service;
import com.school.edu.info.manager.damain.Student;
import com.school.edu.info.manager.dao.StudentDao;
public class StudentService {
// 1.创建StudentDao (库管)
private StudentDao studentDao = new StudentDao();
public boolean addStudent(Student stu) {
// 2. 将学生对象, 传递给StudentDao 库管中的addStudent方法
// 3. 将返回的boolean类型结果, 返还给StudentController
return studentDao.addStudent(stu);
}
public boolean isExist(String id) {
Student[] stus = studentDao.findAllStudent();
//假设id不存在
boolean exists = false;
// 遍历数组, 获取每一个学生对象, 准备进行判断
for (int i = 0; i < stus.length; i++) {
Student student = stus[i];
if (student != null && student.getId().equals(id)) {
exists = true;
break;
}
}
return exists;
}
public Student[] findAllStudent() {
Student[] allStudent = studentDao.findAllStudent();
boolean flag = false;
for (int i = 0; i < allStudent.length; i++) {
Student stu = allStudent[i];
if (stu == null) {
flag = true;
break;
}
}
if (flag) {
return allStudent;
} else {
return null;
}
}
public void deleteStudentById(String delId) {
studentDao.deleteStudentById(delId);
}
public void uppDateStudent(String uppdateId, Student newstu) {
studentDao.uppDateStudent(uppdateId,newstu);
}
}
TeacherService.java
package com.school.edu.info.manager.service;
import com.school.edu.info.manager.damain.Teacher;
import com.school.edu.info.manager.dao.TeacherDao;
public class TeacherService {
private TeacherDao teacherDao = new TeacherDao();
public boolean addTeacher(Teacher t) {
return teacherDao.addTeacher(t);
}
public boolean isExists(String id) {
// 1. 获取库管对象中的数组
Teacher[] teachers = teacherDao.findAllTeacher();
boolean exists = false;
// 2. 遍历数组, 取出每一个元素, 进行判断
for (int i = 0; i < teachers.length; i++) {
Teacher teacher = teachers[i];
if(teacher != null && teacher.getId().equals(id)){
exists = true;
break;
}
}
return exists;
}
public Teacher[] findAllTeacher() {
Teacher[] allTeacher = teacherDao.findAllTeacher();
boolean flag = false;
for (int i = 0; i < allTeacher.length; i++) {
Teacher t = allTeacher[i];
if(t != null){
flag = true;
break;
}
}
if(flag){
return allTeacher;
}else{
return null;
}
}
public void deleteTeacherById(String id) {
teacherDao.deleteTeacherById(id);
}
public void updateTeacher(String id, Teacher newTeacher) {
teacherDao.updateTeacher(id,newTeacher);
}
}
4. dao包
StudentDao.java
package com.school.edu.info.manager.dao;
import com.school.edu.info.manager.damain.Student;
public class StudentDao {
// 创建学生对象数组
private static Student[] stus = new Student[5];
public boolean addStudent(Student stu) {
// 添加学生到数组
//定义变量index为-1,假设数组已经全部存满,没有null的元素
int index = 1;
//遍历数组取出每一个元素,判断是否是null
for (int i = 0; i < stus.length; i++) {
Student student = stus[i];
if (student == null) {
index = i;
//如果为null,让index变量记录当前索引位置,并使用break结束循环遍历
break;
}
}
//返回是否添加成功的boolean类型状态
if (index == -1) {
// 装满了
return false;
} else {
// 没有装满, 正常添加, 返回true
stus[index] = stu;
return true;
}
}
public Student[] findAllStudent() {
return stus;
}
public void deleteStudentById(String delId) {
int index = getIndex(delId);
stus[index] = null;
}
public int getIndex(String id) {
int index = -1;
for (int i = 0; i < stus.length; i++) {
Student stu = stus[i];
if (stu != null && stu.getId().equals(id)) {
index = i;
break;
}
}
return index;
}
public void uppDateStudent(String uppdateId, Student newstu) {
int index = getIndex(uppdateId);
stus[index] = newstu;
}
}
TeacherDao.java
package com.school.edu.info.manager.dao;
import com.school.edu.info.manager.damain.Teacher;
public class TeacherDao {
private static Teacher[] teachers = new Teacher[5];
public boolean addTeacher(Teacher t) {
int index = -1;
for (int i = 0; i < teachers.length; i++) {
Teacher teacher = teachers[i];
if(teacher == null){
index = i;
break;
}
}
if(index == -1){
return false;
}else{
teachers[index] = t;
return true;
}
}
public Teacher[] findAllTeacher() {
return teachers;
}
public void deleteTeacherById(String id) {
// 1. 查询id在数组中的索引位置
int index = getIndex(id);
// 2. 将该索引位置的元素, 使用null进行替换
teachers[index] = null;
}
public int getIndex(String id){
int index = -1;
for (int i = 0; i < teachers.length; i++) {
Teacher t = teachers[i];
if(t != null && t.getId().equals(id)){
index = i;
break;
}
}
return index;
}
public void updateTeacher(String id, Teacher newTeacher) {
int index = getIndex(id);
teachers[index] = newTeacher;
}
}
5. entry包
InfoManagerEntry.java
package com.school.edu.info.manager.entry;
import com.school.edu.info.manager.controller.StudentController;
import com.school.edu.info.manager.controller.TeacherController;
import java.util.Scanner;
public class InfoManagerEntry {
public static void main(String[] args) {
//主菜单搭建
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("--------欢迎来到师生信息管理系统--------");
System.out.println("请输入您的选择: 1.学生管理 2.老师管理 3.退出");
String choice = sc.next();
switch (choice) {
case "1":
System.out.println("学生管理");
//开启学生管理系统
StudentController studentcontroller = new StudentController();
studentcontroller.start();
break;
case "2":
System.out.println("老师管理");
TeacherController teacherController = new TeacherController();
teacherController.start();
break;
case "3":
System.out.println("感谢您的使用");
System.exit(0); // 退出当前正在运行的JVM虚拟机
break;
default:
System.out.println("您的输入有误,请重新输入");
break;
}
}
}
}
tln("学生管理");
//开启学生管理系统
StudentController studentcontroller = new StudentController();
studentcontroller.start();
break;
case "2":
System.out.println("老师管理");
TeacherController teacherController = new TeacherController();
teacherController.start();
break;
case "3":
System.out.println("感谢您的使用");
System.exit(0); // 退出当前正在运行的JVM虚拟机
break;
default:
System.out.println("您的输入有误,请重新输入");
break;
}
}
}
}