学生信息管理系统
具备基础增删改查功能
运行截图:
StudentManger类
public class StudentManager {
private String id ;
private String name ;
private int age ;
private String birth ;
public StudentManager() {
}
public StudentManager(String id, String name, int age, String birth) {
this.id = id;
this.name = name;
this.age = age;
this.birth = birth;
}
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;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
}
Test类:
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList<StudentManager> list = new ArrayList<>();
int choice;
Scanner sc = new Scanner(System.in);
do{
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("请输入您的选择:");
choice = sc.nextInt();
switch (choice){
case 1:
System.out.println("请输入学生学号:");
String id = sc.next();
if(addId(list, id)){
System.out.println("您输入的学号已被占用,请重新输入!");
}
else {
System.out.println("请输入学生姓名:");
String name = sc.next();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
System.out.println("请输入学生生日:");
String birth = sc.next();
StudentManager stu = new StudentManager(id,name,age,birth);
list.add(stu);
System.out.println("添加成功!");
}
break;
case 2:
System.out.println("请输入您要删除的学生学号:");
String deleteId = sc.next();
if(!findId(list, deleteId)){
System.out.println("查无信息,请添加后重试!");
}
else {
for (int i = 0; i < list.size(); i++) {
StudentManager stu1 = list.get(i);
if(deleteId.equals(stu1.getId())){
list.remove(i);
i--;
}
}
System.out.println("删除成功!");
}
break;
case 3:
System.out.println("请输入您要修改信息的学生学号:");
String exchangeId = sc.next();
if(!findId(list, exchangeId)){
System.out.println("查无信息,请添加后重试!");
}
else {
for (int i = 0; i < list.size(); i++) {
StudentManager stu1 = list.get(i);
if(exchangeId.equals(stu1.getId())){
System.out.println("请输入新的学生姓名:");
String newName = sc.next();
System.out.println("请输入新的学生年龄:");
int newAge = sc.nextInt();
System.out.println("请输入新的学生生日:");
String newBirth = sc.next();
StudentManager newStu = new StudentManager(exchangeId,newName,newAge,newBirth);
list.set(i,newStu);
i--;
}
}
System.out.println("修改成功!");
}
break;
case 4:
if(list.isEmpty()){
System.out.println("查无信息,请添加后重试!");
}
else {
System.out.println("学号\t\t姓名\t年龄\t生日");
for (int i = 0; i < list.size(); i++) {
StudentManager showStu = list.get(i);
System.out.println(showStu.getId() +"\t"+ showStu.getName() +"\t"+ showStu.getAge() +"\t"+ showStu.getBirth());
}
}
break;
case 5:
System.out.println("感谢您的使用,再见!");
System.exit(0); //终止正在运行的java虚拟机
break;
default:
System.out.println("请检查您输入的内容!");
break;
}
}while(true);
}
private static boolean addId(ArrayList<StudentManager> list, String id) {
boolean result = false;
for (int i = 0; i < list.size(); i++) {
StudentManager stu1 = list.get(i);
if (id.equals(stu1.getId())) {
result = true;
break;
}
}
return result;
}
private static boolean findId(ArrayList<StudentManager> list, String id) {
boolean result = false;
for (int i = 0; i < list.size(); i++) {
StudentManager stu1 = list.get(i);
if (id.equals(stu1.getId())) {
result = true;
break;
}
}
return result;
}
}