说明:学生管理系统案例需求和步骤1请查看上一张《阶段二10_面向对象高级_分类分包思想》
一.学生管理系统案例
步骤2:搭建主菜单和学生管理系统菜单
(0).主菜单和学习菜单界面和思路
界面:
--------欢迎来到信息管理系统--------
请输入您的选择: 1.学生管理 2.老师管理 3.退出
--------欢迎来到 <学生> 管理系统--------
请输入您的选择: 1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出
思路:
用输出语句完成主界面的编写
用Scanner实现键盘录入数据
用switch语句完成操作的选择
(1).主菜单代码[InfoManagerEntry(程序的入口类提供一个main方法)]
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("--------欢迎来到信息管理系统--------");
System.out.println("请输入您的选择: 1.学生管理 2.老师管理 3.退出");
String c = sc.next();
switch (c) {
case "1":
//System.out.println("学生管理");
//开启学生管理系统
StudentController studentController = new StudentController();
studentController.start();
break;
case "2":
System.out.println("老师管理");
break;
case "3":
System.out.println("欢迎下次再次使用信息管理系统,再见。");
System.exit(0); //退出当前程序的jvm平台
break;
default:
System.out.println("输入有误,请重新输入");
break;
}
}
}
(2).学生管理系统菜单代码[StudentController(和用户打交道客服接待)]
//1.开启学生管理系统并输出对应的菜单
public void start() {
Scanner sc = new Scanner(System.in);
studentLoop:
while (true) {
System.out.println("--------欢迎来到 <学生> 管理系统--------");
System.out.println("请输入您的选择: 1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出");
String c = sc.next();
switch (c) {
case "1":
System.out.println("添加学生");
break;
case "2":
System.out.println("删除学生");
break;
case "3":
System.out.println("修改学生");
break;
case "4":
System.out.println("查看学生");
break;
case "5":
System.out.println("退出学生管理系统");
break studentLoop;
default:
System.out.println("输入有误,请重新输入");
break;
}
}
}
步骤3:基本添加功能-思路分析
1_基本添加功能-思路分析.png
步骤4:基本添加功能-代码实现
1.在客户类[StudentController]中接受学生信息并封装对象
public void addStudent() {
//1.输入学生信息提示和获取学生信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生id:");
String id = sc.next();
System.out.println("请输入学生姓名:");
String name = sc.next();
System.out.println("请输入学生年龄:");
String age = sc.next();
System.out.println("请输入学生生日:");
String birthday = sc.next();
//2.把学生信息封装对象
Student student = new Student(id,name,age,birthday);
//3.把学生信息传给业务员添加方法处理,并返回处理结果
StudentService studentService = new StudentService();
boolean bole = studentService.addStudent(student);
//根据处理结果,提示添加成功或失败
if(bole){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
2.在业务类[StudentService]接受客户类传递的学生对象并提交给库管
public boolean addStudent(Student student) {
//1.接受学生对象并传给库管处理
StudentDao studentDao = new StudentDao();
boolean bol = studentDao.addStudent(student);
//根据库管处理的结果返回给客户
return bol;
}
3.库管类[],根据业务类传递过来得学生对象进行添加操作
public boolean addStudent(Student student) {
//1.创建存放学生数组对象,长度为5
Student[] students = new Student[5];
//2.把学生对象存放到数组中
//2.1 获取存放的数组索引位置
int index = -1;
for (int i = 0; i < students.length; i++) {
//2.2 如果对象数组有null,表示找到存在的索引
if(students[i] == null){
//2.3 记录索引位置
index = i;
//2.4 接受本次查找
break;
}
}
//3.根据存放的结果返回给业务员
if(index==-1){
//3.1表示学生数组对象已放满,添加失败
return false;
}else{
//3.2 没有满可以添加学生,添加成功
students[index] = student;
return true;
}
}