鱼C论坛

 找回密码
 立即注册
查看: 572|回复: 5

做一个简单的选课系统

[复制链接]
发表于 2023-12-28 02:58:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
列出一个简单的选课系统所需要的功能,并列出大致的实现代码,需要将这个系统界面化!@fishc
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-28 02:58:13 | 显示全部楼层
一个简单的选课系统可能需要以下功能:

1. 学生和课程的管理:包括学生和课程的增删改查操作。
2. 学生选课和退课:学生可从课程列表中选择课程,并可以取消已选的课程。
3. 课程查询:学生可以查看自己已选的课程,或者按照不同条件(如课程名称、授课教师等)查询可选课程。

下面是一个简单的选课系统的界面化实现代码(使用Python的tkinter库):
  1. import tkinter as tk
  2. class Course:
  3.     def __init__(self, name, teacher):
  4.         self.name = name
  5.         self.teacher = teacher
  6. class Student:
  7.     def __init__(self, name):
  8.         self.name = name
  9.         self.courses = []
  10. class CourseSelectionSystem:
  11.     def __init__(self):
  12.         self.students = []
  13.         self.courses = []
  14.     def add_student(self, name):
  15.         student = Student(name)
  16.         self.students.append(student)
  17.     def add_course(self, name, teacher):
  18.         course = Course(name, teacher)
  19.         self.courses.append(course)
  20.     def get_student_courses(self, student):
  21.         return student.courses
  22.     def add_course_for_student(self, student, course):
  23.         student.courses.append(course)
  24.     def remove_course_for_student(self, student, course):
  25.         student.courses.remove(course)
  26. # 创建选课系统对象
  27. selection_system = CourseSelectionSystem()
  28. # 添加学生和课程示例数据
  29. selection_system.add_student("小明")
  30. selection_system.add_student("小红")
  31. selection_system.add_course("数学", "王老师")
  32. selection_system.add_course("英语", "李老师")
  33. # 创建界面
  34. window = tk.Tk()
  35. window.title("选课系统")
  36. # 学生选择列表
  37. student_listbox = tk.Listbox(window)
  38. student_listbox.pack(side=tk.LEFT)
  39. # 课程选择列表
  40. course_listbox = tk.Listbox(window)
  41. course_listbox.pack(side=tk.LEFT)
  42. def refresh_student_list():
  43.     student_listbox.delete(0, tk.END)
  44.     for student in selection_system.students:
  45.         student_listbox.insert(tk.END, student.name)
  46. def refresh_course_list():
  47.     course_listbox.delete(0, tk.END)
  48.     for course in selection_system.courses:
  49.         course_listbox.insert(tk.END, f"{course.name} - {course.teacher}")
  50. def select():
  51.     selected_student_index = student_listbox.curselection()[0]
  52.     selected_student = selection_system.students[selected_student_index]
  53.     selected_course_index = course_listbox.curselection()[0]
  54.     selected_course = selection_system.courses[selected_course_index]
  55.     selection_system.add_course_for_student(selected_student, selected_course)
  56.     refresh_student_list()
  57. refresh_student_list()
  58. refresh_course_list()
  59. # 学生选择按钮
  60. select_button = tk.Button(window, text="选课", command=select)
  61. select_button.pack()
  62. window.mainloop()
复制代码

这个代码演示了如何利用Python的tkinter库来实现一个简单的选课系统界面化。在界面中,左侧显示学生列表,右侧显示课程列表,学生通过选择学生和课程后点击“选课”按钮来进行选课操作。选课操作会更新学生列表,显示当前学生所选的课程。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-28 02:58:58 | 显示全部楼层
FishC 发表于 2023-12-28 02:58
一个简单的选课系统可能需要以下功能:

1. 学生和课程的管理:包括学生和课程的增删改查操作。

使用java完成
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-28 02:59:24 | 显示全部楼层

以下是使用Java完成的简单选课系统的示例代码:
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. class Course {
  5.     private String name;
  6.     private String teacher;
  7.     public Course(String name, String teacher) {
  8.         this.name = name;
  9.         this.teacher = teacher;
  10.     }
  11.     public String getName() {
  12.         return name;
  13.     }
  14.     public String getTeacher() {
  15.         return teacher;
  16.     }
  17. }
  18. class Student {
  19.     private String name;
  20.     private List<Course> courses;
  21.     public Student(String name) {
  22.         this.name = name;
  23.         this.courses = new ArrayList<>();
  24.     }
  25.     public String getName() {
  26.         return name;
  27.     }
  28.     public List<Course> getCourses() {
  29.         return courses;
  30.     }
  31.     public void addCourse(Course course) {
  32.         courses.add(course);
  33.     }
  34.     public void removeCourse(Course course) {
  35.         courses.remove(course);
  36.     }
  37. }
  38. class CourseSelectionSystem {
  39.     private List<Student> students;
  40.     private List<Course> courses;
  41.     public CourseSelectionSystem() {
  42.         this.students = new ArrayList<>();
  43.         this.courses = new ArrayList<>();
  44.     }
  45.     public void addStudent(String name) {
  46.         Student student = new Student(name);
  47.         students.add(student);
  48.     }
  49.     public void addCourse(String name, String teacher) {
  50.         Course course = new Course(name, teacher);
  51.         courses.add(course);
  52.     }
  53.     public List<Course> getStudentCourses(Student student) {
  54.         return student.getCourses();
  55.     }
  56.     public void addCourseForStudent(Student student, Course course) {
  57.         student.addCourse(course);
  58.     }
  59.     public void removeCourseForStudent(Student student, Course course) {
  60.         student.removeCourse(course);
  61.     }
  62. }
  63. public class CourseSelectionSystemDemo {
  64.     public static void main(String[] args) {
  65.         CourseSelectionSystem selectionSystem = new CourseSelectionSystem();
  66.         Scanner scanner = new Scanner(System.in);
  67.         // 添加学生和课程示例数据
  68.         selectionSystem.addStudent("小明");
  69.         selectionSystem.addStudent("小红");
  70.         selectionSystem.addCourse("数学", "王老师");
  71.         selectionSystem.addCourse("英语", "李老师");
  72.         while (true) {
  73.             System.out.println("======= 选课系统 =======");
  74.             System.out.println("1. 学生选课");
  75.             System.out.println("2. 学生退课");
  76.             System.out.println("3. 查询学生选课");
  77.             System.out.println("0. 退出系统");
  78.             System.out.print("请输入操作编号:");
  79.             int choice = scanner.nextInt();
  80.             scanner.nextLine(); // 清空缓冲区
  81.             if (choice == 0) {
  82.                 System.out.println("退出系统,谢谢使用!");
  83.                 break;
  84.             }
  85.             switch (choice) {
  86.                 case 1:
  87.                     System.out.print("请输入学生姓名:");
  88.                     String studentName = scanner.nextLine();
  89.                     System.out.println("可选课程列表:");
  90.                     List<Course> availableCourses = selectionSystem.getCourses();
  91.                     for (int i = 0; i < availableCourses.size(); i++) {
  92.                         Course course = availableCourses.get(i);
  93.                         System.out.println((i + 1) + ". " + course.getName() + " - " + course.getTeacher());
  94.                     }
  95.                     System.out.print("请输入选课编号:");
  96.                     int courseChoice = scanner.nextInt();
  97.                     scanner.nextLine(); // 清空缓冲区
  98.                     Student student = null;
  99.                     for (Student s : selectionSystem.getStudents()) {
  100.                         if (s.getName().equals(studentName)) {
  101.                             student = s;
  102.                             break;
  103.                         }
  104.                     }
  105.                     if (student != null) {
  106.                         Course selectedCourse = availableCourses.get(courseChoice - 1);
  107.                         selectionSystem.addCourseForStudent(student, selectedCourse);
  108.                         System.out.println("选课成功!");
  109.                     } else {
  110.                         System.out.println("学生不存在!");
  111.                     }
  112.                     break;
  113.                 case 2:
  114.                     System.out.print("请输入学生姓名:");
  115.                     studentName = scanner.nextLine();
  116.                     System.out.println("已选课程列表:");
  117.                     student = null;
  118.                     for (Student s : selectionSystem.getStudents()) {
  119.                         if (s.getName().equals(studentName)) {
  120.                             student = s;
  121.                             break;
  122.                         }
  123.                     }
  124.                     if (student != null) {
  125.                         List<Course> selectedCourses = student.getCourses();
  126.                         for (int i = 0; i < selectedCourses.size(); i++) {
  127.                             Course course = selectedCourses.get(i);
  128.                             System.out.println((i + 1) + ". " + course.getName() + " - " + course.getTeacher());
  129.                         }
  130.                         System.out.print("请输入退课编号:");
  131.                         int withdrawChoice = scanner.nextInt();
  132.                         scanner.nextLine(); // 清空缓冲区
  133.                         if (withdrawChoice > 0 && withdrawChoice <= selectedCourses.size()) {
  134.                             Course withdrawnCourse = selectedCourses.get(withdrawChoice - 1);
  135.                             selectionSystem.removeCourseForStudent(student, withdrawnCourse);
  136.                             System.out.println("退课成功!");
  137.                         } else {
  138.                             System.out.println("无效的退课编号!");
  139.                         }
  140.                     } else {
  141.                         System.out.println("学生不存在!");
  142.                     }
  143.                     break;
  144.                 case 3:
  145.                     System.out.print("请输入学生姓名:");
  146.                     studentName = scanner.nextLine();
  147.                     student = null;
  148.                     for (Student s : selectionSystem.getStudents()) {
  149.                         if (s.getName().equals(studentName)) {
  150.                             student = s;
  151.                             break;
  152.                         }
  153.                     }
  154.                     if (student != null) {
  155.                         System.out.println("已选课程列表:");
  156.                         List<Course> selectedCourses = student.getCourses();
  157.                         for (int i = 0; i < selectedCourses.size(); i++) {
  158.                             Course course = selectedCourses.get(i);
  159.                             System.out.println((i + 1) + ". " + course.getName() + " - " + course.getTeacher());
  160.                         }
  161.                     } else {
  162.                         System.out.println("学生不存在!");
  163.                     }
  164.                     break;
  165.                 default:
  166.                     System.out.println("无效的操作编号!");
  167.                     break;
  168.             }
  169.             System.out.println();
  170.         }
  171.         scanner.close();
  172.     }
  173. }
复制代码

这个Java代码演示了一个简单的选课系统的实现。在控制台中,用户可以选择进行学生选课、学生退课和查询学生选课等操作,系统会根据用户输入进行相应的处理。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-28 03:03:21 | 显示全部楼层
FishC 发表于 2023-12-28 02:59
以下是使用Java完成的简单选课系统的示例代码:

做一个简单的学生选课系统,我在sql server 中已经建了三张表,Student,Course 和SC表,用于选课系统,如何用Java代码实现基本的增删改查!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-28 03:03:47 | 显示全部楼层
豪在这学python 发表于 2023-12-28 03:03
做一个简单的学生选课系统,我在sql server 中已经建 ...

要使用Java代码实现基本的增删改查操作,你需要首先建立与数据库的连接,并使用Java的JDBC API来执行SQL查询和更新操作。以下是一个简单的示例代码,展示了如何使用Java代码实现对学生选课系统中的学生和课程进行增删改查操作:
  1. import java.sql.*;
  2. import java.util.Scanner;
  3. public class CourseSelectionSystem {
  4.     private static final String DB_URL = "jdbc:sqlserver://localhost:1433;DatabaseName=YourDatabaseName";
  5.     private static final String DB_USER = "YourDBUser";
  6.     private static final String DB_PASSWORD = "YourDBPassword";
  7.     public static void main(String[] args) {
  8.         try {
  9.             // 建立与数据库的连接
  10.             Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
  11.             // 创建Statement对象
  12.             Statement stmt = conn.createStatement();
  13.             // 创建Scanner对象用于获取用户输入
  14.             Scanner scanner = new Scanner(System.in);
  15.             while (true) {
  16.                 System.out.println("======= 选课系统 =======");
  17.                 System.out.println("1. 添加学生");
  18.                 System.out.println("2. 删除学生");
  19.                 System.out.println("3. 修改学生姓名");
  20.                 System.out.println("4. 查询所有学生");
  21.                 System.out.println("5. 添加课程");
  22.                 System.out.println("6. 删除课程");
  23.                 System.out.println("7. 修改课程名称");
  24.                 System.out.println("8. 查询所有课程");
  25.                 System.out.println("0. 退出系统");
  26.                 System.out.print("请输入操作编号:");
  27.                 int choice = scanner.nextInt();
  28.                 scanner.nextLine(); // 清空缓冲区
  29.                 if (choice == 0) {
  30.                     System.out.println("退出系统,谢谢使用!");
  31.                     break;
  32.                 }
  33.                 switch (choice) {
  34.                     case 1:
  35.                         System.out.print("请输入学生姓名:");
  36.                         String studentName = scanner.nextLine();
  37.                         stmt.executeUpdate("INSERT INTO Student (name) VALUES ('" + studentName + "')");
  38.                         System.out.println("添加学生成功!");
  39.                         break;
  40.                     case 2:
  41.                         System.out.print("请输入学生id:");
  42.                         int studentId = scanner.nextInt();
  43.                         scanner.nextLine(); // 清空缓冲区
  44.                         stmt.executeUpdate("DELETE FROM Student WHERE id = " + studentId);
  45.                         System.out.println("删除学生成功!");
  46.                         break;
  47.                     case 3:
  48.                         System.out.print("请输入学生id:");
  49.                         studentId = scanner.nextInt();
  50.                         scanner.nextLine(); // 清空缓冲区
  51.                         System.out.print("请输入修改后的学生姓名:");
  52.                         studentName = scanner.nextLine();
  53.                         stmt.executeUpdate("UPDATE Student SET name = '" + studentName + "' WHERE id = " + studentId);
  54.                         System.out.println("修改学生姓名成功!");
  55.                         break;
  56.                     case 4:
  57.                         ResultSet studentResult = stmt.executeQuery("SELECT * FROM Student");
  58.                         while (studentResult.next()) {
  59.                             int id = studentResult.getInt("id");
  60.                             String name = studentResult.getString("name");
  61.                             System.out.println("学生ID:" + id + ",姓名:" + name);
  62.                         }
  63.                         break;
  64.                     case 5:
  65.                         System.out.print("请输入课程名称:");
  66.                         String courseName = scanner.nextLine();
  67.                         stmt.executeUpdate("INSERT INTO Course (name) VALUES ('" + courseName + "')");
  68.                         System.out.println("添加课程成功!");
  69.                         break;
  70.                     case 6:
  71.                         System.out.print("请输入课程id:");
  72.                         int courseId = scanner.nextInt();
  73.                         scanner.nextLine(); // 清空缓冲区
  74.                         stmt.executeUpdate("DELETE FROM Course WHERE id = " + courseId);
  75.                         System.out.println("删除课程成功!");
  76.                         break;
  77.                     case 7:
  78.                         System.out.print("请输入课程id:");
  79.                         courseId = scanner.nextInt();
  80.                         scanner.nextLine(); // 清空缓冲区
  81.                         System.out.print("请输入修改后的课程名称:");
  82.                         courseName = scanner.nextLine();
  83.                         stmt.executeUpdate("UPDATE Course SET name = '" + courseName + "' WHERE id = " + courseId);
  84.                         System.out.println("修改课程名称成功!");
  85.                         break;
  86.                     case 8:
  87.                         ResultSet courseResult = stmt.executeQuery("SELECT * FROM Course");
  88.                         while (courseResult.next()) {
  89.                             int id = courseResult.getInt("id");
  90.                             String name = courseResult.getString("name");
  91.                             System.out.println("课程ID:" + id + ",名称:" + name);
  92.                         }
  93.                         break;
  94.                     default:
  95.                         System.out.println("无效的操作编号!");
  96.                         break;
  97.                 }
  98.             }
  99.             // 关闭连接和资源
  100.             stmt.close();
  101.             conn.close();
  102.         } catch (Exception e) {
  103.             e.printStackTrace();
  104.         }
  105.     }
  106. }
复制代码

请注意,以上代码中的`DB_URL`、`DB_USER`和`DB_PASSWORD`需要根据你的SQL Server配置进行修改。你还需要确保在运行代码之前已经在SQL Server中创建了`Student`表和`Course`表,并且在`SC`表中建立了学生和课程之间的关系。

以上代码演示了如何使用Java的JDBC API来执行SQL查询和更新操作。通过输入不同的操作编号,你可以进行学生和课程的增删改查操作。要退出系统,请选择编号0。

请注意,此代码示例仅为演示目的,并没有包含完整的错误处理和数据验证。在实际应用中,你可能需要添加更多的错误处理和数据验证的代码,以确保系统的稳定性和安全性。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 19:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表