用java将控制台版本学生选课系统拓展成可读取写入文件的版本
时间: 2023-09-10 14:05:53 浏览: 49
好的,以下是一个用Java将控制台版本学生选课系统拓展成可读取写入文件的版本的示例代码:
```java
import java.io.*;
import java.util.*;
public class StudentCourseSystem {
private Map<String, Student> students;
private Map<String, Course> courses;
public StudentCourseSystem() {
students = new HashMap<>();
courses = new HashMap<>();
}
public void loadStudents(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
Student student = new Student(parts[0], parts[1]);
students.put(parts[0], student);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadCourses(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
Course course = new Course(parts[0], parts[1], Integer.parseInt(parts[2]));
courses.put(parts[0], course);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void enroll(String studentId, String courseId) {
Student student = students.get(studentId);
Course course = courses.get(courseId);
if (student == null || course == null) {
System.out.println("Invalid student ID or course ID");
return;
}
if (student.getCourses().contains(course)) {
System.out.println("The student has already enrolled in the course");
return;
}
if (course.getRemainingCapacity() <= 0) {
System.out.println("The course is full");
return;
}
student.addCourse(course);
course.addStudent(student);
System.out.println("Enrollment succeeded");
save();
}
public void drop(String studentId, String courseId) {
Student student = students.get(studentId);
Course course = courses.get(courseId);
if (student == null || course == null) {
System.out.println("Invalid student ID or course ID");
return;
}
if (!student.getCourses().contains(course)) {
System.out.println("The student has not enrolled in the course");
return;
}
student.dropCourse(course);
course.dropStudent(student);
System.out.println("Drop succeeded");
save();
}
private void save() {
try {
BufferedWriter studentWriter = new BufferedWriter(new FileWriter("students.csv"));
for (Student student : students.values()) {
studentWriter.write(student.getId() + "," + student.getName());
studentWriter.newLine();
}
studentWriter.close();
BufferedWriter courseWriter = new BufferedWriter(new FileWriter("courses.csv"));
for (Course course : courses.values()) {
courseWriter.write(course.getId() + "," + course.getName() + "," + course.getCapacity());
courseWriter.newLine();
}
courseWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StudentCourseSystem system = new StudentCourseSystem();
system.loadStudents("students.csv");
system.loadCourses("courses.csv");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Enroll");
System.out.println("2. Drop");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
String choice = scanner.nextLine();
switch (choice) {
case "1":
System.out.print("Enter student ID: ");
String studentId = scanner.nextLine();
System.out.print("Enter course ID: ");
String courseId = scanner.nextLine();
system.enroll(studentId, courseId);
break;
case "2":
System.out.print("Enter student ID: ");
studentId = scanner.nextLine();
System.out.print("Enter course ID: ");
courseId = scanner.nextLine();
system.drop(studentId, courseId);
break;
case "3":
System.exit(0);
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
}
class Student {
private String id;
private String name;
private List<Course> courses;
public Student(String id, String name) {
this.id = id;
this.name = name;
courses = new ArrayList<>();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public List<Course> getCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
public void dropCourse(Course course) {
courses.remove(course);
}
}
class Course {
private String id;
private String name;
private int capacity;
private List<Student> students;
public Course(String id, String name, int capacity) {
this.id = id;
this.name = name;
this.capacity = capacity;
students = new ArrayList<>();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getCapacity() {
return capacity;
}
public int getRemainingCapacity() {
return capacity - students.size();
}
public List<Student> getStudents() {
return students;
}
public void addStudent(Student student) {
students.add(student);
}
public void dropStudent(Student student) {
students.remove(student);
}
}
```
在上面的示例代码中,我们使用了Java的标准输入输出和文件读写功能。具体来说,我们使用了Scanner类来读取用户输入,使用了BufferedReader和FileWriter类来读写CSV格式的文件。你可以根据自己的需要选择其他文件格式和读写方式。同时,为了避免多次读写文件导致的性能问题,你可以考虑添加缓存机制,例如将内存中的数据定期写入文件。