10月23日

杨家兴發表於2024-10-23

專案名稱:新課程新增
(1)新課程資訊必須唯一,如有重複,提示使用者“課程名稱
重複,重新錄入”;
(2)要求判斷任課教師為王建民、劉立嘉、劉丹、楊子光、
張雲霞、武勇亮、高飛、孫靜八位教師的其中一位。
(3)要求上課地點開頭為“一教、二教、三教、基教”中的
一種;
(4)將新課程資訊新增入庫。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class CourseManager {

// MySQL 資料庫連線資訊
private static final String DB_URL = "jdbc:mysql://localhost:3306/school_db?useSSL=false&serverTimezone=UTC";
private static final String DB_USER = "root";  // 資料庫使用者名稱
private static final String DB_PASSWORD = "password";  // 資料庫密碼

// 有效教師名單
private static final String[] VALID_TEACHERS = {"王建民", "劉立嘉", "劉丹", "楊子光", "張雲霞", "武勇亮", "高飛", "孫靜"};

// 有效上課地點字首
private static final String[] VALID_LOCATIONS = {"一教", "二教", "三教", "基教"};

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // 錄入課程資訊
    System.out.println("請輸入課程名稱:");
    String courseName = scanner.nextLine();

    System.out.println("請輸入任課教師:");
    String teacherName = scanner.nextLine();

    System.out.println("請輸入上課地點:");
    String location = scanner.nextLine();

    scanner.close();

    // 驗證教師姓名
    if (!isValidTeacher(teacherName)) {
        System.out.println("錯誤:任課教師必須是王建民、劉立嘉、劉丹、楊子光、張雲霞、武勇亮、高飛、孫靜中的一位。");
        return;
    }

    // 驗證上課地點
    if (!isValidLocation(location)) {
        System.out.println("錯誤:上課地點必須以'一教'、'二教'、'三教'、'基教'開頭。");
        return;
    }

    // 驗證課程名稱唯一性
    if (isCourseExists(courseName)) {
        System.out.println("課程名稱重複,重新錄入");
        return;
    }

    // 將新課程資訊新增入庫
    if (addCourseToDB(courseName, teacherName, location)) {
        System.out.println("課程資訊新增成功!");
    } else {
        System.out.println("課程資訊新增失敗!");
    }
}

// 驗證教師姓名是否合法
private static boolean isValidTeacher(String teacherName) {
    for (String validTeacher : VALID_TEACHERS) {
        if (validTeacher.equals(teacherName)) {
            return true;
        }
    }
    return false;
}

// 驗證上課地點是否合法
private static boolean isValidLocation(String location) {
    for (String validLocation : VALID_LOCATIONS) {
        if (location.startsWith(validLocation)) {
            return true;
        }
    }
    return false;
}

// 檢查課程名稱是否已經存在
private static boolean isCourseExists(String courseName) {
    try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
        String query = "SELECT COUNT(*) FROM courses WHERE course_name = ?";
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, courseName);

        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            return resultSet.getInt(1) > 0;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}

// 將新課程新增到資料庫
private static boolean addCourseToDB(String courseName, String teacherName, String location) {
    try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
        String insertQuery = "INSERT INTO courses (course_name, teacher_name, location) VALUES (?, ?, ?)";
        PreparedStatement statement = conn.prepareStatement(insertQuery);
        statement.setString(1, courseName);
        statement.setString(2, teacherName);
        statement.setString(3, location);

        int rowsInserted = statement.executeUpdate();
        return rowsInserted > 0;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}

}