diff --git a/AttendanceService.java b/AttendanceService.java new file mode 100644 index 0000000..b5f3196 --- /dev/null +++ b/AttendanceService.java @@ -0,0 +1,53 @@ +package com.example.classroomattendance.service; + +import com.example.classroomattendance.model.Student; +import com.example.classroomattendance.repository.StudentRepository; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.io.InputStream; +import java.util.*; + +@Service +public class AttendanceService { + @Autowired + private StudentRepository studentRepository; + + public List getAllStudents() { + return studentRepository.findAll(); + } + + public void addPoints(Long studentId, double points) { + Optional student = studentRepository.findById(studentId); + student.ifPresent(s -> { + s.setPoints(s.getPoints() + points); + studentRepository.save(s); + }); + } + + public Student pickRandomStudent() { + List students = getAllStudents(); + Collections.shuffle(students, new Random(System.nanoTime())); + return students.get(0); + } + + public void loadStudentsFromExcel(InputStream inputStream) throws IOException { + List students = new ArrayList<>(); + try (Workbook workbook = new XSSFWorkbook(inputStream)) { + Sheet sheet = workbook.getSheetAt(0); + for (Row row : sheet) { + if (row.getRowNum() == 0) { + continue; + } + String name = row.getCell(0).getStringCellValue(); + String studentId = row.getCell(1).getStringCellValue(); + double points = row.getCell(2).getNumericCellValue(); + students.add(new Student(name, studentId, points)); + } + } + studentRepository.saveAll(students); + } +} \ No newline at end of file