用Java增删改查的学员管理项目
时间: 2024-04-17 16:26:40 浏览: 67
用Jsp写的简单的学员增,删,改,查
3星 · 编辑精心推荐
你好!对于学员管理项目,你可以使用Java编写增删改查的功能。以下是一个简单的示例:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student {
private String name;
private int age;
private String grade;
public Student(String name, int age, String grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
// Getters and setters
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 getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
class StudentManagementSystem {
private List<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public void updateStudent(Student student, String newName, int newAge, String newGrade) {
student.setName(newName);
student.setAge(newAge);
student.setGrade(newGrade);
}
public void displayAllStudents() {
for (Student student : students) {
System.out.println("Name: " + student.getName());
System.out.println("Age: " + student.getAge());
System.out.println("Grade: " + student.getGrade());
System.out.println("-------------");
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem system = new StudentManagementSystem();
while (true) {
System.out.println("1. Add student");
System.out.println("2. Remove student");
System.out.println("3. Update student");
System.out.println("4. Display all students");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
System.out.print("Enter student name: ");
String name = scanner.nextLine();
System.out.print("Enter student age: ");
int age = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter student grade: ");
String grade = scanner.nextLine();
Student newStudent = new Student(name, age, grade);
system.addStudent(newStudent);
System.out.println("Student added successfully!");
break;
case 2:
System.out.print("Enter student name to remove: ");
String nameToRemove = scanner.nextLine();
boolean removed = false;
for (Student student : system.getStudents()) {
if (student.getName().equals(nameToRemove)) {
system.removeStudent(student);
removed = true;
System.out.println("Student removed successfully!");
break;
}
}
if (!removed) {
System.out.println("Student not found!");
}
break;
case 3:
System.out.print("Enter student name to update: ");
String nameToUpdate = scanner.nextLine();
boolean updated = false;
for (Student student : system.getStudents()) {
if (student.getName().equals(nameToUpdate)) {
System.out.print("Enter new name: ");
String newName = scanner.nextLine();
System.out.print("Enter new age: ");
int newAge = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter new grade: ");
String newGrade = scanner.nextLine();
system.updateStudent(student, newName, newAge, newGrade);
updated = true;
System.out.println("Student updated successfully!");
break;
}
}
if (!updated) {
System.out.println("Student not found!");
}
break;
case 4:
system.displayAllStudents();
break;
case 0:
System.out.println("Exiting program...");
System.exit(0);
default:
System.out.println("Invalid choice!");
}
}
}
}
```
这是一个基本的学员管理系统,使用了Java的面向对象编程。你可以通过菜单选择来添加、删除、更新和显示学员信息。希望这个示例对你有帮助!如果有任何问题,请随时向我提问。
阅读全文