一、前言二、Student类三、主类四、运行截图五、主类源代码
一、前言采用ArrayList动态数组,实现学生的增加、删除、修改、查找、退出
(主要目的是增加对ArrayList的理解与使用)
学生类中封装四个成员变量(学号、姓名、性别、年龄),设置相应的get和set方法
public class Student {
//学号
private String number;
//姓名
private String name;
//性别
private String sex;
//年龄
private String age;
//get
public String getNumber() {
return number;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getAge() {
return age;
}
//set
public void setNumber(String number) {
this.number = number;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(String age) {
this.age = age;
}
}
如果用的编译器是IDEA,按ALT+INSERT可以快速生成四个封装属性的get和set方法
三、主类1、思维导图
孩子不懂事,瞎画着玩的
2、展示界面信息
单独写个showInfo()方法展示界面信息
//界面
public static void showInfo(){
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("请输入您的选择");
}
3、循环显示界面
switch( )语句,按1-5显示不同的功能
while(true){
//显示界面
showInfo();
String choice=scanner.nextLine();
switch (choice){
case "1"://添加学生信息
addStudent(studentArrayList);
break;
case "2"://删除学生信息
delStudent(studentArrayList);
break;
case "3"://修改学生信息
reviseStudent(studentArrayList);
break;
case "4"://查看所有学生信息
showStudent(studentArrayList);
break;
case "5":
System.out.println("感谢本次使用捏!");
System.exit(0);
}
}
4、添加学生信息
addStudent( ),studentArrayList .add( )方法,添加新学生信息
//添加学生
public static void addStudent(ArrayList studentArrayList){
Scanner sc=new Scanner(System.in);
System.out.println("请输入学生的学号");
String number=sc.nextLine();
System.out.println("请输入学生的姓名");
String name=sc.nextLine();
System.out.println("请输入学生的性别");
String sex=sc.nextLine();
System.out.println("请输入学生的年龄");
String age=sc.nextLine();
//创建学生对象
Student stu=new Student();
stu.setNumber(number);
stu.setName(name);
stu.setSex(sex);
stu.setAge(age);
studentArrayList.add(stu);
System.out.println("添加学生成功了捏!");
}
5、删除学生信息
根据想删除学生的学号进行遍历比对,采用equals( )方法
public static void delStudent(ArrayListstudentArrayList) { if(studentArrayList.size()==0){ System.out.println("都没人你删个番茄炒蛋锤!"); return; } boolean flag = false; Scanner sc = new Scanner(System.in); System.out.println("请输入删除学生的学号"); String delNum = sc.nextLine(); for (int i = 0; i < studentArrayList.size(); i++) { Student stu = studentArrayList.get(i); if (stu.getNumber().equals(delNum)) { studentArrayList.remove(i); System.out.println("删除成功了捏!"); break; }else System.out.println("查无此人捏!"); } }
6、修改学生信息
通过学生学号来判断是否可以进行修改,将修改后的内容存入新对象s(Student s=new Student),若找到该学号则使用studentArrayList .set( )方法
public static void reviseStudent(ArrayListstudentArrayList){ Scanner sc=new Scanner(System.in); System.out.println("请输入需要修改学生的学号"); String reviseNumber=sc.nextLine(); System.out.println("请输入新姓名"); String newName=sc.nextLine(); System.out.println("请输入新性别"); String newSex=sc.nextLine(); System.out.println("请输入新年龄"); String newAge=sc.nextLine(); //创建学生对象 Student s=new Student(); s.setNumber(reviseNumber); s.setName(newName); s.setSex(newSex); s.setAge(newAge); for(int i=0;i 7、查看所有学生
遍历输出即可,注意studentArrayList容量为0时的特殊情况
//查看学生 public static void showStudent(ArrayListstudentArrayList){ //判断是否为空 if(studentArrayList.size()==0){ System.out.println("还没有信息捏,请添加数据!"); return; } //表单栏 System.out.println("学号ttt姓名tt性别t年龄"); for (int index=0;index 四、运行截图 1、添加学生信息
2、查看全部学生啊这,显示不美观,因为没想到名字是五个字
3、修改学生信息
4、删除学生信息
如果继续按2,就会提示以下内容五、主类源代码
5、退出程序
import java.util.ArrayList; import java.util.Scanner; public class Manage { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); //创建学生集合 ArrayListstudentArrayList=new ArrayList<>(); while(true){ //显示界面 showInfo(); String choice=scanner.nextLine(); switch (choice){ case "1"://添加学生信息 addStudent(studentArrayList); break; case "2"://删除学生信息 delStudent(studentArrayList); break; case "3"://修改学生信息 reviseStudent(studentArrayList); break; case "4"://查看所有学生信息 showStudent(studentArrayList); break; case "5": System.out.println("感谢本次使用捏!"); System.exit(0); } } } //界面 public static void showInfo(){ 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("请输入您的选择"); } //添加学生 public static void addStudent(ArrayList studentArrayList){ Scanner sc=new Scanner(System.in); System.out.println("请输入学生的学号"); String number=sc.nextLine(); System.out.println("请输入学生的姓名"); String name=sc.nextLine(); System.out.println("请输入学生的性别"); String sex=sc.nextLine(); System.out.println("请输入学生的年龄"); String age=sc.nextLine(); //创建学生对象 Student stu=new Student(); stu.setNumber(number); stu.setName(name); stu.setSex(sex); stu.setAge(age); studentArrayList.add(stu); System.out.println("添加学生成功了捏!"); } //删除学生 public static void delStudent(ArrayList studentArrayList) { if(studentArrayList.size()==0){ System.out.println("都没人你删个番茄炒蛋锤!"); return; } boolean flag = false; Scanner sc = new Scanner(System.in); System.out.println("请输入删除学生的学号"); String delNum = sc.nextLine(); for (int i = 0; i < studentArrayList.size(); i++) { Student stu = studentArrayList.get(i); if (stu.getNumber().equals(delNum)) { studentArrayList.remove(i); System.out.println("删除成功了捏!"); break; }else System.out.println("查无此人捏!"); } } //修改学生 public static void reviseStudent(ArrayList studentArrayList){ Scanner sc=new Scanner(System.in); System.out.println("请输入需要修改学生的学号"); String reviseNumber=sc.nextLine(); System.out.println("请输入新姓名"); String newName=sc.nextLine(); System.out.println("请输入新性别"); String newSex=sc.nextLine(); System.out.println("请输入新年龄"); String newAge=sc.nextLine(); //创建学生对象 Student s=new Student(); s.setNumber(reviseNumber); s.setName(newName); s.setSex(newSex); s.setAge(newAge); for(int i=0;i studentArrayList){ //判断是否为空 if(studentArrayList.size()==0){ System.out.println("还没有信息捏,请添加数据!"); return; } //表单栏 System.out.println("学号ttt姓名tt性别t年龄"); for (int index=0;index



