From 926c28fd9a688241a57ec6294a70861a19378ae8 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Mon, 22 Oct 2018 16:23:21 +0200 Subject: [PATCH] Fixed bug in team generation code. --- src/main/java/nl/andrewlalis/Main.java | 1 + .../java/nl/andrewlalis/model/Student.java | 20 ++++++++++++++++++- .../nl/andrewlalis/model/StudentTeam.java | 2 +- .../view/table_models/StudentTableModel.java | 5 ++++- .../java/nl/andrewlalis/util/Logging.java | 7 +++++-- .../nl/andrewlalis/util/TeamGenerator.java | 2 ++ 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/nl/andrewlalis/Main.java b/src/main/java/nl/andrewlalis/Main.java index 0f3658a..af2f226 100644 --- a/src/main/java/nl/andrewlalis/Main.java +++ b/src/main/java/nl/andrewlalis/Main.java @@ -60,6 +60,7 @@ public class Main { private static void initializeTestingData() { try { List teams = TeamGenerator.generateFromCSV("/home/andrew/Documents/School/ta/GithubInitializer/sampleAOOP.csv", 2); + System.out.println(teams); DbHelper.saveStudentTeams(teams); managementView.updateModels(); } catch (IOException e) { diff --git a/src/main/java/nl/andrewlalis/model/Student.java b/src/main/java/nl/andrewlalis/model/Student.java index dbf9360..0ded9a1 100644 --- a/src/main/java/nl/andrewlalis/model/Student.java +++ b/src/main/java/nl/andrewlalis/model/Student.java @@ -1,5 +1,7 @@ package nl.andrewlalis.model; +import nl.andrewlalis.util.Pair; + import javax.persistence.*; import java.util.ArrayList; import java.util.List; @@ -20,7 +22,7 @@ public class Student extends Person { /** * A list of partners that the student has said that they would like to be partners with. */ - @ManyToMany + @ManyToMany(fetch = FetchType.EAGER) @JoinTable( name = "student_preferred_partners", joinColumns = { @JoinColumn(name = "student_id")}, @@ -95,4 +97,20 @@ public class Student extends Person { public StudentTeam getAssignedTeam() { return this.team; } + + @Override + public List> getDetailPairs() { + List> pairs = super.getDetailPairs(); + String teamNumber = "None"; + if (this.getAssignedTeam() != null) { + teamNumber = String.valueOf(this.getAssignedTeam().getNumber()); + } + pairs.add(new Pair<>("Team Number", teamNumber)); + + for (int i = 0; i < this.preferredPartners.size(); i++) { + pairs.add(new Pair<>("Preferred partner " + (i + 1), this.preferredPartners.get(i).getDetailName())); + } + + return pairs; + } } diff --git a/src/main/java/nl/andrewlalis/model/StudentTeam.java b/src/main/java/nl/andrewlalis/model/StudentTeam.java index 6d987e1..e862411 100644 --- a/src/main/java/nl/andrewlalis/model/StudentTeam.java +++ b/src/main/java/nl/andrewlalis/model/StudentTeam.java @@ -54,7 +54,7 @@ public class StudentTeam extends Team { // If the student doesn't have an preferred partners, then assume that this is valid. if (!studentA.getPreferredPartners().isEmpty()) { for (Student studentB : this.getStudents()) { - if (!studentA.equals(studentB) && !studentA.getPreferredPartners().contains(studentB.getNumber())) { + if (!studentA.equals(studentB) && !studentA.getPreferredPartners().contains(studentB)) { return false; } } diff --git a/src/main/java/nl/andrewlalis/ui/view/table_models/StudentTableModel.java b/src/main/java/nl/andrewlalis/ui/view/table_models/StudentTableModel.java index 5361704..6234223 100644 --- a/src/main/java/nl/andrewlalis/ui/view/table_models/StudentTableModel.java +++ b/src/main/java/nl/andrewlalis/ui/view/table_models/StudentTableModel.java @@ -43,7 +43,10 @@ public class StudentTableModel extends AbstractTableModel { * @return The student object at the specified row, or null if none is found. */ public Student getStudentAt(int row) { - return this.studentsList.get(row); + if (row >= 0 && row < this.studentsList.size()) { + return this.studentsList.get(row); + } + return null; } @Override diff --git a/src/main/java/nl/andrewlalis/util/Logging.java b/src/main/java/nl/andrewlalis/util/Logging.java index f0014ca..3145278 100644 --- a/src/main/java/nl/andrewlalis/util/Logging.java +++ b/src/main/java/nl/andrewlalis/util/Logging.java @@ -1,7 +1,10 @@ package nl.andrewlalis.util; import java.io.IOException; -import java.util.logging.*; +import java.util.logging.FileHandler; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; /** * Responsible for creating logs to standard output and writing to files. @@ -25,7 +28,7 @@ public class Logging { e.printStackTrace(); } - logger.setLevel(Level.ALL); + logger.setLevel(Level.FINEST); Logger.getLogger("").setLevel(Level.OFF); } diff --git a/src/main/java/nl/andrewlalis/util/TeamGenerator.java b/src/main/java/nl/andrewlalis/util/TeamGenerator.java index 15f6fe9..5487960 100644 --- a/src/main/java/nl/andrewlalis/util/TeamGenerator.java +++ b/src/main/java/nl/andrewlalis/util/TeamGenerator.java @@ -91,12 +91,14 @@ public class TeamGenerator { for (StudentTeam team : studentTeams) { if (newTeam.hasSameMembers(team)) { matchFound = true; + logger.finest("A team was found with the same members: " + team.getId()); break; } } if (!matchFound) { // Once we know this team is completely valid, we remove all the students in it from the list of singles. newTeam.setNumber(teamCount++); + studentTeams.add(newTeam); singleStudents.removeAll(Arrays.asList(newTeam.getStudents())); assignStudentsToTeam(newTeam); logger.fine("Created team:\n" + newTeam);