使用Java编写一个学生管理系统,需要包括,添加,排序,查询,修改,删除学生信息的功能
时间: 2023-12-13 07:04:08 浏览: 269
Java课程设计-学生管理系统
5星 · 资源好评率100%
以下是一个简单的学生管理系统的Java代码实现,包括添加、排序、查询、修改和删除学生信息的功能。该程序使用了ArrayList作为学生信息的容器,使用了Scanner类来获取用户的输入。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students = new ArrayList<Student>();
private Scanner scanner = new Scanner(System.in);
public void addStudent() {
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
Student student = new Student(name, age, gender, id);
students.add(student);
System.out.println("学生信息添加成功!");
}
public void sortStudents() {
Collections.sort(students);
System.out.println("学生信息排序成功!");
}
public void queryStudent() {
System.out.println("请输入要查询的学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println(student);
return;
}
}
System.out.println("该学生不存在!");
}
public void modifyStudent() {
System.out.println("请输入要修改的学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
student.setName(name);
student.setAge(age);
student.setGender(gender);
System.out.println("学生信息修改成功!");
return;
}
}
System.out.println("该学生不存在!");
}
public void deleteStudent() {
System.out.println("请输入要删除的学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
students.remove(student);
System.out.println("学生信息删除成功!");
return;
}
}
System.out.println("该学生不存在!");
}
public void printMenu() {
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("0. 退出程序");
System.out.println("请选择菜单项:");
}
public void run() {
while (true) {
printMenu();
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addStudent();
break;
case 2:
sortStudents();
break;
case 3:
queryStudent();
break;
case 4:
modifyStudent();
break;
case 5:
deleteStudent();
break;
case 0:
System.out.println("程序已退出!");
return;
default:
System.out.println("无效的菜单项,请重新选择!");
}
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.run();
}
}
class Student implements Comparable<Student> {
private String name;
private int age;
private String gender;
private String id;
public Student(String name, int age, String gender, String id) {
this.name = name;
this.age = age;
this.gender = gender;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public int compareTo(Student o) {
return this.id.compareTo(o.id);
}
@Override
public String toString() {
return "学生信息:" +
"姓名='" + name + '\'' +
", 年龄=" + age +
", 性别='" + gender + '\'' +
", 学号='" + id + '\'';
}
}
```
在该程序中,Student类表示学生信息,具有姓名、年龄、性别和学号等属性。StudentManagementSystem类表示学生管理系统,具有添加、排序、查询、修改和删除学生信息等功能。该程序使用了ArrayList来存储学生信息,使用了Collections类的sort方法来对学生信息进行排序,使用了Scanner类来获取用户的输入。
阅读全文