Java基础:学生管理系统

包含增删查改功能

适用于新手开发

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;
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰逸.itbignyi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值