一、学生类
首先创建一个学生类,定义学号姓名年龄居住地
public class Student {
private String id;
private String name;
private String age;
private String address;
//构造函数
public Student(String id, String name, String age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
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 getAddress() {return address;}
public void setAddress(String address) { this.address = address; }
二、测试类
1、界面实现
在实现类里面写界面格式,选项以及方法的实现
ArrayList<Student> array = new ArrayList<>();
while (true) {
System.out.println("-----------欢迎来到学生管理系统----------");
System.out.println("1、查询学生");
System.out.println("2、添加学生");
System.out.println("3、删除学生");
System.out.println("4、退出");
System.out.println("请输入你的选择:");
Scanner sc=new Scanner(System.in);
String choice=sc.nextLine();
2、功能选择
exit能够直接关闭java虚拟机
//选择
switch(choice){
case "1":
findAllStudent(array);
break;
case "2":
addAllStudent(array);
break;
case "3":
deleteAllStudent(array);
break;
case "4":
System.out.println("谢谢你的使用");
System.exit(0);
break;
default:
System.out.println("输入号码有误,请重新输入");
break;
}
3、方法实现
查询方法
public static void findAllStudent(ArrayList<Student> array){
if(array.size()==0){
System.out.println("无该学生信息,请重新输入");
return;
}
for(int x=0;x<array.size();x++){
Student s=array.get(x);
System.out.println("学号:"+s.getId()+"\t"+" 姓名:"+s.getName()+"\t"+"年龄:"+s.getAge()+"\t"+" 居住地:"+s.getAddress());
}
}
添加
public static void addAllStudent(ArrayList<Student>array){
Scanner sc=new Scanner(System.in);
String id;
while(true){
System.out.println("请输入学生学号:");
id=sc.next();
boolean flag=false;
for(int x=0;x<array.size();x++){
Student s=array.get(x);
if(s.getId().equals(id)){
flag=true;
break;
}
}
if(flag){
System.out.println("您输入的学号被占用,请重新输入");
}
else{
break;
}
}
System.out.println("请输入学生姓名:");
String name=sc.next();
System.out.println("请输入学生年龄:");
String age=sc.next();
System.out.println("请输入学生居住地:");
String address=sc.next();
Student s=new Student(id,name,age,address);
array.add(s);
System.out.println("添加学生成功");
}
删除
public static void deleteAllStudent(ArrayList<Student>array){
Scanner sc=new Scanner(System.in);
System.out.println("请输入您要删除的学生学号:");
String id=sc.nextLine();
int index=-1;
for(int x=0;x< array.size();x++){
Student s=array.get(x);
if(s.getId().equals(id)){
index=x;
break;
}
}
if(index==-1){
System.out.println("该学生不存在");
}
else{
array.remove(index);
System.out.println("删除成功");
}
}
版权声明:本文为博主作者:莓果兰竹原创文章,版权归属原作者,如果侵权,请联系我们删除!
原文链接:https://blog.csdn.net/m0_74319211/article/details/134883093