效果展示
添加学生信息
查看添加结果
修改学生信息(根据学号)并查看
根据学号删除学生信息
退出
代码
studentManager类
package com.cfqp;
import java.util.ArrayList;
import java.util.Scanner;
public class studentManager {
public static void main(String[] args) {
ArrayList<Student> studentArrayList = 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(studentArrayList);
System.out.println("添加学生成功");
break;
case "2":
deleteStudent(studentArrayList);
System.out.println("删除学生成功");
break;
case "3":
alterStudent(studentArrayList);
System.out.println("修改学生成功");
break;
case "4":
System.out.println("学号 姓名 年龄 住址");
shoWStudent(studentArrayList);
break;
case "5":
System.out.println("谢谢使用");
System.exit(0);
}
}
}
public static void addStudent(ArrayList<Student> studentArrayList) {
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 stu = new Student(age, name, sid, address);
studentArrayList.add(stu);
}
public static void deleteStudent(ArrayList<Student> studentArrayList) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学号");
String sid = sc.nextLine();
for(int i=0;i<studentArrayList.size();i++) {
if(studentArrayList.get(i).getSid().equals(sid)) {
studentArrayList.remove(i);
}
}
}
public static void alterStudent(ArrayList<Student> studentArrayList) {
System.out.println("输入您要修改的学号");
Scanner sc = new Scanner(System.in);
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();
for (int i = 0; i < studentArrayList.size(); i++) {
if (studentArrayList.get(i).getSid().equals(sid)) {
studentArrayList.get(i).setName(name);
studentArrayList.get(i).setAge(age);
studentArrayList.get(i).setAddress(address);
}
}
}
public static void shoWStudent(ArrayList<Student> studentArrayList) {
for (int i = 0; i < studentArrayList.size(); i++) {
System.out.print(studentArrayList.get(i).getSid()+" ");
System.out.print(studentArrayList.get(i).getName()+" ");
System.out.print(studentArrayList.get(i).getAge()+" ");
System.out.println(studentArrayList.get(i).getAddress()+" ");
}
}
}
Student类
package com.cfqp;
public class Student {
private String age;
private String name;
private String sid;
private String address;
public Student() {
}
public Student(String age, String name, String sid, String address) {
this.age = age;
this.name = name;
this.sid = sid;
this.address = address;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student(String sid, String address) {
this.sid = sid;
this.address = address;
}
}