包含增删查改功能
适用于新手开发
package com.test;
import com.domain.Student;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
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("6.退出");
System.out.println("请输入选择");
int choice = sc.nextInt();
switch (choice) {
case 1:
addStudent(list);
break;
case 2:
deleteStudentById(list);
break;
case 3:
updateStudentInfoById(list);
break;
case 4:
queryStudentInfos(list);
break;
case 5:
findStudentById(list);
break;
case 6:
System.out.println("感谢使用");
System.exit(0);
break;
default:
System.out.println("输入有误");
break;
}
}
}
/*
* 查找学生
* */
private static void findStudentById(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查找的学生学号");
while (true) {
String findId = sc.next();
Student stu = new Student();
int index = getIndex(findId, list);
if (list.size()==0) {
}else {
System.out.println("现有学生信息如下:");
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println("学号\t\t\t姓名\t年龄\t生日");
System.out.println(student.getId()+"\t" +
"\t" +student.getName()+
"\t" +student.getAge()+
"\t" +student.getBirthday());
}
}if (index == -1) {
System.out.println("查无此人,请重新输入");
break;
}
}
}
/*
* 添加学生信息
* */
private static void addStudent(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号");
String id= null;
/*
添加学生信息
*/
while (true) {
id = sc.next();
int index = getIndex(id, list);
if (index == -1) {
break;
} else {
System.out.println("该学号已被占用,请重新输入");
}
}
System.out.println("请输入学生姓名");
String name = sc.next();
System.out.println("请输入学生年龄");
int age = sc.nextInt();
System.out.println("请输入学生生日");
String birthday = sc.next();
Student stu = new Student(id, name, age, birthday);
list.add(stu);
System.out.println("添加成功!");
}
/*
* 根据学号修改学生信息
* */
private static void updateStudentInfoById(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的学生学号");
String updateId = sc.next();
int index = getIndex(updateId, list);
if (index == -1) {
System.out.println("查无此人");
} else {
System.out.println("请输入姓名");
String name = sc.next();
System.out.println("请输入年龄");
int age = sc.nextInt();
System.out.println("请输入生日");
String birthday = sc.next();
//将新录入的学生信息分装为学生对象
Student stu = new Student(updateId, name, age, birthday);
list.set(index, stu);
} System.out.println("修改成功");
}
/*
* 根据学号删除集合中的学生
* */
private static void deleteStudentById(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学生学号");
String id = sc.next();
int index = getIndex(id, list);
if (index == -1) {
System.out.println("查无此人");
} else {
list.remove(index);
System.out.println("删除成功");
}
}
private static void queryStudentInfos(ArrayList<Student> list) {
if (list.size() == 0) {
System.out.println("查无此人,请添加");
} else {
System.out.println("学号\t\t\t姓名\t年龄\t生日");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
}
}
}
/*
根据学号,查找索引
*/
private static int getIndex(String id, ArrayList<Student> list) {
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
if (stu.getId().equals(id)) {
return i;
}
}
return -1;
}
}