写一段学生管理系统代码 前后端的Java代码
时间: 2024-09-14 19:02:01 浏览: 71
创建一个简单的学生管理系统涉及到前端和后端的开发。前端通常是用户与程序交互的界面,而后端则是处理逻辑和数据存储的地方。下面我将提供一个简化版的学生管理系统的Java代码示例,包括基本的前端和后端实现。
后端代码(后端通常负责处理逻辑和数据):
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student {
private String name;
private int age;
private String studentID;
public Student(String name, int age, String studentID) {
this.name = name;
this.age = age;
this.studentID = studentID;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getStudentID() {
return studentID;
}
}
public class StudentManagementSystem {
private List<Student> students = new ArrayList<>();
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(String studentID) {
students.removeIf(student -> student.getStudentID().equals(studentID));
}
public void printStudents() {
for (Student student : students) {
System.out.println("Name: " + student.getName() + ", Age: " + student.getAge() + ", ID: " + student.getStudentID());
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem sms = new StudentManagementSystem();
while (true) {
System.out.println("Welcome to the Student Management System");
System.out.println("1. Add Student");
System.out.println("2. Remove Student");
System.out.println("3. List Students");
System.out.println("4. Exit");
System.out.print("Please select an option: ");
int option = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (option) {
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
System.out.print("Enter student ID: ");
String studentID = scanner.nextLine();
sms.addStudent(new Student(name, age, studentID));
break;
case 2:
System.out.print("Enter student ID to remove: ");
String id = scanner.nextLine();
sms.removeStudent(id);
break;
case 3:
sms.printStudents();
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
break;
default:
System.out.println("Invalid option, please try again.");
}
}
}
}
```
前端代码(在命令行中,用户可以直接输入指令来管理学生信息):
```java
// 该示例的前端代码实际上在控制台输入指令与系统交互,没有分离的前端代码。
// 用户在控制台运行main方法后,通过输入不同选项来管理学生信息。
```
请注意,这个例子非常简化,实际的系统可能需要更复杂的功能和错误处理。此外,真实的系统通常会在服务器上运行后端代码,并通过HTTP请求与前端代码交互,前端代码可能会是基于Web页面的JavaScript或者其他客户端程序。
阅读全文