案例需求
完成一个综合案例:学生管理系统!该系统主要功能如下:
添加学生:通过键盘录入学生信息,添加到集合中
删除学生:通过键盘录入要删除学生的学号,将该学生对象从集合中删除
修改学生:通过键盘录入要修改学生的学号,将该学生对象其他信息进行修改
查看学生:将集合中的学生对象信息进行展示
退出系统:结束程序
项目演示:
实现步骤:
(1):定义学生类,包含以下成员变量
学生类: Student成员变量: 学号:sid
姓名:name
年龄:age
生日:birthday 构造方法:
无参构造
带四个参数的构造成员方法:
每个成员变量对应给出get/set方法
(2):主界面的代码编写
(3):添加学生功能代码的编写
(4):查找学生功能代码的编写
(5):删除学生功能代码的编写
(6):修改学生功能代码的编写
代码如下:
学生类:
package com.company;
/*学生类Alt+Insert 根据自己的需要进行选择*/public class Student {//学号private String sid;//姓名private String name;//年龄private String age;//生日private String birthday;public Student() {}public Student(String sid, String name, String age, String birthday) {this.sid = sid;this.name = name;this.age = age;this.birthday = birthday;}public String getSid() {return sid;}public void setSid(String sid) {this.sid = sid;}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;}
}
主函数:
package com.company;
/*学生管理系统*/import java.util.ArrayList;
import java.util.Scanner;public class Main {/*1:用输出语句完成主界面的编写2:用Scanner实现键盘录入数据3:用switch语句完成操作的选择4:用循环完成再次回到主界面*/public static void main(String[] args) {//创建集合对象,用于存储学生数据ArrayList<Student> array = new ArrayList<Student>();//用循环再次回到主界面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("请输入你的选择");//用Scanner实现键盘录入数据Scanner sc = new Scanner(System.in);String line = sc.nextLine();//用switch语句完成操作的选择switch (line) {case "1":
// System.out.println("添加学生");addStudent(array);break;case "2":
// System.out.println("删除学生");deleteStudent(array);break;case "3":
// System.out.println("修改学生");updateStudent(array);break;case "4":// System.out.println("查看所有学生");findAllStudent(array);break;case "5":System.out.println("谢谢使用");
// break;System.exit(0);//JVM退出,退出虚拟机}}}//定义一个方法,用于添加学生信息public static void addStudent(ArrayList<Student>array){//键盘录入学生对象所需要的数据,显示提示信息,提示要输入何种信息Scanner sc = new Scanner(System.in);//为了让 sid 在循环外面被访问到,我们把它定义在循环外面String sid;//为了让程序能够回到这里,使用循环实现while (true) {System.out.println("请输入学生学号:");sid = sc.nextLine();boolean flag = isUsed(array, sid);if (flag) {System.out.println("你输入的学号已经被使用,请重新输入");}else {break;}}System.out.println("请输入学生姓名:");String name = sc.nextLine();System.out.println("请输入学生年龄:");String age = sc.nextLine();System.out.println("请输入学生生日:");String address = sc.nextLine();//创建学生对象,把键盘录入的数据赋值给学生对象的成员变量Student s = new Student();s.setSid(sid);s.setName(name);s.setAge(age);s.setBirthday(address);//将学生对象添加到集合中array.add(s);//给出添加成功提示System.out.println("添加学生成功");}//定义一个方法,判断学号是否被使用public static boolean isUsed(ArrayList<Student>array,String sid){//如果与集合中某一个学生学号相同,返回ture;如果不相同,返回falseboolean flag = false;for (int i=0;i<array.size();i++){Student s = array.get(i);if (s.getSid().equals(sid)){flag = true;break;}}return flag;}//定义一个方法,用于查看学生信息public static void findAllStudent(ArrayList<Student> array){//判断集合中是否有数据,如果没有显示提示信息if (array.size()==0){System.out.println("无学生信息,请添加学生信息再查询");//为了让程序不再往下执行return;}//显示表头信息//\t其实就是tab键的位置System.out.println("学号\t姓名\t\t年龄\t\t生日");//将集合中数据元素取出按照对应格式显示学生信息,年龄显示补充“岁”for (int i=0; i<array.size();i++) {Student s = array.get(i);System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "岁\t" + s.getBirthday());}}//定义一个方法,用于删除学生信息public static void deleteStudent(ArrayList<Student>array){//键盘录入要删除学生的学号,显示提示信息Scanner sc = new Scanner(System.in);System.out.println("请输入你要删除学生的学号:");String sid =sc.nextLine();//在删除/修改学生操作时,对学号是否存在进行判断//如果不存在,显示提示信息//如果存在,执行操作int index = -1;for (int i=0;i<array.size();i++){Student s = array.get(i);if (s.getSid().equals(sid)){index = i;break;}}if(index == -1){System.out.println("该学生信息不存在,请重新输入");}else {array.remove(index);//给出删除成功提示信息System.out.println("删除学生成功");}//遍历集合将对应学生对象从集合中删除for (int i=0;i<array.size();i++){Student s = array.get(i);if (s.getSid().equals(sid)){array.remove(i);break;}}}//定义一个方法,用于修改学生信息public static void updateStudent(ArrayList<Student>array){//键盘录入要修改的学生学号,显示提示信息Scanner sc = new Scanner(System.in);System.out.println("请输入你要修改的学生学号:");String sid = sc.nextLine();//键盘录入要修改的学生信息System.out.println("请输入新的学生姓名:");String name = sc.nextLine();System.out.println("请输入新的学生年龄:");String age = sc.nextLine();System.out.println("请输入新的学生生日:");String address = sc.nextLine();//创建学生对象Student s = new Student();s.setSid(sid);s.setName(name);s.setAge(age);s.setBirthday(address);//遍历集合修改对应的学生信息for (int i=0;i<array.size();i++){Student student = array.get(i);if (student.getSid().equals(sid)){array.set(i,s);break;}}//给出修改成功提示System.out.println("修改学生成功");}
}
注:本代码是根据观看的黑马程序员视频所写