使用ArrayList编写一个学生信息管理系统:
实现增删改查
//代码实现
import java.util.Scanner;
import java.util.ArrayList;
class StudentManageSystem
{
public static void main(String[] args)
{
ArrayList<Student> list = new ArrayList<Student>();
init(list);
}
public static void menu(){
//menu
System.out.println("欢迎进入学生管理系统:");
System.out.println("请输入:a--增加学生");
System.out.println("请输入:r--删除学生");
System.out.println("请输入:u--修改学生");
System.out.println("请输入:s--查询学生");
System.out.println("请输入:S--查询所有学生");
System.out.println("请输入:L--查询集合长度");
System.out.println("请输入:e--退出系统");
System.out.println("请输入-a-r-u-s-S-L-e-");
}
public static void init(ArrayList<Student> list){
while(true){
menu();
Scanner sc = new Scanner(System.in);
int key = sc.next().charAt(0);
if(key == 'a'){
add(list);
}else if(key == 'r'){
if(list.size()>0){
remove(list);
}else{
System.out.println("集合为空,无法删除");
}
}else if(key == 'u'){
if(list.size()>0){
update(list);
}else{
System.out.println("集合为空,无法修改");
}
}else if(key == 's'){
if(list.size()>0){
search(list);
}else{
System.out.println("集合为空");
}
}else if(key == 'S'){
if(list.size()>0){
show(list);
}else{
System.out.println("集合为空");
}
}else if(key == 'L'){
lengthOfList(list);
}else if(key == 'e'){
sc.close();
System.out.println("退出系统,欢迎下次光临");
break;
}else{
System.out.println("输入有误,请重新输入");
}
}
}
public static void add(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名:");
String stuName = sc.next();
System.out.println("请输入年龄:");
int stuAge = sc.nextInt();
System.out.println("请输入学号:");
int stuId = sc.nextInt();
Student stu = new Student();
stu.name = stuName;
stu.age = stuAge;
stu.id = stuId;
list.add(stu);
System.out.println("学生增加成功!!!!!!!!!!!!!!!!!!!");
}
public static void remove(ArrayList<Student> list){
//删除指定学号的学生
System.out.println("输入需要删除学生的学号:");
Scanner sc = new Scanner(System.in);
int removeId = sc.nextInt();
//遍历
for(int i=0;i<list.size();i++){
if(list.get(i).id == removeId){
list.remove(i);
System.out.println("学生删除成功!!!!!!!!!!!!!!!!!!!");
break;
}else{
continue;
}
}
}
public static void update(ArrayList<Student> list){
System.out.println("输入需要修改学生的学号:");
Scanner sc = new Scanner(System.in);
int updateId = sc.nextInt();
//遍历
for(int i=0;i<list.size();i++){
if(list.get(i).id == updateId){
System.out.println("输入需要修改学生的姓名:");
String updateName = sc.next();
System.out.println("输入需要修改学生的年龄");
int updateAge = sc.nextInt();
System.out.println("输入需要修改学生的学号");
int updateId_final = sc.nextInt();
list.get(i).name = updateName;
list.get(i).age = updateAge;
list.get(i).id = updateId_final;
System.out.println("学生信息修改成功!!!!!!!!!!!!!!!!!!!");
break;
}else{
continue;
}
}
}
public static void search(ArrayList<Student> list){
System.out.println("输入需要查询学生的学号:");
Scanner sc = new Scanner(System.in);
int searchId = sc.nextInt();
//遍历
for(int i=0;i<list.size();i++){
if(list.get(i).id == searchId){
System.out.println("学生姓名:"+list.get(i).name+"\n学生年龄:"+list.get(i).age+"\n学生学号:"+list.get(i).id);
System.out.println("\n指定学生信息查询成功!!!!!!!!!!!!!!!!!!!");
break;
}else{
continue;
}
}
}
public static void show(ArrayList<Student> list){
//遍历
for(int i=0;i<list.size();i++){
System.out.println("学生姓名:"+list.get(i).name+"\n学生年龄:"+list.get(i).age+"\n学生学号:"+list.get(i).id);
}
System.out.println("学生信息查询成功!!!!!!!!!!!!!!!!!!!");
}
public static void lengthOfList(ArrayList<Student> list){
int res = list.size();
System.out.println("集合长度为:"+res);
System.out.println("集合长度查询成功!!!!!!!!!!!!!!!!!!!");
}
}
class Student
{
String name;
int age;
int id;
}