Java学生信息管理系统:简单与详细的实现
Java学生信息管理系统可以用于管理学生的信息,如学生的姓名、学号、课程等。下面我将提供一个简单版本以及一个详细版本的实现方案。
简单实现
功能: 添加学生、查看所有学生信息、根据学号删除学生。
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private String name;
private String id;
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
@Override
public String toString() {
return "Student ID: " + id + ", Name: " + name;
}
}
public class StudentManagementSystem {
private ArrayList<Student> students = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
new StudentManagementSystem().run();
}
private void run() {
while (true) {
System.out.println("\n1. Add Student");
System.out.println("2. View Students");
System.out.println("3. Delete Student");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addStudent();
break;
case 2:
viewStudents();
break;
case 3:
deleteStudent();
break;
case 4:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
private void addStudent() {
System.out.print("Enter Student Name: ");
String name = scanner.nextLine();
System.out.print("Enter Student ID: ");
String id = scanner.nextLine();
students.add(new Student(name, id));
System.out.println("Student added successfully.");
}
private void viewStudents() {
if (students.isEmpty()) {
System.out.println("No students found.");
return;
}
for (Student student : students) {
System.out.println(student);
}
}
private void deleteStudent() {
System.out.print("Enter Student ID to delete: ");
String id = scanner.nextLine();
students.removeIf(student -> student.getId().equals(id));
System.out.println("Student deleted if existed.");
}
}
详细实现
功能: 在简单版本基础上增加更新学生信息、查找学生,以及持久化数据到文件中。
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
class Student {
private String name;
private String id;
private String course;
public Student(String name, String id, String course) {
this.name = name;
this.id = id;
this.course = course;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getCourse() {
return course;
}
public void setName(String name) {
this.name = name;
}
public void setCourse(String course) {
this.course = course;
}
@Override
public String toString() {
return "Student ID: " + id + ", Name: " + name + ", Course: " + course;
}
}
public class StudentManagementSystem {
private ArrayList<Student> students = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
private static final String FILE_PATH = "students.txt";
public static void main(String[] args) {
new StudentManagementSystem().run();
}
private void run() {
loadStudents();
while (true) {
System.out.println("\n1. Add Student");
System.out.println("2. View Students");
System.out.println("3. Delete Student");
System.out.println("4. Update Student");
System.out.println("5. Find Student");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addStudent();
break;
case 2:
viewStudents();
break;
case 3:
deleteStudent();
break;
case 4:
updateStudent();
break;
case 5:
findStudent();
break;
case 6:
saveStudents();
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
private void addStudent() {
System.out.print("Enter Student Name: ");
String name = scanner.nextLine();
System.out.print("Enter Student ID: ");
String id = scanner.nextLine();
System.out.print("Enter Course: ");
String course = scanner.nextLine();
students.add(new Student(name, id, course));
System.out.println("Student added successfully.");
}
private void viewStudents() {
if (students.isEmpty()) {
System.out.println("No students found.");
return;
}
for (Student student : students) {
System.out.println(student);
}
}
private void deleteStudent() {
System.out.print("Enter Student ID to delete: ");
String id = scanner.nextLine();
students.removeIf(student -> student.getId().equals(id));
System.out.println("Student deleted if existed.");
}
private void updateStudent() {
System.out.print("Enter Student ID to update: ");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.print("Enter new name: ");
String newName = scanner.nextLine();
System.out.print("Enter new course: ");
String newCourse = scanner.nextLine();
student.setName(newName);
student.setCourse(newCourse);
System.out.println("Student updated.");
return;
}
}
System.out.println("Student not found.");
}
private void findStudent() {
System.out.print("Enter Student ID to find: ");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println(student);
return;
}
}
System.out.println("Student not found.");
}
private void loadStudents() {
try (BufferedReader br = new BufferedReader(new FileReader(FILE_PATH))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
students.add(new Student(parts[0], parts[1], parts[2]));
}
}
} catch (IOException e) {
System.out.println("Error loading students: " + e.getMessage());
}
}
private void saveStudents() {
try (PrintWriter pw = new PrintWriter(new FileWriter(FILE_PATH))) {
for (Student student : students) {
pw.println(student.getName() + "," + student.getId() + "," + student.getCourse());
}
} catch (IOException e) {
System.out.println("Error saving students: " + e.getMessage());
}
}
}
说明
- 简单实现适合初学者和缺少复杂功能的管理系统,提供最基本的CRUD操作。
- 详细实现增加了学生信息更新、查找功能,并使用文件存储学生信息,以便程序关闭后数据不丢失。
- 可以根据需求扩展功能,例如实现图形界面、加入异常处理、使用数据库来代替文件存储等。