From 847e43793dd91e314ee3354e2fcd81ae8f371fe2 Mon Sep 17 00:00:00 2001 From: pb8qzmito <13100778657@163.com> Date: Mon, 7 Oct 2024 12:23:45 +0800 Subject: [PATCH] ADD file via upload --- StudentService.java | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 StudentService.java diff --git a/StudentService.java b/StudentService.java new file mode 100644 index 0000000..5e9c4bb --- /dev/null +++ b/StudentService.java @@ -0,0 +1,62 @@ +package com.example.classroomattendance.service; + +import com.example.classroomattendance.model.Student; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +@Service +public class StudentService { + private List students = new ArrayList<>(); + private Random random = new Random(); + + public void addStudents(List studentList) { + students.addAll(studentList); + } + + public Student pickRandomStudent() { + if (students.isEmpty()) { + return null; + } + + // 计算所有学生的总权重 + double totalWeight = 0; + for (Student student : students) { + totalWeight += getWeight(student); + } + + // 随机选择权重范围内的值 + double randomValue = random.nextDouble() * totalWeight; + + // 选取符合该随机值的学生 + for (Student student : students) { + randomValue -= getWeight(student); + if (randomValue <= 0) { + return student; + } + } + + // 防止极端情况下返回最后一个学生 + return students.get(students.size() - 1); + } + + // 获取学生的权重,积分越高权重越低 + private double getWeight(Student student) { + return 1.0 / (student.getPoints() + 1.0); // 防止分母为0 + } + + public void updateStudentPoints(Long id, double points) { + for (Student student : students) { + if (student.getId().equals(id)) { + student.setPoints(student.getPoints() + points); + break; + } + } + } + + public List getAllStudents() { + return students; + } +} \ No newline at end of file