import java.util.Scanner;
import java.util.ArrayList;
public class StudentTest {
public static void main(String[] args) {
System.out.println("-------------欢迎来到学生系统--------------");
System.out.println("0.添加学生");
System.out.println("1.删除学生");
System.out.println("2.修改学生");
System.out.println("3.查看学生");
System.out.println("4.退出系统");
ArrayList<Student> al = new ArrayList<Student>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入您的选择:");
int select = sc.nextInt();
switch (select) {
case 0:
addstudent(al);
break;
case 1:
deletestudent(al);
break;
case 2:
updatestudent(al);
case 3:
lookstudent(al);
break;
case 4:
exitstudent(al);
break;
default:
System.out.println("您的选择有误,检查");
}
}
}
//退出系统
private static void exitstudent(ArrayList<Student> al) {
System.out.println("--------您已退出--------");
System.exit(0);
}
//查看学生信息
private static void lookstudent(ArrayList<Student> al) {
if (al.isEmpty()) {
System.out.println("当前无学生信息,请先添加");
} else {
System.out.println("学号\t姓名\t年龄");
for (Student stu : al) {
System.out.println(stu.getId() + ", " + stu.getName() + ", " + stu.getAge());
}
}
}
//根据学号修改学生信息
private static void updatestudent(ArrayList<Student> al) {
if (al.isEmpty()) {
System.out.println("当前无学生信息,请先添加");
} else {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
String id = sc.next();
int result = index(al, id);
if (result == -1) {
System.out.println("无此学生信息,请检查");
} else {
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入年龄:");
int age = sc.nextInt();
Student sutdent = new Student(id, name, age);
al.set(result, sutdent);
System.out.println("修改成功");
}
}
}
//删除学生
private static void deletestudent(ArrayList<Student> al) {
if (al.isEmpty()) {
System.out.println("当前无学生信息,请先添加");
} else {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = sc.next();
int result = index(al, id);
if (result == -1) {
System.out.println("无此学生信息,请检查");
} else {
al.remove(result);
System.out.println("删除成功");
}
}
}
//添加学生
private static void addstudent(ArrayList<Student> al) {
Scanner sc = new Scanner(System.in);
String id;
while (true) {
System.out.println("请输入学号");
id = sc.next();
int result = index(al, id);
if (result == -1) {
break;
} else {
System.out.println("学号重复,请检查");
}
}
System.out.println("请输入学生姓名:");
String name = sc.next();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
Student student = new Student(id, name, age);
al.add(student);
System.out.println("添加成功");
}
//检查学号的唯一性
private static int index(ArrayList<Student> al, String id) {
int index = -1;
for (int i = 0; i < al.size(); i++) {
if (id.equals(al.get(i).getId())) {
index = i;
}
}
return index;
}
}
//学生类JavaBean
class Student {
private String id;
private String name;
private int age;
public Student() {
}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}