View on GitHub

PrimitiveApes

Tri 1: Java and Spring Intro

PrimitiveApes

Team Page

Home Project Owner + Requirements Project Ideas Technical Plan Team Roles Hosting Plan Project Documentation Technical Documentation

Database

@NonNull
    @Size(min = 1, message = "Name")
    private String name;

    private int table_number;


    private int period;

    /* Initializer used when setting data from an API */
    public Student(String name, int table_number, int period_number) {
        this.table_number = table_number;
        this.name = name;
        this.period = period_number;
    }
Student student = modelRepository.get(id);
        List<Note> notes = noteRepository.findAllByStudent(student);
        Note note = new Note();
        note.setStudent(student);

        for (Note n : notes)
            n.setText(n.getText());
            
            @PostMapping("/database/notes")
    public String notesAdd(@Valid Note note, BindingResult bindingResult) {
        // back to person ID on redirect
        String redirect = "redirect:/database/notes/"+note.getStudent().getId();

        // database errors
        if (bindingResult.hasErrors()) {
            return redirect;
        }

        // note is saved and person ID is pre-set
        noteRepository.save(note);
public  List<Student>listAll() {
        return jpa.findAll();
    }


    public  List<Student>listLikeNative(String term) {
        String like_term = String.format("%%%s%%",term);  // Like required % rappers
        return jpa.findByLikeTermNative(like_term);
    }

    public void save(Student student) {
        jpa.save(student);

    }

    public Student get(long id) {
        return (jpa.findById(id).isPresent())
                ? jpa.findById(id).get()
                : null;
    }

    public void delete(long id) {
        jpa.deleteById(id);
    }
<tbody>
                    <tr th:each="student : ${list}">
                        <td th:text="${student.id}"> ID</td>
                        <td th:text="${student.name}"> Name</td>
                        <td th:text="${student.table_number}">Table</td>
                        <td th:text="${student.period}">Period</td>

                        <td>
                            <a th:href="@{/database/student_update/{id}(id = ${student.id})}">Update</a>
                            <a th:href="@{/database/student_delete/{id}(id = ${student.id})}">Delete</a>
                            <a th:href="@{/database/notes/{id}(id = ${student.id})}">Notes</a>
                        </td>
                    </tr>

                    </tbody>

Randomization Algorithm

sets studentsPerTable = numStudents/numTables, and then randomly adds students to table groups by creating Student objects with their table # and a random student name from the names list. Removes names from names list so that students can not be added to multiple groups.

        studentsPerTable = numStudents / numTables;

        for (int i = 0; i < numTables; i++) {
            for (int j = 0; j < studentsPerTable; j++) {
                int index = (int) (Math.random() * names.size());
                StudentObject oneStudent = new StudentObject(i, names.get(index));
                groups.add(oneStudent);
                names.remove(index);
            }
        }

To deal with the case that numStudents is not divisble by numTables, we check that names is not empty (or names.size() > 0), and add the remaining students randomly to table 0 -> table n.

        int i = 0;
            while(names.size() > 0 && i < numTables){
                int index = (int) (Math.random() * names.size());
                StudentObject oneStudent = new StudentObject(i, names.get(index));
                groups.add(oneStudent);
                names.remove(index);
                i++;
            }

        return groups;

After doing this, though, the students may not ordered by their table group. Thus, I implemented a selection sort in order to ensure that students within the same table are next to oneanother inside the arraylist.

public static void sort(ArrayList<StudentObject> a){
        int n = a.size();

        for(int i =0;i < n-1; i++){
            int min_idx = i;
            for(int j = i+1; j < n; j++){
                if(a.get(j).getTableGroup() < a.get(min_idx).getTableGroup()){
                    min_idx = j;
                }
            }
            StudentObject temp = a.get(min_idx);
            a.set(min_idx, a.get(i));
            a.set(i, temp);
        }
    }