用Java写出学生选课系统
时间: 2023-08-31 11:11:58 浏览: 86
好的,下面是一个简单的学生选课系统的Java代码示例。
首先,我们需要设计一个数据库,用于存储学生和课程的信息。这里以MySQL数据库为例,创建一个名为“course_selection”的数据库,其中包含两个表格:学生表格“students”和课程表格“courses”。
```sql
CREATE DATABASE course_selection;
USE course_selection;
CREATE TABLE students (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
grade INT NOT NULL,
major VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE courses (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
teacher VARCHAR(50) NOT NULL,
credit INT NOT NULL,
capacity INT NOT NULL,
PRIMARY KEY (id)
);
```
然后,我们可以使用Java JDBC技术连接数据库,并且创建一个名为“CourseSelection”的类,用于处理学生选课系统的业务逻辑。
```java
import java.sql.*;
public class CourseSelection {
private Connection conn;
public CourseSelection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/course_selection", "root", "password");
} catch (Exception e) {
e.printStackTrace();
}
}
public void addStudent(String name, String gender, int grade, String major) {
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO students (name, gender, grade, major) VALUES (?, ?, ?, ?)");
stmt.setString(1, name);
stmt.setString(2, gender);
stmt.setInt(3, grade);
stmt.setString(4, major);
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void addCourse(String name, String teacher, int credit, int capacity) {
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO courses (name, teacher, credit, capacity) VALUES (?, ?, ?, ?)");
stmt.setString(1, name);
stmt.setString(2, teacher);
stmt.setInt(3, credit);
stmt.setInt(4, capacity);
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void selectCourse(int studentId, int courseId) {
try {
PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(*) FROM selections WHERE student_id = ? AND course_id = ?");
stmt.setInt(1, studentId);
stmt.setInt(2, courseId);
ResultSet rs = stmt.executeQuery();
rs.next();
int count = rs.getInt(1);
rs.close();
stmt.close();
if (count > 0) {
System.out.println("You have already selected this course.");
} else {
stmt = conn.prepareStatement("SELECT COUNT(*) FROM selections WHERE course_id = ?");
stmt.setInt(1, courseId);
rs = stmt.executeQuery();
rs.next();
int selectedCount = rs.getInt(1);
rs.close();
stmt.close();
stmt = conn.prepareStatement("SELECT capacity FROM courses WHERE id = ?");
stmt.setInt(1, courseId);
rs = stmt.executeQuery();
rs.next();
int capacity = rs.getInt(1);
rs.close();
stmt.close();
if (selectedCount >= capacity) {
System.out.println("This course is full.");
} else {
stmt = conn.prepareStatement("INSERT INTO selections (student_id, course_id) VALUES (?, ?)");
stmt.setInt(1, studentId);
stmt.setInt(2, courseId);
stmt.executeUpdate();
stmt.close();
System.out.println("You have selected this course successfully.");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
最后,我们可以编写一个测试程序,用于演示学生选课系统的使用方法。
```java
import java.util.Scanner;
public class TestCourseSelection {
public static void main(String[] args) {
CourseSelection cs = new CourseSelection();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Please select an operation:");
System.out.println("1. Add student");
System.out.println("2. Add course");
System.out.println("3. Select course");
System.out.println("4. Exit");
int option = scanner.nextInt();
if (option == 1) {
System.out.println("Please enter student's name:");
String name = scanner.next();
System.out.println("Please enter student's gender:");
String gender = scanner.next();
System.out.println("Please enter student's grade:");
int grade = scanner.nextInt();
System.out.println("Please enter student's major:");
String major = scanner.next();
cs.addStudent(name, gender, grade, major);
} else if (option == 2) {
System.out.println("Please enter course's name:");
String name = scanner.next();
System.out.println("Please enter course's teacher:");
String teacher = scanner.next();
System.out.println("Please enter course's credit:");
int credit = scanner.nextInt();
System.out.println("Please enter course's capacity:");
int capacity = scanner.nextInt();
cs.addCourse(name, teacher, credit, capacity);
} else if (option == 3) {
System.out.println("Please enter student's id:");
int studentId = scanner.nextInt();
System.out.println("Please enter course's id:");
int courseId = scanner.nextInt();
cs.selectCourse(studentId, courseId);
} else if (option == 4) {
break;
} else {
System.out.println("Invalid option.");
}
}
scanner.close();
}
}
```
以上就是一个简单的学生选课系统的Java代码示例。当然,这只是一个基础版的系统,实际开发中还需要考虑更多的因素,如安全性、性能、可扩展性等。
阅读全文