11 changed files with 1087 additions and 0 deletions
@ -0,0 +1,2 @@ |
|||||
|
# java |
||||
|
|
||||
@ -0,0 +1,5 @@ |
|||||
|
public class HelloWorld { |
||||
|
public static void main(String[] args) { |
||||
|
System.out.println("Hello, World!"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,74 @@ |
|||||
|
import java.util.Scanner; |
||||
|
|
||||
|
/** |
||||
|
* 温度转换器程序(Java版本) |
||||
|
* 支持摄氏度(C)与华氏度(F)之间互转 |
||||
|
* |
||||
|
* 对应Python原始程序的功能移植 |
||||
|
*/ |
||||
|
public class TemperatureConverter { |
||||
|
|
||||
|
/** |
||||
|
* 将摄氏度转换为华氏度 |
||||
|
* |
||||
|
* @param c 摄氏温度(浮点数) |
||||
|
* @return 对应的华氏温度(浮点数) |
||||
|
*/ |
||||
|
public static double celsiusToFahrenheit(double c) { |
||||
|
return c * 9.0 / 5.0 + 32.0; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将华氏度转换为摄氏度 |
||||
|
* |
||||
|
* @param f 华氏温度(浮点数) |
||||
|
* @return 对应的摄氏温度(浮点数) |
||||
|
*/ |
||||
|
public static double fahrenheitToCelsius(double f) { |
||||
|
return (f - 32.0) * 5.0 / 9.0; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
Scanner scanner = new Scanner(System.in); |
||||
|
|
||||
|
// 提示用户输入,格式示例:“36.6 C”或“97 F”
|
||||
|
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
||||
|
String input = scanner.nextLine().trim(); |
||||
|
|
||||
|
// 检查输入是否为空
|
||||
|
if (input.isEmpty()) { |
||||
|
System.out.println("输入为空,程序退出。"); |
||||
|
scanner.close(); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
// 按空格分割输入
|
||||
|
String[] parts = input.split("\\s+"); |
||||
|
|
||||
|
try { |
||||
|
// 允许用户输入两个部分:数值与单位
|
||||
|
double value = Double.parseDouble(parts[0]); |
||||
|
String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; |
||||
|
|
||||
|
// 判断单位并进行相应转换
|
||||
|
if (unit.startsWith("C")) { |
||||
|
// 从摄氏度转换为华氏度
|
||||
|
double f = celsiusToFahrenheit(value); |
||||
|
System.out.printf("%.2f °C = %.2f °F%n", value, f); |
||||
|
} else if (unit.startsWith("F")) { |
||||
|
// 从华氏度转换为摄氏度
|
||||
|
double c = fahrenheitToCelsius(value); |
||||
|
System.out.printf("%.2f °F = %.2f °C%n", value, c); |
||||
|
} else { |
||||
|
System.out.println("未知单位,请使用 C 或 F。"); |
||||
|
} |
||||
|
|
||||
|
} catch (NumberFormatException e) { |
||||
|
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
||||
|
} catch (ArrayIndexOutOfBoundsException e) { |
||||
|
System.out.println("输入格式错误,请按示例输入数值与单位,例如:36.6 C"); |
||||
|
} finally { |
||||
|
scanner.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
package com.example.datacollect.command; |
||||
|
|
||||
|
import com.example.datacollect.repository.ArticleRepository; |
||||
|
|
||||
|
public interface Command { |
||||
|
String getName(); |
||||
|
void execute(String[] args, ArticleRepository repository); |
||||
|
} |
||||
@ -0,0 +1,127 @@ |
|||||
|
package com.example.entity; |
||||
|
|
||||
|
import java.time.LocalDateTime; |
||||
|
|
||||
|
/** |
||||
|
* 课程实体类 |
||||
|
* 不依赖Spring Boot,使用普通Java类实现 |
||||
|
*/ |
||||
|
public class Course { |
||||
|
private Long id; |
||||
|
private String courseCode; |
||||
|
private String courseName; |
||||
|
private Double credit; |
||||
|
private String teacher; |
||||
|
private String department; |
||||
|
private Integer capacity; |
||||
|
private Integer enrolled; |
||||
|
private String classTime; |
||||
|
private String classRoom; |
||||
|
private String courseType; |
||||
|
private String semester; |
||||
|
private LocalDateTime createTime; |
||||
|
|
||||
|
public Long getId() { |
||||
|
return id; |
||||
|
} |
||||
|
|
||||
|
public void setId(Long id) { |
||||
|
this.id = id; |
||||
|
} |
||||
|
|
||||
|
public String getCourseCode() { |
||||
|
return courseCode; |
||||
|
} |
||||
|
|
||||
|
public void setCourseCode(String courseCode) { |
||||
|
this.courseCode = courseCode; |
||||
|
} |
||||
|
|
||||
|
public String getCourseName() { |
||||
|
return courseName; |
||||
|
} |
||||
|
|
||||
|
public void setCourseName(String courseName) { |
||||
|
this.courseName = courseName; |
||||
|
} |
||||
|
|
||||
|
public Double getCredit() { |
||||
|
return credit; |
||||
|
} |
||||
|
|
||||
|
public void setCredit(Double credit) { |
||||
|
this.credit = credit; |
||||
|
} |
||||
|
|
||||
|
public String getTeacher() { |
||||
|
return teacher; |
||||
|
} |
||||
|
|
||||
|
public void setTeacher(String teacher) { |
||||
|
this.teacher = teacher; |
||||
|
} |
||||
|
|
||||
|
public String getDepartment() { |
||||
|
return department; |
||||
|
} |
||||
|
|
||||
|
public void setDepartment(String department) { |
||||
|
this.department = department; |
||||
|
} |
||||
|
|
||||
|
public Integer getCapacity() { |
||||
|
return capacity; |
||||
|
} |
||||
|
|
||||
|
public void setCapacity(Integer capacity) { |
||||
|
this.capacity = capacity; |
||||
|
} |
||||
|
|
||||
|
public Integer getEnrolled() { |
||||
|
return enrolled; |
||||
|
} |
||||
|
|
||||
|
public void setEnrolled(Integer enrolled) { |
||||
|
this.enrolled = enrolled; |
||||
|
} |
||||
|
|
||||
|
public String getClassTime() { |
||||
|
return classTime; |
||||
|
} |
||||
|
|
||||
|
public void setClassTime(String classTime) { |
||||
|
this.classTime = classTime; |
||||
|
} |
||||
|
|
||||
|
public String getClassRoom() { |
||||
|
return classRoom; |
||||
|
} |
||||
|
|
||||
|
public void setClassRoom(String classRoom) { |
||||
|
this.classRoom = classRoom; |
||||
|
} |
||||
|
|
||||
|
public String getCourseType() { |
||||
|
return courseType; |
||||
|
} |
||||
|
|
||||
|
public void setCourseType(String courseType) { |
||||
|
this.courseType = courseType; |
||||
|
} |
||||
|
|
||||
|
public String getSemester() { |
||||
|
return semester; |
||||
|
} |
||||
|
|
||||
|
public void setSemester(String semester) { |
||||
|
this.semester = semester; |
||||
|
} |
||||
|
|
||||
|
public LocalDateTime getCreateTime() { |
||||
|
return createTime; |
||||
|
} |
||||
|
|
||||
|
public void setCreateTime(LocalDateTime createTime) { |
||||
|
this.createTime = createTime; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,261 @@ |
|||||
|
package com.example; |
||||
|
|
||||
|
import com.example.entity.Course; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* 课程分析类 |
||||
|
* 不依赖Spring Boot,使用普通Java类实现 |
||||
|
*/ |
||||
|
public class CourseAnalysis { |
||||
|
|
||||
|
// 无参构造方法
|
||||
|
public CourseAnalysis() { |
||||
|
} |
||||
|
|
||||
|
// 获取课程类型分布
|
||||
|
public Map<String, Integer> getCourseTypeDistribution() { |
||||
|
return DatabaseUtil.getCourseTypeDistribution(); |
||||
|
} |
||||
|
|
||||
|
// 获取院系课程分布
|
||||
|
public Map<String, Integer> getDepartmentDistribution() { |
||||
|
return DatabaseUtil.getDepartmentDistribution(); |
||||
|
} |
||||
|
|
||||
|
// 获取学分分布
|
||||
|
public Map<Double, Integer> getCreditDistribution() { |
||||
|
Map<Double, Integer> distribution = new HashMap<>(); |
||||
|
List<Course> courses = DatabaseUtil.getAllCourses(); |
||||
|
|
||||
|
for (Course course : courses) { |
||||
|
double credit = course.getCredit(); |
||||
|
distribution.put(credit, distribution.getOrDefault(credit, 0) + 1); |
||||
|
} |
||||
|
|
||||
|
return distribution; |
||||
|
} |
||||
|
|
||||
|
// 获取热门课程
|
||||
|
public List<Map<String, Object>> getTopCourses() { |
||||
|
return DatabaseUtil.getTopCourses(); |
||||
|
} |
||||
|
|
||||
|
// 获取课程容量使用率
|
||||
|
public List<Map<String, Object>> getCourseUsageRate() { |
||||
|
List<Map<String, Object>> usageRates = new ArrayList<>(); |
||||
|
List<Course> courses = DatabaseUtil.getAllCourses(); |
||||
|
|
||||
|
for (Course course : courses) { |
||||
|
Map<String, Object> usageRate = new HashMap<>(); |
||||
|
usageRate.put("courseName", course.getCourseName()); |
||||
|
usageRate.put("capacity", course.getCapacity()); |
||||
|
usageRate.put("enrolled", course.getEnrolled()); |
||||
|
double rate = course.getCapacity() > 0 ? (double) course.getEnrolled() / course.getCapacity() * 100 : 0; |
||||
|
usageRate.put("usageRate", rate); |
||||
|
usageRates.add(usageRate); |
||||
|
} |
||||
|
|
||||
|
// 按使用率排序
|
||||
|
usageRates.sort((a, b) -> Double.compare((Double) b.get("usageRate"), (Double) a.get("usageRate"))); |
||||
|
|
||||
|
return usageRates; |
||||
|
} |
||||
|
|
||||
|
// 获取整体统计信息
|
||||
|
public Map<String, Object> getOverallStatistics() { |
||||
|
Map<String, Object> statistics = new HashMap<>(); |
||||
|
try { |
||||
|
List<Course> allCourses = DatabaseUtil.getAllCourses(); |
||||
|
|
||||
|
// 总课程数
|
||||
|
statistics.put("totalCourses", allCourses.size()); |
||||
|
|
||||
|
// 总学分
|
||||
|
double totalCredits = allCourses.stream() |
||||
|
.mapToDouble(Course::getCredit) |
||||
|
.sum(); |
||||
|
statistics.put("totalCredits", totalCredits); |
||||
|
|
||||
|
// 平均学分
|
||||
|
double avgCredit = allCourses.isEmpty() ? 0 : totalCredits / allCourses.size(); |
||||
|
statistics.put("averageCredit", avgCredit); |
||||
|
|
||||
|
// 总容量
|
||||
|
int totalCapacity = allCourses.stream() |
||||
|
.mapToInt(Course::getCapacity) |
||||
|
.sum(); |
||||
|
statistics.put("totalCapacity", totalCapacity); |
||||
|
|
||||
|
// 总已选人数
|
||||
|
int totalEnrolled = allCourses.stream() |
||||
|
.mapToInt(Course::getEnrolled) |
||||
|
.sum(); |
||||
|
statistics.put("totalEnrolled", totalEnrolled); |
||||
|
|
||||
|
// 总体使用率
|
||||
|
double overallUsageRate = totalCapacity > 0 ? (double) totalEnrolled / totalCapacity * 100 : 0; |
||||
|
statistics.put("overallUsageRate", overallUsageRate); |
||||
|
|
||||
|
// 课程类型数量
|
||||
|
long requiredCourses = allCourses.stream() |
||||
|
.filter(course -> "必修课".equals(course.getCourseType())) |
||||
|
.count(); |
||||
|
statistics.put("requiredCourses", requiredCourses); |
||||
|
|
||||
|
long electiveCourses = allCourses.stream() |
||||
|
.filter(course -> "选修课".equals(course.getCourseType())) |
||||
|
.count(); |
||||
|
statistics.put("electiveCourses", electiveCourses); |
||||
|
|
||||
|
// 院系数量
|
||||
|
long departmentCount = allCourses.stream() |
||||
|
.map(Course::getDepartment) |
||||
|
.distinct() |
||||
|
.count(); |
||||
|
statistics.put("departmentCount", departmentCount); |
||||
|
} catch (Exception e) { |
||||
|
System.err.println("获取整体统计信息失败:" + e.getMessage()); |
||||
|
// 返回默认值
|
||||
|
statistics.put("totalCourses", 0); |
||||
|
statistics.put("totalCredits", 0.0); |
||||
|
statistics.put("averageCredit", 0.0); |
||||
|
statistics.put("totalCapacity", 0); |
||||
|
statistics.put("totalEnrolled", 0); |
||||
|
statistics.put("overallUsageRate", 0.0); |
||||
|
statistics.put("requiredCourses", 0); |
||||
|
statistics.put("electiveCourses", 0); |
||||
|
statistics.put("departmentCount", 0); |
||||
|
} |
||||
|
return statistics; |
||||
|
} |
||||
|
|
||||
|
// 获取按院系分组的课程统计
|
||||
|
public Map<String, Map<String, Object>> getDepartmentStatistics() { |
||||
|
Map<String, Map<String, Object>> deptStats = new HashMap<>(); |
||||
|
try { |
||||
|
List<Course> allCourses = DatabaseUtil.getAllCourses(); |
||||
|
|
||||
|
// 按院系分组
|
||||
|
Map<String, List<Course>> coursesByDept = allCourses.stream() |
||||
|
.collect(Collectors.groupingBy(Course::getDepartment)); |
||||
|
|
||||
|
for (Map.Entry<String, List<Course>> entry : coursesByDept.entrySet()) { |
||||
|
String department = entry.getKey(); |
||||
|
List<Course> deptCourses = entry.getValue(); |
||||
|
|
||||
|
Map<String, Object> stats = new HashMap<>(); |
||||
|
stats.put("courseCount", deptCourses.size()); |
||||
|
|
||||
|
double deptCredits = deptCourses.stream() |
||||
|
.mapToDouble(Course::getCredit) |
||||
|
.sum(); |
||||
|
stats.put("totalCredits", deptCredits); |
||||
|
|
||||
|
int deptCapacity = deptCourses.stream() |
||||
|
.mapToInt(Course::getCapacity) |
||||
|
.sum(); |
||||
|
stats.put("totalCapacity", deptCapacity); |
||||
|
|
||||
|
int deptEnrolled = deptCourses.stream() |
||||
|
.mapToInt(Course::getEnrolled) |
||||
|
.sum(); |
||||
|
stats.put("totalEnrolled", deptEnrolled); |
||||
|
|
||||
|
double deptUsageRate = deptCapacity > 0 ? (double) deptEnrolled / deptCapacity * 100 : 0; |
||||
|
stats.put("usageRate", deptUsageRate); |
||||
|
|
||||
|
deptStats.put(department, stats); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
System.err.println("获取院系统计信息失败:" + e.getMessage()); |
||||
|
} |
||||
|
return deptStats; |
||||
|
} |
||||
|
|
||||
|
// 获取教师课程统计
|
||||
|
public Map<String, Map<String, Object>> getTeacherStatistics() { |
||||
|
Map<String, Map<String, Object>> teacherStats = new HashMap<>(); |
||||
|
try { |
||||
|
List<Course> allCourses = DatabaseUtil.getAllCourses(); |
||||
|
|
||||
|
// 按教师分组
|
||||
|
Map<String, List<Course>> coursesByTeacher = allCourses.stream() |
||||
|
.collect(Collectors.groupingBy(Course::getTeacher)); |
||||
|
|
||||
|
for (Map.Entry<String, List<Course>> entry : coursesByTeacher.entrySet()) { |
||||
|
String teacher = entry.getKey(); |
||||
|
List<Course> teacherCourses = entry.getValue(); |
||||
|
|
||||
|
Map<String, Object> stats = new HashMap<>(); |
||||
|
stats.put("courseCount", teacherCourses.size()); |
||||
|
|
||||
|
double totalCredits = teacherCourses.stream() |
||||
|
.mapToDouble(Course::getCredit) |
||||
|
.sum(); |
||||
|
stats.put("totalCredits", totalCredits); |
||||
|
|
||||
|
int totalEnrolled = teacherCourses.stream() |
||||
|
.mapToInt(Course::getEnrolled) |
||||
|
.sum(); |
||||
|
stats.put("totalEnrolled", totalEnrolled); |
||||
|
|
||||
|
teacherStats.put(teacher, stats); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
System.err.println("获取教师统计信息失败:" + e.getMessage()); |
||||
|
} |
||||
|
return teacherStats; |
||||
|
} |
||||
|
|
||||
|
// 获取课程容量利用率分析
|
||||
|
public Map<String, Object> getCapacityAnalysis() { |
||||
|
Map<String, Object> analysis = new HashMap<>(); |
||||
|
try { |
||||
|
List<Course> allCourses = DatabaseUtil.getAllCourses(); |
||||
|
|
||||
|
int totalCapacity = allCourses.stream() |
||||
|
.mapToInt(Course::getCapacity) |
||||
|
.sum(); |
||||
|
|
||||
|
int totalEnrolled = allCourses.stream() |
||||
|
.mapToInt(Course::getEnrolled) |
||||
|
.sum(); |
||||
|
|
||||
|
int availableCapacity = totalCapacity - totalEnrolled; |
||||
|
|
||||
|
// 计算不同使用率区间的课程数量
|
||||
|
long highUsage = allCourses.stream() |
||||
|
.filter(course -> course.getCapacity() > 0 && |
||||
|
(double) course.getEnrolled() / course.getCapacity() >= 0.9) |
||||
|
.count(); |
||||
|
|
||||
|
long mediumUsage = allCourses.stream() |
||||
|
.filter(course -> course.getCapacity() > 0 && |
||||
|
(double) course.getEnrolled() / course.getCapacity() >= 0.5 && |
||||
|
(double) course.getEnrolled() / course.getCapacity() < 0.9) |
||||
|
.count(); |
||||
|
|
||||
|
long lowUsage = allCourses.stream() |
||||
|
.filter(course -> course.getCapacity() > 0 && |
||||
|
(double) course.getEnrolled() / course.getCapacity() < 0.5) |
||||
|
.count(); |
||||
|
|
||||
|
analysis.put("totalCapacity", totalCapacity); |
||||
|
analysis.put("totalEnrolled", totalEnrolled); |
||||
|
analysis.put("availableCapacity", availableCapacity); |
||||
|
analysis.put("highUsageCourses", highUsage); |
||||
|
analysis.put("mediumUsageCourses", mediumUsage); |
||||
|
analysis.put("lowUsageCourses", lowUsage); |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
System.err.println("获取容量分析失败:" + e.getMessage()); |
||||
|
} |
||||
|
return analysis; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,136 @@ |
|||||
|
package com.example; |
||||
|
|
||||
|
import com.example.entity.Course; |
||||
|
|
||||
|
/** |
||||
|
* 测试类,用于验证湖大选课系统功能 |
||||
|
*/ |
||||
|
public class CourseSystemTest { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
// 初始化数据库
|
||||
|
DatabaseUtil.initDatabase(); |
||||
|
|
||||
|
// 清空现有数据
|
||||
|
DatabaseUtil.clearCourses(); |
||||
|
|
||||
|
// 添加测试数据
|
||||
|
addTestData(); |
||||
|
|
||||
|
// 创建CourseAnalysis实例
|
||||
|
CourseAnalysis analysis = new CourseAnalysis(); |
||||
|
|
||||
|
// 测试整体统计信息
|
||||
|
System.out.println("===== 测试整体统计信息 ====="); |
||||
|
System.out.println(analysis.getOverallStatistics()); |
||||
|
|
||||
|
// 测试课程类型分布
|
||||
|
System.out.println("\n===== 测试课程类型分布 ====="); |
||||
|
System.out.println(analysis.getCourseTypeDistribution()); |
||||
|
|
||||
|
// 测试院系统计
|
||||
|
System.out.println("\n===== 测试院系统计 ====="); |
||||
|
System.out.println(analysis.getDepartmentDistribution()); |
||||
|
|
||||
|
// 测试学分分布
|
||||
|
System.out.println("\n===== 测试学分分布 ====="); |
||||
|
System.out.println(analysis.getCreditDistribution()); |
||||
|
|
||||
|
// 测试热门课程
|
||||
|
System.out.println("\n===== 测试热门课程 ====="); |
||||
|
System.out.println(analysis.getTopCourses()); |
||||
|
|
||||
|
// 测试课程容量使用率
|
||||
|
System.out.println("\n===== 测试课程容量使用率 ====="); |
||||
|
System.out.println(analysis.getCourseUsageRate()); |
||||
|
|
||||
|
// 测试按院系分组的统计
|
||||
|
System.out.println("\n===== 测试按院系分组的统计 ====="); |
||||
|
System.out.println(analysis.getDepartmentStatistics()); |
||||
|
|
||||
|
// 测试教师课程统计
|
||||
|
System.out.println("\n===== 测试教师课程统计 ====="); |
||||
|
System.out.println(analysis.getTeacherStatistics()); |
||||
|
|
||||
|
// 测试课程容量利用率分析
|
||||
|
System.out.println("\n===== 测试课程容量利用率分析 ====="); |
||||
|
System.out.println(analysis.getCapacityAnalysis()); |
||||
|
|
||||
|
System.out.println("\n===== 测试完成 ====="); |
||||
|
} |
||||
|
|
||||
|
// 添加测试数据
|
||||
|
private static void addTestData() { |
||||
|
Course course1 = new Course(); |
||||
|
course1.setCourseCode("CS101"); |
||||
|
course1.setCourseName("计算机科学导论"); |
||||
|
course1.setCredit(3.0); |
||||
|
course1.setTeacher("张教授"); |
||||
|
course1.setDepartment("计算机学院"); |
||||
|
course1.setCapacity(100); |
||||
|
course1.setEnrolled(95); |
||||
|
course1.setClassTime("周一 8:00-10:00"); |
||||
|
course1.setClassRoom("A101"); |
||||
|
course1.setCourseType("必修课"); |
||||
|
course1.setSemester("2024春季"); |
||||
|
DatabaseUtil.saveCourse(course1); |
||||
|
|
||||
|
Course course2 = new Course(); |
||||
|
course2.setCourseCode("CS102"); |
||||
|
course2.setCourseName("数据结构"); |
||||
|
course2.setCredit(4.0); |
||||
|
course2.setTeacher("李教授"); |
||||
|
course2.setDepartment("计算机学院"); |
||||
|
course2.setCapacity(80); |
||||
|
course2.setEnrolled(75); |
||||
|
course2.setClassTime("周二 10:00-12:00"); |
||||
|
course2.setClassRoom("A102"); |
||||
|
course2.setCourseType("必修课"); |
||||
|
course2.setSemester("2024春季"); |
||||
|
DatabaseUtil.saveCourse(course2); |
||||
|
|
||||
|
Course course3 = new Course(); |
||||
|
course3.setCourseCode("MATH101"); |
||||
|
course3.setCourseName("高等数学"); |
||||
|
course3.setCredit(5.0); |
||||
|
course3.setTeacher("王教授"); |
||||
|
course3.setDepartment("数学学院"); |
||||
|
course3.setCapacity(120); |
||||
|
course3.setEnrolled(110); |
||||
|
course3.setClassTime("周三 8:00-10:00"); |
||||
|
course3.setClassRoom("B101"); |
||||
|
course3.setCourseType("必修课"); |
||||
|
course3.setSemester("2024春季"); |
||||
|
DatabaseUtil.saveCourse(course3); |
||||
|
|
||||
|
Course course4 = new Course(); |
||||
|
course4.setCourseCode("ENG101"); |
||||
|
course4.setCourseName("大学英语"); |
||||
|
course4.setCredit(2.0); |
||||
|
course4.setTeacher("刘教授"); |
||||
|
course4.setDepartment("外国语学院"); |
||||
|
course4.setCapacity(150); |
||||
|
course4.setEnrolled(120); |
||||
|
course4.setClassTime("周四 14:00-16:00"); |
||||
|
course4.setClassRoom("C101"); |
||||
|
course4.setCourseType("选修课"); |
||||
|
course4.setSemester("2024春季"); |
||||
|
DatabaseUtil.saveCourse(course4); |
||||
|
|
||||
|
Course course5 = new Course(); |
||||
|
course5.setCourseCode("PHYS101"); |
||||
|
course5.setCourseName("大学物理"); |
||||
|
course5.setCredit(4.0); |
||||
|
course5.setTeacher("陈教授"); |
||||
|
course5.setDepartment("物理学院"); |
||||
|
course5.setCapacity(90); |
||||
|
course5.setEnrolled(85); |
||||
|
course5.setClassTime("周五 10:00-12:00"); |
||||
|
course5.setClassRoom("D101"); |
||||
|
course5.setCourseType("必修课"); |
||||
|
course5.setSemester("2024春季"); |
||||
|
DatabaseUtil.saveCourse(course5); |
||||
|
|
||||
|
System.out.println("测试数据添加完成,共添加 5 门课程"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,195 @@ |
|||||
|
package com.example; |
||||
|
|
||||
|
import com.example.entity.Course; |
||||
|
|
||||
|
import java.sql.*; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.HashMap; |
||||
|
|
||||
|
/** |
||||
|
* 数据库工具类 |
||||
|
* 提供数据库操作相关方法 |
||||
|
*/ |
||||
|
public class DatabaseUtil { |
||||
|
// 数据库URL
|
||||
|
private static final String DB_URL = "jdbc:sqlite:course.db"; |
||||
|
|
||||
|
// 静态初始化块,加载SQLite驱动
|
||||
|
static { |
||||
|
try { |
||||
|
Class.forName("org.sqlite.JDBC"); |
||||
|
System.out.println("SQLite驱动加载成功"); |
||||
|
} catch (ClassNotFoundException e) { |
||||
|
System.err.println("SQLite驱动加载失败: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 初始化数据库
|
||||
|
public static void initDatabase() { |
||||
|
try (Connection conn = DriverManager.getConnection(DB_URL); |
||||
|
Statement stmt = conn.createStatement()) { |
||||
|
|
||||
|
// 创建courses表
|
||||
|
String createTableSQL = "CREATE TABLE IF NOT EXISTS courses (" + |
||||
|
"id INTEGER PRIMARY KEY AUTOINCREMENT," + |
||||
|
"course_code TEXT," + |
||||
|
"course_name TEXT," + |
||||
|
"credit REAL," + |
||||
|
"teacher TEXT," + |
||||
|
"department TEXT," + |
||||
|
"capacity INTEGER," + |
||||
|
"enrolled INTEGER," + |
||||
|
"class_time TEXT," + |
||||
|
"class_room TEXT," + |
||||
|
"course_type TEXT," + |
||||
|
"semester TEXT," + |
||||
|
"create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + |
||||
|
")"; |
||||
|
stmt.executeUpdate(createTableSQL); |
||||
|
|
||||
|
System.out.println("数据库初始化成功"); |
||||
|
} catch (SQLException e) { |
||||
|
System.err.println("数据库初始化失败: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 获取所有课程
|
||||
|
public static List<Course> getAllCourses() { |
||||
|
List<Course> courses = new ArrayList<>(); |
||||
|
String sql = "SELECT * FROM courses"; |
||||
|
|
||||
|
try (Connection conn = DriverManager.getConnection(DB_URL); |
||||
|
Statement stmt = conn.createStatement(); |
||||
|
ResultSet rs = stmt.executeQuery(sql)) { |
||||
|
|
||||
|
while (rs.next()) { |
||||
|
Course course = new Course(); |
||||
|
course.setId(rs.getLong("id")); |
||||
|
course.setCourseCode(rs.getString("course_code")); |
||||
|
course.setCourseName(rs.getString("course_name")); |
||||
|
course.setCredit(rs.getDouble("credit")); |
||||
|
course.setTeacher(rs.getString("teacher")); |
||||
|
course.setDepartment(rs.getString("department")); |
||||
|
course.setCapacity(rs.getInt("capacity")); |
||||
|
course.setEnrolled(rs.getInt("enrolled")); |
||||
|
course.setClassTime(rs.getString("class_time")); |
||||
|
course.setClassRoom(rs.getString("class_room")); |
||||
|
course.setCourseType(rs.getString("course_type")); |
||||
|
course.setSemester(rs.getString("semester")); |
||||
|
courses.add(course); |
||||
|
} |
||||
|
} catch (SQLException e) { |
||||
|
System.err.println("获取课程列表失败: " + e.getMessage()); |
||||
|
} |
||||
|
|
||||
|
return courses; |
||||
|
} |
||||
|
|
||||
|
// 获取课程类型分布
|
||||
|
public static Map<String, Integer> getCourseTypeDistribution() { |
||||
|
Map<String, Integer> distribution = new HashMap<>(); |
||||
|
String sql = "SELECT course_type, COUNT(*) as count FROM courses GROUP BY course_type"; |
||||
|
|
||||
|
try (Connection conn = DriverManager.getConnection(DB_URL); |
||||
|
Statement stmt = conn.createStatement(); |
||||
|
ResultSet rs = stmt.executeQuery(sql)) { |
||||
|
|
||||
|
while (rs.next()) { |
||||
|
String type = rs.getString("course_type"); |
||||
|
int count = rs.getInt("count"); |
||||
|
distribution.put(type, count); |
||||
|
} |
||||
|
} catch (SQLException e) { |
||||
|
System.err.println("获取课程类型分布失败: " + e.getMessage()); |
||||
|
} |
||||
|
|
||||
|
return distribution; |
||||
|
} |
||||
|
|
||||
|
// 获取院系分布
|
||||
|
public static Map<String, Integer> getDepartmentDistribution() { |
||||
|
Map<String, Integer> distribution = new HashMap<>(); |
||||
|
String sql = "SELECT department, COUNT(*) as count FROM courses GROUP BY department"; |
||||
|
|
||||
|
try (Connection conn = DriverManager.getConnection(DB_URL); |
||||
|
Statement stmt = conn.createStatement(); |
||||
|
ResultSet rs = stmt.executeQuery(sql)) { |
||||
|
|
||||
|
while (rs.next()) { |
||||
|
String department = rs.getString("department"); |
||||
|
int count = rs.getInt("count"); |
||||
|
distribution.put(department, count); |
||||
|
} |
||||
|
} catch (SQLException e) { |
||||
|
System.err.println("获取院系分布失败: " + e.getMessage()); |
||||
|
} |
||||
|
|
||||
|
return distribution; |
||||
|
} |
||||
|
|
||||
|
// 获取热门课程
|
||||
|
public static List<Map<String, Object>> getTopCourses() { |
||||
|
List<Map<String, Object>> topCourses = new ArrayList<>(); |
||||
|
String sql = "SELECT course_name, teacher, department, capacity, enrolled FROM courses ORDER BY enrolled DESC LIMIT 10"; |
||||
|
|
||||
|
try (Connection conn = DriverManager.getConnection(DB_URL); |
||||
|
Statement stmt = conn.createStatement(); |
||||
|
ResultSet rs = stmt.executeQuery(sql)) { |
||||
|
|
||||
|
while (rs.next()) { |
||||
|
Map<String, Object> course = new HashMap<>(); |
||||
|
course.put("courseName", rs.getString("course_name")); |
||||
|
course.put("teacher", rs.getString("teacher")); |
||||
|
course.put("department", rs.getString("department")); |
||||
|
course.put("capacity", rs.getInt("capacity")); |
||||
|
course.put("enrolled", rs.getInt("enrolled")); |
||||
|
topCourses.add(course); |
||||
|
} |
||||
|
} catch (SQLException e) { |
||||
|
System.err.println("获取热门课程失败: " + e.getMessage()); |
||||
|
} |
||||
|
|
||||
|
return topCourses; |
||||
|
} |
||||
|
|
||||
|
// 保存课程
|
||||
|
public static void saveCourse(Course course) { |
||||
|
String sql = "INSERT INTO courses (course_code, course_name, credit, teacher, department, capacity, enrolled, class_time, class_room, course_type, semester) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
||||
|
|
||||
|
try (Connection conn = DriverManager.getConnection(DB_URL); |
||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) { |
||||
|
|
||||
|
pstmt.setString(1, course.getCourseCode()); |
||||
|
pstmt.setString(2, course.getCourseName()); |
||||
|
pstmt.setDouble(3, course.getCredit()); |
||||
|
pstmt.setString(4, course.getTeacher()); |
||||
|
pstmt.setString(5, course.getDepartment()); |
||||
|
pstmt.setInt(6, course.getCapacity()); |
||||
|
pstmt.setInt(7, course.getEnrolled()); |
||||
|
pstmt.setString(8, course.getClassTime()); |
||||
|
pstmt.setString(9, course.getClassRoom()); |
||||
|
pstmt.setString(10, course.getCourseType()); |
||||
|
pstmt.setString(11, course.getSemester()); |
||||
|
|
||||
|
pstmt.executeUpdate(); |
||||
|
} catch (SQLException e) { |
||||
|
System.err.println("保存课程失败: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 清空课程数据
|
||||
|
public static void clearCourses() { |
||||
|
String sql = "DELETE FROM courses"; |
||||
|
|
||||
|
try (Connection conn = DriverManager.getConnection(DB_URL); |
||||
|
Statement stmt = conn.createStatement()) { |
||||
|
|
||||
|
stmt.executeUpdate(sql); |
||||
|
System.out.println("课程数据清空成功"); |
||||
|
} catch (SQLException e) { |
||||
|
System.err.println("清空课程数据失败: " + e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,127 @@ |
|||||
|
package com.example; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.Scanner; |
||||
|
|
||||
|
/** |
||||
|
* 湖大选课系统主应用类 |
||||
|
* 不依赖Spring Boot,使用普通Java类实现 |
||||
|
*/ |
||||
|
public class HnuCourseSystem { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
// 初始化数据库
|
||||
|
DatabaseUtil.initDatabase(); |
||||
|
|
||||
|
// 创建CourseAnalysis实例
|
||||
|
CourseAnalysis analysis = new CourseAnalysis(); |
||||
|
|
||||
|
// 显示菜单
|
||||
|
try (Scanner scanner = new Scanner(System.in)) { |
||||
|
int choice; |
||||
|
|
||||
|
do { |
||||
|
System.out.println("===== 湖大选课系统 ====="); |
||||
|
System.out.println("1. 查看整体统计信息"); |
||||
|
System.out.println("2. 查看课程类型分布"); |
||||
|
System.out.println("3. 查看院系统计"); |
||||
|
System.out.println("4. 查看学分分布"); |
||||
|
System.out.println("5. 查看热门课程"); |
||||
|
System.out.println("6. 查看课程容量使用率"); |
||||
|
System.out.println("7. 查看按院系分组的统计"); |
||||
|
System.out.println("0. 退出系统"); |
||||
|
System.out.print("请选择操作: "); |
||||
|
|
||||
|
choice = scanner.nextInt(); |
||||
|
scanner.nextLine(); // 消费换行符
|
||||
|
|
||||
|
switch (choice) { |
||||
|
case 1 -> { |
||||
|
// 查看整体统计信息
|
||||
|
System.out.println("\n整体统计信息:"); |
||||
|
Map<String, Object> overallStats = analysis.getOverallStatistics(); |
||||
|
for (Map.Entry<String, Object> entry : overallStats.entrySet()) { |
||||
|
System.out.println(entry.getKey() + ": " + entry.getValue()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
case 2 -> { |
||||
|
// 查看课程类型分布
|
||||
|
System.out.println("\n课程类型分布:"); |
||||
|
Map<String, Integer> typeDistribution = analysis.getCourseTypeDistribution(); |
||||
|
for (Map.Entry<String, Integer> entry : typeDistribution.entrySet()) { |
||||
|
System.out.println(entry.getKey() + ": " + entry.getValue()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
case 3 -> { |
||||
|
// 查看院系统计
|
||||
|
System.out.println("\n院系统计:"); |
||||
|
Map<String, Integer> deptDistribution = analysis.getDepartmentDistribution(); |
||||
|
for (Map.Entry<String, Integer> entry : deptDistribution.entrySet()) { |
||||
|
System.out.println(entry.getKey() + ": " + entry.getValue()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
case 4 -> { |
||||
|
// 查看学分分布
|
||||
|
System.out.println("\n学分分布:"); |
||||
|
Map<Double, Integer> creditDistribution = analysis.getCreditDistribution(); |
||||
|
for (Map.Entry<Double, Integer> entry : creditDistribution.entrySet()) { |
||||
|
System.out.println(entry.getKey() + "学分: " + entry.getValue() + "门"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
case 5 -> { |
||||
|
// 查看热门课程
|
||||
|
System.out.println("\n热门课程:"); |
||||
|
List<Map<String, Object>> topCourses = analysis.getTopCourses(); |
||||
|
for (int i = 0; i < topCourses.size(); i++) { |
||||
|
Map<String, Object> course = topCourses.get(i); |
||||
|
System.out.println((i + 1) + ". " + course.get("courseName") + " - " + course.get("teacher")); |
||||
|
System.out.println(" 院系: " + course.get("department") + ", 容量: " + course.get("capacity") + ", 已选: " + course.get("enrolled")); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
case 6 -> { |
||||
|
// 查看课程容量使用率
|
||||
|
System.out.println("\n课程容量使用率:"); |
||||
|
List<Map<String, Object>> usageRates = analysis.getCourseUsageRate(); |
||||
|
for (int i = 0; i < Math.min(10, usageRates.size()); i++) { |
||||
|
Map<String, Object> usage = usageRates.get(i); |
||||
|
System.out.println((i + 1) + ". " + usage.get("courseName")); |
||||
|
System.out.println(" 容量: " + usage.get("capacity") + ", 已选: " + usage.get("enrolled") + ", 使用率: " + String.format("%.2f%%", usage.get("usageRate"))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
case 7 -> { |
||||
|
// 查看按院系分组的统计
|
||||
|
System.out.println("\n按院系分组的统计:"); |
||||
|
Map<String, Map<String, Object>> deptStats = analysis.getDepartmentStatistics(); |
||||
|
for (Map.Entry<String, Map<String, Object>> entry : deptStats.entrySet()) { |
||||
|
String department = entry.getKey(); |
||||
|
Map<String, Object> stats = entry.getValue(); |
||||
|
System.out.println("院系: " + department); |
||||
|
System.out.println(" 课程数量: " + stats.get("courseCount")); |
||||
|
System.out.println(" 总学分: " + stats.get("totalCredits")); |
||||
|
System.out.println(" 总容量: " + stats.get("totalCapacity")); |
||||
|
System.out.println(" 总已选人数: " + stats.get("totalEnrolled")); |
||||
|
System.out.println(" 使用率: " + String.format("%.2f%%", stats.get("usageRate"))); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
case 0 -> System.out.println("退出系统..."); |
||||
|
|
||||
|
default -> System.out.println("无效选择,请重新输入"); |
||||
|
} |
||||
|
|
||||
|
if (choice != 0) { |
||||
|
System.out.println("\n按Enter键继续..."); |
||||
|
scanner.nextLine(); |
||||
|
} |
||||
|
|
||||
|
} while (choice != 0); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,89 @@ |
|||||
|
package com.example; |
||||
|
|
||||
|
/** |
||||
|
* 泛型Pair类,用于存储两个不同类型的值 |
||||
|
* @param <K> 键的类型 |
||||
|
* @param <V> 值的类型 |
||||
|
*/ |
||||
|
public class Pair<K, V> { |
||||
|
private K key; |
||||
|
private V value; |
||||
|
|
||||
|
/** |
||||
|
* 构造方法 |
||||
|
* @param key 键 |
||||
|
* @param value 值 |
||||
|
*/ |
||||
|
public Pair(K key, V value) { |
||||
|
this.key = key; |
||||
|
this.value = value; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取键 |
||||
|
* @return 键 |
||||
|
*/ |
||||
|
public K getKey() { |
||||
|
return key; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置键 |
||||
|
* @param key 键 |
||||
|
*/ |
||||
|
public void setKey(K key) { |
||||
|
this.key = key; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取值 |
||||
|
* @return 值 |
||||
|
*/ |
||||
|
public V getValue() { |
||||
|
return value; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 设置值 |
||||
|
* @param value 值 |
||||
|
*/ |
||||
|
public void setValue(V value) { |
||||
|
this.value = value; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return "Pair{" + |
||||
|
"key=" + key + |
||||
|
", value=" + value + |
||||
|
'}'; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
Pair<?, ?> pair = (Pair<?, ?>) o; |
||||
|
if (key != null ? !key.equals(pair.key) : pair.key != null) return false; |
||||
|
return value != null ? value.equals(pair.value) : pair.value == null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
int result = key != null ? key.hashCode() : 0; |
||||
|
result = 31 * result + (value != null ? value.hashCode() : 0); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 静态工厂方法,创建Pair实例 |
||||
|
* @param key 键 |
||||
|
* @param value 值 |
||||
|
* @param <K> 键的类型 |
||||
|
* @param <V> 值的类型 |
||||
|
* @return Pair实例 |
||||
|
*/ |
||||
|
public static <K, V> Pair<K, V> of(K key, V value) { |
||||
|
return new Pair<>(key, value); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<groupId>com.example</groupId> |
||||
|
<artifactId>hnu-course-analysis</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
<packaging>jar</packaging> |
||||
|
|
||||
|
<name>湖南大学选课系统信息爬取与分析</name> |
||||
|
<description>湖南大学选课系统课程信息爬取、整理与可视化分析系统</description> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>17</maven.compiler.source> |
||||
|
<maven.compiler.target>17</maven.compiler.target> |
||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
||||
|
<sqlite.version>3.44.1.0</sqlite.version> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.xerial</groupId> |
||||
|
<artifactId>sqlite-jdbc</artifactId> |
||||
|
<version>${sqlite.version}</version> |
||||
|
</dependency> |
||||
|
<dependency> |
||||
|
<groupId>org.slf4j</groupId> |
||||
|
<artifactId>slf4j-simple</artifactId> |
||||
|
<version>2.0.13</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
<build> |
||||
|
<plugins> |
||||
|
<plugin> |
||||
|
<groupId>org.apache.maven.plugins</groupId> |
||||
|
<artifactId>maven-compiler-plugin</artifactId> |
||||
|
<version>3.11.0</version> |
||||
|
<configuration> |
||||
|
<source>17</source> |
||||
|
<target>17</target> |
||||
|
</configuration> |
||||
|
</plugin> |
||||
|
<plugin> |
||||
|
<groupId>org.codehaus.mojo</groupId> |
||||
|
<artifactId>exec-maven-plugin</artifactId> |
||||
|
<version>3.1.0</version> |
||||
|
<configuration> |
||||
|
<mainClass>com.example.CourseSystemTest</mainClass> |
||||
|
</configuration> |
||||
|
<executions> |
||||
|
<execution> |
||||
|
<goals> |
||||
|
<goal>java</goal> |
||||
|
</goals> |
||||
|
</execution> |
||||
|
</executions> |
||||
|
</plugin> |
||||
|
</plugins> |
||||
|
</build> |
||||
|
</project> |
||||
Loading…
Reference in new issue