学生管理系统
构思:首先我们先创建一个Student类用来存储学生基本信息,让后创建ArrayList读取Student信息
定义循环,循环使用学生管理系统直到退出使用。循环内套用switch调用选项,根据键盘录入选项
1..添加学生
2.删除学生
3.修改学生
4.查询学生
5.退出
输入其他选项将重新从键盘录入
主方法
ArrayList<Student> list = new ArrayList<>();
loop: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("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose){
case "1" -> addStudent(list);
case "2" -> delStudent(list);
case "3" -> updateStudent(list);
case "4" -> queryStudent(list);
case "5" -> {
System.out.println("退出");
// break loop;
System.exit(0);//停止运行虚拟机
}
default -> System.out.println("没有这个选项");
}
}
}
1.添加学生
public static void addStudent(ArrayList<Student> list){
Student s = new Student();
Scanner sc = new Scanner(System.in);
String id = null;
while (true) {
System.out.println("请输入学生id");
id = sc.next();
boolean flag = contains(list, id);
if (flag){
System.out.println("id已经存在,请重新录入!");
}else {
s.setId(id);
break;
}
}
public static boolean contains(ArrayList<Student> list,String id){
// for (int i = 0; i < list.size(); i++) {
// Student stu = list.get(i);
// String sid = stu.getId();
// if (sid.equals(id)){
// return true;
// }
// }
// return false;
return getIndex(list,id) >= 0;
}
//通过id获取索引方法
public static int getIndex(ArrayList<Student> list,String id){
for (int i = 0; i < list.size(); i++) {
String sid = list.get(i).getId();
// String sid = stu.getId();
if (sid.equals(id)){
return i;
}
}
return -1;
}
}
2.删除学生
public static void delStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的ID");
String id = sc.next();
int index = getIndex(list, id);
if (index >= 0){
list.remove(index);
System.out.println("id为:" + id + "的学生删除成功");
}else {
System.out.println("id不存在,删除失败!");
}
}
3.修改学生
public static void updateStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改学生的id");
String id = sc.next();
int index = getIndex(list, id);
if (index == -1){
System.out.println("要修改的"+ id + "不存在");
return;
}
//获取要修改的学生对象
Student stu = list.get(index);
System.out.println("请输入要修改的学生姓名");
String newName = sc.next();
stu.setName(newName);
System.out.println("请输入要修改的学生年龄");
int newAge = sc.nextInt();
stu.setAge(newAge);
System.out.println("请输入要修改的学生家庭住址");
String newAddress = sc.next();
stu.setAddress(newAddress);
System.out.println("学生信息修改成功!");
}
4.查询学生
public static void queryStudent(ArrayList<Student> list){
if (list.size() == 0){
System.out.println("当前没有学生信息,请添加后在查询");
return;
}
System.out.println("id\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.getAddress());
}
}
完整代码
package StudentManagementSystem;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentSystem {
public static void /*StudentSystem()*/main(String[] asge) {
// 定义一个集合调用Student对象来存储学生信息
ArrayList<Student> list = new ArrayList<>();
// 使用while循环遍历 loop是给循环赋值名称,以便break跳出
loop: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("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose){
case "1" -> addStudent(list);
case "2" -> delStudent(list);
case "3" -> updateStudent(list);
case "4" -> queryStudent(list);
case "5" -> {
System.out.println("退出");
// break loop;
System.exit(0);//停止运行虚拟机
}
default -> System.out.println("没有这个选项");
}
}
}
//添加学生信息
public static void addStudent(ArrayList<Student> list){
Student s = new Student();
Scanner sc = new Scanner(System.in);
String id = null;
while (true) {
System.out.println("请输入学生id");
id = sc.next();
boolean flag = contains(list, id);
if (flag){
System.out.println("id已经存在,请重新录入!");
}else {
s.setId(id);
break;
}
}
System.out.println("请输入学生姓名");
String name = sc.next();
s.setName(name);
System.out.println("请输入学生年龄");
int age = sc.nextInt();
s.setAge(age);
System.out.println("请输入学生家庭住址");
String address = sc.next();
s.setAddress(address);
list.add(s);
System.out.println("添加成功!");
}
//删除学生
public static void delStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的ID");
String id = sc.next();
int index = getIndex(list, id);
if (index >= 0){
list.remove(index);
System.out.println("id为:" + id + "的学生删除成功");
}else {
System.out.println("id不存在,删除失败!");
}
}
//修改学生
public static void updateStudent(ArrayList<Student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改学生的id");
String id = sc.next();
int index = getIndex(list, id);
if (index == -1){
System.out.println("要修改的"+ id + "不存在");
return;
}
//获取要修改的学生对象
Student stu = list.get(index);
System.out.println("请输入要修改的学生姓名");
String newName = sc.next();
stu.setName(newName);
System.out.println("请输入要修改的学生年龄");
int newAge = sc.nextInt();
stu.setAge(newAge);
System.out.println("请输入要修改的学生家庭住址");
String newAddress = sc.next();
stu.setAddress(newAddress);
System.out.println("学生信息修改成功!");
}
//查询学生
public static void queryStudent(ArrayList<Student> list){
if (list.size() == 0){
System.out.println("当前没有学生信息,请添加后在查询");
return;
}
System.out.println("id\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.getAddress());
}
}
//判断id是否存在
public static boolean contains(ArrayList<Student> list,String id){
// for (int i = 0; i < list.size(); i++) {
// Student stu = list.get(i);
// String sid = stu.getId();
// if (sid.equals(id)){
// return true;
// }
// }
// return false;
return getIndex(list,id) >= 0;
}
//通过id获取索引方法
public static int getIndex(ArrayList<Student> list,String id){
for (int i = 0; i < list.size(); i++) {
String sid = list.get(i).getId();
// String sid = stu.getId();
if (sid.equals(id)){
return i;
}
}
return -1;
}
}
这是Student对象
package StudentManagementSystem;
public class Student {
//id name age address
// 学生ID
private String id;
// 学生姓名
private String name;
// 学生年龄
private int age;
// 学生家庭住址
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
这是运行结果图