Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/main/java/org/codedifferently/cbtyson/data/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
public class Group {
String name;
String groupID;
int size;
ArrayList<Student> studentList;


public Group(String name, String groupID, int size, ArrayList<Student> studentList) {
public Group(String name, String groupID, ArrayList<Student> studentList) {
this.name = name;
this.groupID = groupID;
this.size = size;
this.studentList = studentList;
}

Expand All @@ -27,8 +25,4 @@ public ArrayList<Student> getStudentList() {
public String getGroupID() {
return groupID;
}

public int getSize() {
return size;
}
}
61 changes: 57 additions & 4 deletions src/main/java/org/codedifferently/cbtyson/data/GroupList.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void AddGroup(Group group) {
groups.add(group);
}

public static void AddStudentToGroup(String groupID,Student student) {
public static boolean AddStudentToGroup(String groupID,Student student) {

boolean groupFound = false;

Expand All @@ -30,21 +30,74 @@ public static void AddStudentToGroup(String groupID,Student student) {
System.out.println("Group not found!");
}

return groupFound;

}

public static void RemoveStudentFromGroup(String groupID, Student student) {
public static boolean RemoveStudentFromGroup(String groupID, String studentID) {

boolean groupFound = false;
boolean studentFound = false;

for (Group group : groups) {
if(group.getGroupID().equals(groupID)) {
groupFound = true;
group.getStudentList().remove(student);
for(Student student : group.getStudentList()) {
if(student.getStudentID().equals(studentID)) {
studentFound = true;
group.getStudentList().remove(student);
return true;
}
}

}
}

if(!studentFound && groupFound) {
System.out.println("Student ID not found!");
}

if(!groupFound) {
System.out.println("Group not found!");
System.out.println("Group ID not found!");
}
return false;
}

public static Student GetStudentFromGroup(String groupID, String studentID) {
boolean groupFound = false;
boolean studentFound = false;

for (Group group : groups) {
if(group.getGroupID().equals(groupID)) {
groupFound = true;
for(Student student : group.getStudentList()) {
if(student.getStudentID().equals(studentID)) {
studentFound = true;
return student;
}
}

}
}

if(!studentFound && groupFound) {
System.out.println("Student ID not found!");
}

if(!groupFound) {
System.out.println("Group ID not found!");
}
return null;
}

public static String GetGroupIDFromStudentID(String studentID) {
for(Group group : groups) {
for(Student student : group.getStudentList()) {
if(student.getStudentID().equals(studentID)) {
return group.getGroupID();
}
}
}
return null;
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/codedifferently/cbtyson/data/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class Student {
public String email;
public String studentID;
public int age;
public String gpa;
public double gpa;


public Student(String firstName, String lastName, String email, String studentID, int age, String gpa) {
public Student(String firstName, String lastName, String email, String studentID, int age, double gpa) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
Expand All @@ -35,7 +35,7 @@ public String getEmail() {
return email;
}

public String getGpa() {
public double getGpa() {
return gpa;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,85 @@
package org.codedifferently.cbtyson.helpers;

import org.codedifferently.cbtyson.data.Group;
import org.codedifferently.cbtyson.data.GroupList;
import org.codedifferently.cbtyson.data.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class GroupRandomizer {
public static void randomizeStudentsIntoGroups(ArrayList<Student> allStudents, int groupSize) {
List<Group> groups = GroupList.GetGroups();

if (groups.isEmpty()) {
System.out.println("No groups exist. Please add groups first.");
return;
}
if (allStudents == null || allStudents.isEmpty()) {
System.out.println("No students to assign.");
return;
}

// Clear existing students from all groups
for (Group group : groups) {
group.getStudentList().clear();
}

// Shuffle a copy of the student list
ArrayList<Student> shuffled = new ArrayList<>(allStudents);
Collections.shuffle(shuffled);

// Determine how many groups we need based on desired group size
int numGroupsNeeded = (int) Math.ceil((double) shuffled.size() / groupSize);

if (numGroupsNeeded > groups.size()) {
System.out.println("Warning: Not enough groups for group size of " + groupSize +
". Need " + numGroupsNeeded + " but only have " + groups.size() +
". Some groups will be larger than requested.");
}

// Assign students round-robin across available groups
for (int i = 0; i < shuffled.size(); i++) {
int groupIndex = i % groups.size();
groups.get(groupIndex).getStudentList().add(shuffled.get(i));
}

System.out.println("Students assigned! Group breakdown:");
for (Group g : groups) {
System.out.println(" " + g.getName() + ": " + g.getStudentList().size() + " students");
}
}

public static void randomizeStudentsIntoGroupsInteractive(ArrayList<Student> allStudents) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter desired group size: ");

while (!scanner.hasNextInt()) {
System.out.print("Invalid input. Enter a number: ");
scanner.next();
}

int groupSize = scanner.nextInt();

if (groupSize <= 0) {
System.out.println("Group size must be at least 1.");
return;
}

randomizeStudentsIntoGroups(allStudents, groupSize);
}

public static void randomizeGroups() {
ArrayList<Student> studentList = new ArrayList<>();

for (Group group : GroupList.GetGroups()) {
for(Student student : group.getStudentList()) {
studentList.add(student);
}
}
randomizeStudentsIntoGroupsInteractive(studentList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@ public static String handleStringInput() {
return scanInput;
}

public static String handleDoubleInput() {
public static Double handleDoubleInput() {
Scanner scan = new Scanner(System.in);
String scanInput = "";
double scanInput = 0;
boolean validScanInput = false;
//While loop to make sure user puts in the correct input
while(!validScanInput) {
//Call Scanner methods
try {
//Scanner method to collect input
scanInput = scan.nextLine();
scanInput = scan.nextDouble();
validScanInput = true;
}
catch (Exception e) {
//If user enters invalid input, the catch block will prevent errors.
System.out.println("Invalid input! Try typing a valid String!");
System.out.println("Invalid input! Try typing a valid Double!");
scan.next();
}
}
return String.format(".%2f", scanInput);
return scanInput;
}

public static String handleYesNoInput() {
Expand Down
44 changes: 41 additions & 3 deletions src/main/java/org/codedifferently/cbtyson/menus/AddGroupMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.codedifferently.cbtyson.helpers.InputHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class AddGroupMenu {
Expand All @@ -18,14 +19,14 @@ public void promptGroupMenu() {
System.out.println("Enter your Group Name:");
String name = InputHandler.handleStringInput();

System.out.println("How big is your group? ");
int groupSize = InputHandler.handleIntegerInput();
// System.out.println("How big is your group? ");
// int groupSize = InputHandler.handleIntegerInput();

// Generate a new UUID object
String groupID = UUID.randomUUID().toString();
groupID = groupID.substring(0,6);

Group group = new Group(name, groupID, groupSize, new ArrayList<Student>());
Group group = new Group(name, groupID, new ArrayList<Student>());

System.out.println();

Expand All @@ -39,4 +40,41 @@ public void promptGroupMenu() {
}
}
}

public void promptGroupMenu(Student student) {
boolean inGroupMenu = true;
boolean addedStudent = false;

while(inGroupMenu) {
System.out.println("Enter your new Group Name:");
String name = InputHandler.handleStringInput();

//System.out.println("How big is your group? ");
//int groupSize = InputHandler.handleIntegerInput();

// Generate a new UUID object
String groupID = UUID.randomUUID().toString();
groupID = groupID.substring(0,6);

//add student to studentList here, only diff in overloaded method
ArrayList<Student> studentList = new ArrayList<>();
if(!addedStudent) studentList.add(student);
addedStudent = true;

Group group = new Group(name, groupID, studentList);

System.out.println();

//Add new Group to List
GroupList.AddGroup(group);
System.out.println("The " + group.getName() + " has been made! ID: " + group.getGroupID());

System.out.println("Continue adding groups? (y/n)");
System.out.println("===================================");
String answer = InputHandler.handleYesNoInput();
if(answer.equals("n")) {
inGroupMenu = false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,44 @@ public void promptAddStudentMenu() {
int age = InputHandler.handleIntegerInput();

System.out.println("Enter their GPA: ");
String gpa = InputHandler.handleDoubleInput();
double gpa = InputHandler.handleDoubleInput();

// Generate a new UUID object
String studentID = UUID.randomUUID().toString();
studentID = studentID.substring(0,6);

Student student = new Student(firstName, lastName, email, studentID, age, gpa);

System.out.println();
System.out.println("Which group is this new student going to?");
System.out.println("Enter the Group ID here:");

//Actually add student to group
String groupID = InputHandler.handleStringInput();
GroupList.AddStudentToGroup(groupID, student);
boolean studentAddedToGroup = false;

while(!studentAddedToGroup) {
System.out.println();
System.out.println("Which group is this new student going to?");
System.out.println("Enter the Group ID here:");

//Actually add student to group
String groupID = InputHandler.handleStringInput();
boolean hasAddedStudent = GroupList.AddStudentToGroup(groupID, student);


if(!hasAddedStudent) {
System.out.println("What you like to create a new Group to add this student to? (y/n)");
System.out.println("=============================================================");
String answerStr = InputHandler.handleYesNoInput();
if (answerStr.equals("y")) {
AddGroupMenu addGroupMenu = new AddGroupMenu();
addGroupMenu.promptGroupMenu(student);
studentAddedToGroup = true;
}
}
else {
studentAddedToGroup = true;
}
}

System.out.println();
System.out.println("Make another one? (y/n)");
System.out.println("Continue adding students? (y/n)");
System.out.println("=========================================");

String answerStr = InputHandler.handleYesNoInput();
Expand Down
Loading