该程序包含2个java文件,一个学生类Student.java,一个主方法StudentDemo.java,具体代码如下。
Student.java
package com.studentmange;
public class Student {
private String sid;
private String name;
private String age;
private String address;
public Student() {
}
public Student(String sid, String name, String age, String address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
StudentDemo.java
package com.studentmange;
import java.util.ArrayList;//导入ArrayList包
import java.util.Scanner;//导入Scanner输入包
public class StudentDemo {
public static void main(String[] args) {
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("┃━━━━━━━━━━━5━退出━━━━━━━━━ ┃");
System.out.println("┗━━━━━━━━━输入你的选择━━━━━━━┛");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
switch(line){
case "1":addstudent(array);break;
case "2":findallstudent(array);break;
case "3":updatestudent(array);break;
case "4":delete(array);break;
case "5":
System.out.println("感谢你的使用");
System.exit(0);
}
}
}
//添加学生信息
public static void addstudent(ArrayList<Student> array){
//ArrayList<Student> stu = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("输入学号");
String sid = sc.next();
System.out.println("输入名字");
String name = sc.next();
System.out.println("输入年龄");
String age = sc.next();
System.out.println("输入地址");
String address = sc.next();
Student stu = new Student();
stu.setSid(sid);
stu.setName(name);
stu.setAge(age);
stu.setAddress(address);
array.add(stu);
}
//查询学生信息
public static void findallstudent(ArrayList<Student> array){
if(array.size()==0){
System.out.println("该系统中没有学生元素");
}else{
System.out.println("学号 姓名 年龄 地址");
for (int i = 0; i < array.size(); i++) {
Student stu = array.get(i);
System.out.println(stu.getSid()+" "+stu.getName()+" "+stu.getAge()+" "+stu.getAddress());
}
}
}
//更新学生信息
public static void updatestudent(ArrayList<Student> array){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生ID");
String id = sc.next();
for (int i = 0; i < array.size(); i++) {
if(!array.get(i).getSid().equals(id)){
System.out.println("学号不存在");
}
}
for (int i = 0; i < array.size(); i++) {
if(array.get(i).getSid().equals(id)){
System.out.println("输入学号");
String sid = sc.next();
System.out.println("输入名字");
String name = sc.next();
System.out.println("输入年龄");
String age = sc.next();
System.out.println("输入地址");
String address = sc.next();
Student stu = new Student();
stu.setSid(sid);
stu.setName(name);
stu.setAge(age);
stu.setAddress(address);
array.set(i,stu); //修改对应的学生的信息
System.out.println("☛☛☛☛修改成功☜☜☜☜");
}else if (i==array.size() - 1) {
System.out.println("你输入的学号不存在");
}
}
}
//删除学生信息
public static void delete(ArrayList<Student> array){
System.out.println("请输入你要删除的学生的id");
Scanner sc = new Scanner(System.in);
String id = sc.next();
for (int i = 0; i < array.size(); i++) {
if(array.get(i).getSid().equals(id)){
array.remove(i);
System.out.println("☛☛☛☛删除成功☜☜☜☜");
}else if (i==array.size() - 1) {
System.out.println("你输入的学号不存在");
}
}
}
}