使用List集合实现学员信息管理系统,功能要求
– 显示菜单选择功能
– 实现学员信息的录入,查询,修改,删除,统计等功能。
– 统计功能需要统计集合中所有学员的平均考试成绩
功能菜单如下:
1. 录入学员信息
2. 根据学号查询学员信息
3. 修改学员信息
4. 根据学号删除学员信息
5. 统计学员平均考试成绩
6. 退出系统
这个功能我用三个类实现,1.测试类Test 2.StudentManage 3.Student//测试类
public class Test {
public static void main(String[] args) {
StudentManage manage=new StudentManage();
manage.menu();
}
}
public class Student {
private String num;
private String name;
private int age;
private int score;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
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 int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "学号为:" + num + ", 名字是:" + name + ", 年龄是:" + age
+ ",成绩为:" + score + "]";
}
}
public class StudentManage {
private Scanner input;
private ArrayList<Student> list;
public StudentManage(){
list=new ArrayList<Student>();
input=new Scanner(System.in);
}
public void menu(){
System.out.println("0.显示所有学员信息");
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("---------------------");
System.out.println("请选择菜单");
String choose=input.next();
if ("0".equals(choose)) {
show();
}else if ("1".equals(choose)) {
add();
}else if("2".equals(choose)){
find();
}else if("3".equals(choose)){
modify();
}else if("4".equals(choose)){
delete();
}else if("5".equals(choose)){
getAverageScore();
}else if("6".equals(choose)){
System.exit(0);
}else {
System.out.println("请输入正确选项.......");
menu();
}
}
private void show() {
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i).toString());
}
menu();
}
private void delete() {
System.out.println("请输入学号");
String num=input.next();
if (checkNum(num)) {
Student student=getStudentByNum(num);
if (student!=null) {
list.remove(student);
}
menu();
}else {
System.out.println("该学号不存在.....");
menu();
}
}
private void getAverageScore() {
float scores=0;
for (int i = 0; i < list.size(); i++) {
scores+=list.get(i).getScore();
}
System.out.println("学生平均成绩为:"+scores/list.size());
menu();
}
private void modify() {
System.out.println("请输入要修改学生的学号");
String num=input.next();
if (checkNum(num)) {
Student student=getStudentByNum(num);
if (student!=null) {
System.out.println(student.toString());
modefyDetail(student);
}
}else {
System.out.println("该学号不存在.....");
menu();
}
}
private void modefyDetail(Student student) {
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("-------------------");
System.out.println("请输入要修改的菜单项");
String choose=input.next();
if ("1".equals(choose)) {
System.out.println("请输入新学号......");
String num=input.next();
if (checkNum(num)) {
System.out.println("该学号已存在,请重新输入");
modefyDetail(student);
}else {
student.setNum(num);
}
modefyDetail(student);
}else if("2".equals(choose)){
System.out.println("请输入新名字......");
String name=input.next();
student.setName(name);
modefyDetail(student);
}else if("3".equals(choose)){
System.out.println("请输入新年龄......");
int age=input.nextInt();
student.setAge(age);
modefyDetail(student);
}else if("4".equals(choose)){
System.out.println("请输入新成绩......");
int score=input.nextInt();
student.setScore(score);
modefyDetail(student);
}else if("5".equals(choose)){
menu();
}else {
System.out.println("请输入正确的菜单项......");
modefyDetail(student);
}
}
private void find() {
System.out.println("请输入学号");
String num=input.next();
if (checkNum(num)) {
Student student=getStudentByNum(num);
if (student!=null) {
System.out.println(student.toString());
menu();
}
}else {
System.out.println("该学号不存在.....");
menu();
}
}
public void add(){
System.out.println("请输入学号");
String num=input.next();
Student student=new Student();
student.setNum(num);
if (checkNum(num)) {
System.out.println("该学生已存在...");
menu();
}else {
System.out.println("请输入姓名");
String name=input.next();
System.out.println("请输入年龄");
int age=input.nextInt();
System.out.println("请输入成绩");
int score=input.nextInt();
student.setAge(age);
student.setName(name);
student.setScore(score);
list.add(student);
menu();
}
}
public boolean checkNum(String num){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getNum().equals(num)) {
return true;
}
}
return false;
}
public Student getStudentByNum(String num){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getNum().equals(num)) {
return list.get(i);
}
}
return null;
}
}