View 视图层 展示数据 获取用户的请求数据
Controller 控制器层 功能调度
Model 模型层数据相关的处理
SQL 数据库
Global
import java.util.ArrayList;
/**
* @author: 阿斋
* @Date Created in 2024/3/14 && 10:44
* @Description: 全局类 定义一些功能性的方法
*/
public class Global {
public static int stuID = 1;
public static ArrayList<Student> stuList = new ArrayList<>();
private Global() {
}
public static void initStuList() {
stuList.add(new Student(stuID++,"ZhangSan","1",19,99));
stuList.add(new Student(stuID++,"LiSi","0",17,100));
stuList.add(new Student(stuID++,"WangWu","1",18,39));
stuList.add(new Student(stuID++,"ZhaoLiu","0",11,100));
stuList.add(new Student(stuID++,"NiuQi","1",19,99));
}
}
StuController
import java.util.ArrayList;
/**
* @author: 阿斋
* @Date Created in 2024/3/14 && 10:43
* @Description: 功能调度 控制器
*/
public class StuController {
private StuModel sm = new StuModel();
/**
* 总控制器
* @param a
*/
public void action(int a) {
switch (a){
case 1:
//定义一个二级页面
//通过二级页面获取一个查询请求数据
int sa = StuPage.selShowBy();
//查询功能调度
selAction(sa);
break ;
case 2:
//添加
//(添加一个,添加多个) 当我输入的over作为学生姓名的时候,我就不再继续添加
//获取一个要添加的学员对象
// Student stuAdd = StuPage.getAddStu();
//将要添加的学员对象传递给Model层来实现添加
// boolean b = sm.doAddStu(StuPage.getAddStu());
while (sm.doAddStu(StuPage.getAddStu())) {
;
}
// System.out.println("添加");
break ;
case 3:
// System.out.println("修改");
//获取要修改的学员ID
int editStuID = StuPage.getStuID();
//通过ID查询出要修改的学员信息
ArrayList<Student> editStu = sm.getByStuID(editStuID);
//在编辑页面中显示原有的学员信息,并且等待输入新的信息
//通过编辑页面编辑之后得到一个新的对象
Student stuNew = StuPage.editStu(editStu.get(0));
//更新学员信息
sm.doUpdateStu(stuNew);
break ;
case 4:
// System.out.println("删除");
//删除
int delID = StuPage.getStuID();
//获取要删除的学员ID
//输出确认删除信息 Y:确认 N:取消
ArrayList<Student> delStu = sm.getByStuID(delID);
if(!delStu.isEmpty()){
StuPage.showStuList(delStu);
//true 确认删除 false 取消
// boolean delB = StuPage.delAlert();
if(StuPage.delAlert()){
sm.doDelStuByID(delID);
}
}else {
StuPage.failed("要删除的学员信息不存在!");
}
//如果是Y的话就删除 N的话就什么都不做
break ;
case 0:
System.out.println("退出");
System.exit(0);
break ;
}
}
/**
* 查询功能的调度器
* @param sa
*/
private void selAction(int sa) {
switch (sa){
case 1://全部
//获取所有的学员信息
ArrayList<Student> allStuList = sm.getAll();
//通过页面输出信息
StuPage.showStuList(allStuList);
break;
case 2://学号
int stuID = StuPage.getStuID();
ArrayList<Student> stuListByID = sm.getByStuID(stuID);
StuPage.showStuList(stuListByID);
break;
case 3://姓名
String stuName = StuPage.getStuName();
ArrayList<Student> stuListByName = sm.getByStuName(stuName);
StuPage.showStuList(stuListByName);
break;
case 4://性别
String stuSex = StuPage.getStuSex();
ArrayList<Student> stuListBySex = sm.getByStuSex(stuSex);
StuPage.showStuList(stuListBySex);
break;
case 5://年龄
int stuAge = StuPage.getStuAge();
ArrayList<Student> stuListByAge = sm.getByStuAge(stuAge);
StuPage.showStuList(stuListByAge);
break;
case 6://成绩
Float stuScore = StuPage.getStuScore();
ArrayList<Student> stuListByScore = sm.getByStuScore(stuScore);
StuPage.showStuList(stuListByScore);
break;
case 0://退出查询
break;
}
}
}
Student
/**
* @author: 阿斋
* @Date Created in 2024/3/14 && 10:42
* @Description:学生类
*/
public class Student {
private int id;
private String name;
private String sex;
private int age;
private float score;
public Student(int id, String name, String sex, int age, float score) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
public Student() {
}
public Student(String name, String sex, int age, float score) {
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
StuManagerMain
import com.sun.jdi.PathSearchingVirtualMachine;
import java.util.ArrayList;
/**
* @author: 阿斋
* @Date Created in 2024/3/14 && 10:44
* @Description: 主类 测试类
*/
public class StuManagerMain {
//实例化一个控制器的对象
private static StuController sc = new StuController();
public static void main(String[] args) {
//测试类主类方法
// ArrayList<Student> stuList = new ArrayList<>();
//调用Global类当中的测试数据初始化方法来初始化 学生数据集合
Global.initStuList();
//输出欢迎页面,并让用户选择要操作的功能,获取到功能对应的序号
while (true){
int a = StuPage.welcome();
sc.action(a);
}
}
}
StuModel
import java.util.ArrayList;
/**
* @author: 阿斋
* @Date Created in 2024/3/14 && 10:43
* @Description:数据处理 模型层
*/
public class StuModel {
/**
* 获取所有的学生信息
* @return 所有的学生信息集合
*/
public ArrayList<Student> getAll() {
return Global.stuList;
}
/**
* 根据学员的ID查询数据
* @param stuID 学生ID
* @return 查询结果集合
*/
public ArrayList<Student> getByStuID(int stuID) {
ArrayList<Student> resList = new ArrayList<>();
for(Student stu : Global.stuList){
if(stuID == stu.getId()){
resList.add(stu);
}
}
return resList;
}
/**
* 根据姓名查询
* @param stuName 学生姓名
* @return 学生姓名集合
*/
public ArrayList<Student> getByStuName(String stuName) {
ArrayList<Student> resList = new ArrayList<>();
for(Student stu : Global.stuList){
if(stuName.equals(stu.getName())){
resList.add(stu);
}
}
return resList;
}
/**
* 根据性别查询
* @param stuSex 学生性别
* @return 学生性别集合
*/
public ArrayList<Student> getByStuSex(String stuSex) {
ArrayList<Student> resList = new ArrayList<>();
for(Student stu : Global.stuList){
if(stuSex.equals(stu.getSex())){
resList.add(stu);
}
}
return resList;
}
/**
* 根据年龄查询
* @param stuAge 学生年龄
* @return 学生年龄集合
*/
public ArrayList<Student> getByStuAge(int stuAge) {
ArrayList<Student> resList = new ArrayList<>();
for(Student stu : Global.stuList){
if(stuAge == stu.getAge()){
resList.add(stu);
}
}
return resList;
}
/**
* 根据成绩查询
* @param stuScore 学生成绩
* @return 学生成绩集合
*/
public ArrayList<Student> getByStuScore(Float stuScore) {
ArrayList<Student> resList = new ArrayList<>();
for(Student stu : Global.stuList){
if(stuScore == stu.getScore()){
resList.add(stu);
}
}
return resList;
}
/**
* 添加学员信息
* @param stuAdd 一个要被添加的学员信息,不包含ID的学员信息
* 如果为null 说明结束添加
* @return 继续添加返回true 结束添加返回false
*/
public boolean doAddStu(Student stuAdd) {
if(stuAdd == null){
return false;
}
//通过实例化对象,让ID自增 获取一个完整的学员对象
Student stu = new Student(
Global.stuID++,
stuAdd.getName(),
stuAdd.getSex(),
stuAdd.getAge(),
stuAdd.getScore()
);
return Global.stuList.add(stu);
}
/**
* 根据学生ID删除学员
* @param delID 要删除的学员ID
*/
public void doDelStuByID(int delID) {
for (int i = 0; i < Global.stuList.size(); i++) {
if(delID == Global.stuList.get(i).getId()){
Global.stuList.remove(i);
return;
}
}
}
/**
* 更新学员
* @param stuNew 要更新的学员信息
*/
public void doUpdateStu(Student stuNew) {
for (int i = 0; i < Global.stuList.size(); i++) {
if(stuNew.getId() == Global.stuList.get(i).getId()){
Global.stuList.set(i,stuNew);
}
}
}
}
StuPage
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author: 阿斋
* @Date Created in 2024/3/14 && 10:43
* @Description: 页面类 视图层 数据的展示与请求数据的获取
*/
public class StuPage {
//构造方法私有化 禁止在类外对其进行实例化
private StuPage(){}
/**
* 欢迎页面,获取用户的操作数据
* @return 用户的操作意图
*/
public static int welcome() {
System.out.println("************");
System.out.println("学生信息管理系统");
System.out.println("*1 - 查询");
System.out.println("*2 - 添加");
System.out.println("*3 - 修改");
System.out.println("*4 - 删除");
System.out.println("*0 - 退出");
System.out.println("************");
int a;
do{
System.out.println("请输入正确的功能序号:");
a = new Scanner(System.in).nextInt();
}while (a < 0 || a > 4);
return a;
}
/**
* 查询二级菜单页面
* @return
*/
public static int selShowBy() {
System.out.println("************");
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("*0 - 退出");
System.out.println("************");
int a;
do{
System.out.println("请输入正确的查询功能序号:");
a = new Scanner(System.in).nextInt();
}while (a < 0 || a > 6);
return a;
}
/**
* 显示学生信息集合(多个)
* @param stuList 学员信息集合
*/
public static void showStuList(ArrayList<Student> stuList) {
String[] sex = {"girl", "boy"};
// System.out.printf("┌┬┐├┼┤ ┌ ┬ ┐ ├ ┼ ┤ └ ┴ ┘ ─ │ ");
System.out.printf("┌─────┬────────────┬──────┬──────┬──────┐\n");
System.out.printf("│%-5s│%-12s│%-6s│%-6s│%-6s│\n", "ID", "NAME", "SEX", "AGE", "SCORE");
for(Student stu : stuList){
System.out.printf("├─────┼────────────┼──────┼──────┼──────┤\n");
System.out.printf("│%-5s│%-12s│%-6s│%-6s│%-6s│\n",
stu.getId(),
stu.getName(),
sex[Integer.parseInt(stu.getSex())],
stu.getAge(),
stu.getScore());
}
// System.out.printf("├─────┼────────────┼──────┼──────┼──────┤");
System.out.printf("└─────┴────────────┴──────┴──────┴──────┘\n");
}
public static int getStuID() {
System.out.println("请输入学员ID:");
return new Scanner(System.in).nextInt();
}
public static String getStuName() {
System.out.println("请输入要查询的学员姓名:");
return new Scanner(System.in).nextLine();
}
public static String getStuSex() {
System.out.println("请输入要查询的学员性别:");
return new Scanner(System.in).nextLine();
}
public static int getStuAge() {
System.out.println("请输入要查询的学员年龄");
return new Scanner(System.in).nextInt();
}
public static Float getStuScore() {
System.out.println("请输入要查询的学员成绩");
return new Scanner(System.in).nextFloat();
}
/**
* 获取一个学员对象,用于添加到学生集合中
* @return 继续添加——用户输入的学员对象(不完整的学员对象,不包含ID)
* 不想添加——用户输入over 返回null
*/
public static Student getAddStu() {
System.out.println("请输入要添加的学生姓名:");
String stuName = new Scanner(System.in).nextLine();
if("over".equals(stuName)){
return null;
}
System.out.println("请输入要添加的学生性别:");
String stuSex = new Scanner(System.in).nextLine();
System.out.println("请输入要添加的学生年龄:");
int stuAge = new Scanner(System.in).nextInt();
System.out.println("请输入要添加的学生成绩");
float stuScore = new Scanner(System.in).nextFloat();
return new Student(stuName,stuSex,stuAge,stuScore);
}
/**
* 操作失败页面
* @param info 失败信息
*/
public static void failed(String info) {
System.out.println("操作失败" + info);
}
/**
* 获取是否要删除
* @return true 确认删除 false 取消删除
*/
public static boolean delAlert() {
System.out.println("Y:确认删除 / N:取消删除");
switch (new Scanner(System.in).next()){
case "Y":
case "y":
return true;
case "N":
case "n":
return false;
default:
System.out.println("输入非法,取消删除");
}
return false;
}
/**
* 编辑页面 包含原有信息的内容
* @param student 原有信息对象
* @return 新的信息对象
*/
public static Student editStu(Student student) {
System.out.println("请输入要删除的学生姓名(" + student.getName() + "): ");
String stuName = new Scanner(System.in).nextLine();
System.out.println("请输入要删除的学生性别(" + student.getSex() + "): ");
String stuSex = new Scanner(System.in).nextLine();
System.out.println("请输入要删除的学生年龄(" + student.getAge() + "): ");
int stuAge = new Scanner(System.in).nextInt();
System.out.println("请输入要删除的学生成绩(" + student.getScore() + "): ");
float stuScore = new Scanner(System.in).nextFloat();
return new Student(student.getId(),stuName,stuSex,stuAge,stuScore);
}
// /**
// * 获取要删除的学员ID
// * @return 学员ID
// */
// public static int getDelID() {
// System.out.println("请输入要删除的学员ID:");
// return new Scanner(System.in).nextInt();
// }
}