package com.sise06;
public class Student {
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 void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.sise06;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManage {
public static void main(String[] args) {
ArrayList<Student> arrayList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("-------------------");
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("请输入你的选择:");
int i = scanner.nextInt();
switch (i) {
case 1:
select(arrayList);
break;
case 2:
boolean a = add(arrayList);
if (a == false) {
System.out.println("添加失败");
} else {
System.out.println("添加成功");
}
break;
case 3:
boolean b = edit(arrayList);
if (b == false) {
System.out.println("修改失败");
} else {
System.out.println("修改成功");
}
break;
case 4:
delete(arrayList);
break;
case 5:
System.out.println("已退出");
System.exit(0);
default:
System.out.println("请输入正确的选择");
break;
}
}
}
public static boolean add(ArrayList<Student> arrayList) {
Scanner sc = new Scanner(System.in);
Student stu = new Student();
String id;
while (true) {
System.out.println("请输入学生学号:");
id = sc.nextLine();
boolean b = util(arrayList, id);
if (b) {
break;
} else {
System.out.println("你输入的学号已存在");
}
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
Scanner s = new Scanner(System.in);
System.out.println("请输入学生地址:");
String address = s.nextLine();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setAddress(address);
arrayList.add(stu);
return true;
}
public static boolean edit(ArrayList<Student> arrayList) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的学号:");
String id = sc.nextLine();
int item = -1;
for (int i = 0; i < arrayList.size(); i++) {
Student st = arrayList.get(i);
if (st.getId().equals(id)) {
item = i;
}
}
if (item == -1) {
System.out.println("你输入的学号有误");
return false;
} else {
System.out.println("请输入学生新姓名:");
String name = sc.nextLine();
System.out.println("请输入学生新年龄:");
int age = sc.nextInt();
Scanner s = new Scanner(System.in);
System.out.println("请输入学生新地址:");
String address = s.nextLine();
Student stu = new Student();
stu.setId(id);
stu.setName(name);
stu.setAge(age);
stu.setAddress(address);
for (int i = 0; i < arrayList.size(); i++) {
Student student = arrayList.get(i);
if (student.getId().equals(id)) {
item = i;
arrayList.set(i, stu);
break;
}
}
return true;
}
}
public static void delete(ArrayList<Student> arrayList) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学号:");
String id = sc.nextLine();
int item = -1;
for (int i = 0; i < arrayList.size(); i++) {
Student stu = arrayList.get(i);
if (stu.getId().equals(id)) {
item = i;
arrayList.remove(i);
}
}
if (item == -1) {
System.out.println("你输入的学号不存在,请重新输入");
} else {
System.out.println("删除成功");
}
}
public static void select(ArrayList<Student> arrayList) {
if (arrayList.size() == 0) {
System.out.println("请添加信息再查询");
return;
}
System.out.println("id\tname\tage\t\taddress");
for (int i = 0; i < arrayList.size(); i++) {
Student stu = arrayList.get(i);
System.out.println(stu.getId() + "\t" + stu.getName()
+ "\t\t" + stu.getAge() + "\t\t" + stu.getAddress());
}
}
public static boolean util(ArrayList<Student> arrayList, String id) {
boolean f = true;
for (int i = 0; i < arrayList.size(); i++) {
Student student = arrayList.get(i);
if (student.getId().equals(id)) {
f = false;
}
}
return f;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/03feee7cdc8189d89f394a04e5f9d8ea.png)