Use java to realize a regular grouping

The code implementation interface is as follows

First create a Group class to realize the functions of adding, selecting, grouping and selecting the speaker for students

code show as below

package fz;

import java.util.ArrayList;
import java.util.Random;

public class Group {

// record students
private ArrayList<Student> student = new ArrayList<>();

// record group
private ArrayList<ArrayList<Student>> group = new ArrayList<>();

// record speaker
private ArrayList<Student> speaker = new ArrayList<>();

// group switch
private boolean fenGroup = false;

// Get group switch
public boolean getFenGroup() {
return fenGroup;
}

// set group switch
public void setFenGroup(boolean b) {
this.fenGroup = b;
}

// Get the number of students
public int getStudentNum() {
if (student. isEmpty()) {
return 0;
}
return student. size();
}

// Get the number of people in the group
public int getGroupNum() {
if (group. isEmpty()) {
return 0;
}
return group. size();
}

// Get the names of the students in the j group of i
public String getGroupStudentName(int i, int j) {
if (group. isEmpty()) {
return "";
}
return group.get(i).get(j).getName();
}

// Get the student id of group j in group i
public int getGroupStudentId(int i, int j) {
if (group. isEmpty()) {
return 0;
}
return group.get(i).get(j).getId();
}

// Get the number of members in each group
public int getGroupStudentNum(int num) {
if (group. isEmpty()) {
return 0;
}
return group.get(num).size();
}

// Randomly add students
public void addStudent(int num) {
int count = 0;// count student id
int addStudent = 0;// Calculate the added student
// count from 1001
if (student != null) {
count = 1001 + student. size();
addStudent = student. size();
} else {
count = 1001;
}

String x = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String name = "Zhang San";
for (int i = addStudent; i < (addStudent + num); i ++ ) {
Student st = new Student(name + x. charAt(i), count);
student. add(st);
count + + ;
}
}

// Group, num as a group
public void selectGroup(int num) {
if (student. isEmpty() || num < 1) {
System.out.println("The number of students or groups is 0, cannot be grouped");
return;
}

ArrayList<Student> stu = new ArrayList<>();
Random r = new Random();
int addStudent = 0;// need to store students
int t = 0;// random number

if (!group. isEmpty()) {
int temp = (group. size() - 1) * num + group. get(group. size() - 1). size();
addStudent = student. size() - temp;
this.copyFenGroupStudent(stu, temp, student.size());// Students stored in deep copy
int addStu = num;
if (stu. size() > num - group. get(group. size() - 1). size()) {
addStu = num - group.get(group.size() - 1).size();
}
ArrayList<Student> st = new ArrayList<Student>(group. get(group. size() - 1));
group. remove(group. get(group. size() - 1));
for (int i = 0; i < addStu; i ++ ) {
t = r.nextInt(addStudent);
Student s = new Student(stu. get(t). getName(), stu. get(t). getId());
st. add(s);
addStudent--;
this. exchangeStudent(stu, t, addStudent);

}
group. add(st);

} else {
addStudent = student. size();
this.copyFenGroupStudent(stu, 0, addStudent);// Students stored in deep copy
}

int Group1 = addStudent / num;// the number of groups with full members
int Group2 = addStudent % num;// Students whose number is less than num are a new group

for (int i = 0; i < Group1; i ++ ) {
ArrayList<Student> st = new ArrayList<>();
for (int j = 0; j < num; j ++ ) {
t = r.nextInt(addStudent);
Student s = new Student(stu. get(t). getName(), stu. get(t). getId());
st. add(s);
addStudent--;
this. exchangeStudent(stu, t, addStudent);
}
group. add(st);

}
if (Group2 > 0) {
ArrayList<Student> st = new ArrayList<>();
for (int i = 0; i < Group2; i ++ ) {
Student s = new Student(stu. get(i). getName(), stu. get(i). getId());
st. add(s);
}
group. add(st);
}

}

// group member exchange
private void exchangeStudent(ArrayList<Student> stu, int qian, int hou) {
Student temp = new Student(stu.get(hou).getName(), stu.get(hou).getId());
stu.get(hou).addStudentDate(stu.get(qian).getName(), stu.get(qian).getId());
stu.get(qian).addStudentDate(temp.getName(), temp.getId());
}

// deep copy student data
private void copyFenGroupStudent(ArrayList<Student> stu, int existStudent, int allStudent) {
for (int i = 0; i < allStudent - existStudent; i ++ ) {
Student s = new Student(student. get(existStudent + i). getName(), student. get(existStudent + i). getId());
stu. add(s);
}
}

// extract speaker from group
public void selectStudent() {
if (group. isEmpty()) {
System.out.println("The group is empty, the speaker cannot be selected");
return;
}

int t = 0;// record the random number drawn
Random r = new Random();// random number function
int speakerNum = 0;//Number of speakers
if (!speaker. isEmpty()) {
speakerNum = speaker. size();
// If the last group is not full, draw again
if (group.get(0).size() != group.get(speakerNum - 1).size()) {
speakerNum--;
speaker. remove(speakerNum - 1);
}
}

for (int i = speakerNum; i <this. group. size(); i ++ ) {
int size = this.group.get(i).size();
t = r.nextInt(size);
Student stu = new Student(group.get(i).get(t).getName(), group.get(i).get(t).getId());
speaker.add(stu);
}

}

// show speaker
public String showGroupSpeaker(int num) {
if (speaker. isEmpty()) {
return "Speaker is empty\
";
}
if (num > -1 & amp; & amp; num < speaker. size()) {
return ("th" + (num + 1) + "group" + "speaker:" + speaker.get(num).getName() + "id:" + speaker.get(num).getId()
+ "\
");
} else if (num >= speaker. size()) {
return ("Number" + (num + 2) + "group not selected speakers\
");
} else {
return ("Failed to view speakers, input data error!\
");
}

}

// Check group member names
public String lookGroupName(int num) {
if (group. isEmpty()) {
return ("The group is empty and cannot be viewed");
}

String getName = ("th" + (num + 1) + "group\
" + "group member name is:");
for (int i = 0; i <this.group.get(num).size(); i++ ) {
getName + = ("\t" + this.group.get(num).get(i).getName());
}
getName += "\
";
return getName;
}

// View group member id
public String lookGroupId(int num) {
if (group. isEmpty()) {
return ("The group is empty and cannot be viewed");
}

String getId = "The group member id is:";
for (int i = 0; i <this.group.get(num).size(); i++ ) {
getId + = ("\t" + this.group.get(num).get(i).getId());
}
getId + = "\
";
return getId;
}

// clear all
public void clearAll(int num) {
if (num == 0) {
student. clear();
group. clear();
speaker. clear();
System.out.println("Empty successfully");
}
}
// end class
}

class Student {
private String studentName;// student name

private int studentId;// student id

public Student() {
}// No parameter constructor

// parameterized constructor
public Student(String name, int id) {
this.studentName = name;
this.studentId = id;
}

// add student information
public void addStudentDate(String name, int id) {
this.studentName = name;
this.studentId = id;
}

public String getName() {
return this. studentName;
}

public int getId() {
return this.studentId;
}
}

Then create a panel that selects students, selects the speaker, and implements the functions of displaying groups and clearing groups

That is to create a GroupJFrame class

code show as below

package fz;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;

public class GroupJFrame extends JFrame implements ActionListener {

/**
*
*/
private static final long serialVersionUID = -3394774975694421546L;

Group g = new Group();

// create button
JButton jbt1 = new JButton("Add students");
JButton jbt2 = new JButton("Group");
JButton jbt3 = new JButton("clear");
JButton jbt4 = new JButton("Extract speaker");
JButton jbt5 = new JButton("Display grouping");

// // create text
JTextField jf1 = null;
// JTextField jf2 = null;

\t// Drop-down menu
JComboBox<String> co1 = new JComboBox<>();
JComboBox<String> co2 = new JComboBox<>();

// message dialog
JDialog jd1 = new JDialog();
JDialog jd2 = new JDialog();
JDialog jd3 = new JDialog();

\t// Label
Font font = new Font("Arial", Font.PLAIN, 20);
JLabel jl1 = new JLabel("Add the number of students:");
JLabel jl2 = new JLabel("Number of people in each group:");
JLabel jl3 = new JLabel("Total number of students:");
JLabel jl4 = new JLabel("0");
JLabel jl5 = new JLabel();

// text area
JTextArea jt1 = new JTextArea();
JTextArea jt2 = new JTextArea();

\t// menu
JMenu menu1 = new JMenu("Options");
JMenuItem mi1 = new JMenuItem("clear");
JMenuItem mi2 = new JMenuItem("Close");

JMenu menu2 = new JMenu("About Us");
JMenuItem mi10 = new JMenuItem("QQ");
JMenuItem mi11 = new JMenuItem("WeChat");

public GroupJFrame() {
// title
super("group");
// interface
this. setSize(850, 600);
this. setResizable(false);
// top
this.setAlwaysOnTop(true);
// center
this.setLocationRelativeTo(null);

// cancel centering
this. setLayout(null);

// add text field
JPanel jp = new JPanel();
this.add(jp);

jl1.setBounds(20, 20, 150, 32);
jl1. setFont(font);
this.add(jl1);

jl2. setBounds(320, 20, 100, 32);
jl2.setFont(font);
this.add(jl2);

jl3. setBounds(550, 20, 120, 32);
jl3.setFont(font);
this.add(jl3);

jl4.setBounds(670, 20, 40, 32);
jl4.setFont(font);
this.add(jl4);

\t\t// picture
JLabel jl10 = new JLabel(new ImageIcon("image\qq.jpg"));
JLabel jl11 = new JLabel(new ImageIcon("image\weixin.jpg"));

jl10.setBounds(0, 0, 400, 600);
// add the image to the dialog
jd1.getContentPane().add(jl10);
// Set the popup frame size
jd1. setSize(600, 600);
jd1.setAlwaysOnTop(true);
jd1.setLocationRelativeTo(null);
// If the popup is not closed, it cannot be closed
jd1. setModal(true);

jl11.setBounds(0, 0, 400, 600);
// add the image to the dialog
jd2.getContentPane().add(jl11);
// Set the popup frame size
jd2. setSize(600, 600);
jd2.setAlwaysOnTop(true);
jd2.setLocationRelativeTo(null);
// If the popup is not closed, it cannot be closed
jd2. setModal(true);

jf1 = new JTextField();
jf1.setBounds(420, 20, 80, 32);

// jf2 = new JTextField();
// jf2.setFont(font);
// jf2.setBounds(20, 150, 500, 300);
// jf2.setEditable(false);
// this.add(jf2);

// jl5.setBounds(20,150,500,400);
// jl5.setFont(font);
// this.add(jl5);

// text area
// jt1.setBounds(20,150,530,360);
jt1.setFont(font);
// this.add(jt1);

\t\t// scroll bar
JScrollPane scrollPane = new JScrollPane(jt1);
// always show scrollbar
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(20, 150, 530, 360);
this.add(scrollPane);

// drop-down menu settings
co1. addItem("1");
co1. addItem("2");
co1. addItem("3");
co1. addItem("4");
co1. addItem("5");
co1. addItem("6");
co1. addItem("7");
co1. addItem("8");
co1. addItem("9");
co1. addItem("10");
co1. addItem("11");
co1. addItem("12");
co1.setBounds(170, 20, 80, 32);
co1. setFont(font);
this.add(co1);

co2. addItem("1");
co2. addItem("2");
co2. addItem("3");
co2. addItem("4");
co2. addItem("5");
co2.setEditable(false);

co2.setBounds(420, 20, 80, 32);
co2.setFont(font);
this. add(co2);

// set button coordinates
jbt1.setBounds(50, 100, 120, 40);
jbt2.setBounds(230, 100, 120, 40);
jbt3.setBounds(570, 100, 120, 40);
jbt4.setBounds(400, 100, 120, 40);
jbt5.setBounds(570, 180, 120, 40);

jbt1. addActionListener(this);
jbt2. addActionListener(this);
jbt3. addActionListener(this);
jbt4. addActionListener(this);
jbt5. addActionListener(this);

this.getContentPane().add(jbt1);
this.getContentPane().add(jbt2);
this.getContentPane().add(jbt3);
this.getContentPane().add(jbt4);
this.getContentPane().add(jbt5);

// Initialize the menu
JMenuBar menubar = new JMenuBar();
menubar. setFont(font);
this.setJMenuBar(menubar);

menubar.add(menu1);
menubar.add(menu2);

ButtonGroup bg1 = new ButtonGroup();
bg1.add(mi1);
bg1.add(mi2);
menu1. add(mi1);
menu1.add(mi2);

bg1.add(mi10);
bg1.add(mi11);
menu2.add(mi10);
menu2.add(mi11);

mi1. addActionListener(this);
mi2. addActionListener(this);
mi10.addActionListener(this);
mi11.addActionListener(this);

\t\t// show
this. setVisible(true);
// set shutdown mode
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO auto-generated method stub
Object source = e. getSource();

if (source == jbt1) {
String s = (String) co1. getSelectedItem();
int num = Integer. parseInt(s);
g. addStudent(num);
g. setFenGroup(true);
jl4.setText("" + g.getStudentNum());
JOptionPane.showMessageDialog(this, "Added successfully" + s + "students");

} else if (source == jbt2) {
if (g. getFenGroup()) {
String s = (String) co2. getSelectedItem();
int num = Integer. parseInt(s);
g. selectGroup(num);
g. setFenGroup(false);
this.add(jf1);
co2. removeAll();
co2. addItem(s);
co2.setEditable(false);
JOptionPane.showMessageDialog(this, "Successful Grouping");
}

} else if (source == jbt3) {
g. clearAll(0);
g. setFenGroup(false);
jl4.setText("" + g.getStudentNum());
jt1.setText("");
} else if (source == jbt4) {
g. selectStudent();
JOptionPane.showMessageDialog(this, "Successful Extraction" + g.getGroupNum() + "Speaker");
} else if (source == jbt5) {
String text = "";
jt1.setText(text);
for (int i = 0; i < g. getGroupNum(); i ++ ) {
text = g.lookGroupName(i);// name
jt1.append(text);
text = g.lookGroupId(i);// id number
jt1.append(text);

// get speaker
text = g. showGroupSpeaker(i);
jt1.append(text);
}
} else if (source == mi1) {
g. clearAll(0);
g. setFenGroup(false);
jl4.setText("" + g.getStudentNum());
jt1.setText("");
} else if (source == mi2) {
System. exit(0);
} else if (source == mi10) {
// show popup
jd1. setVisible(true);
} else if (source == mi11) {
// show popup
jd2. setVisible(true);
}

}

}

Finally call the main function to implement the above method

package fz;

import java.util.Random;

public class GroupMain {
\t
static void test()
{
for(int i=0;i<100;i ++ )
{
Random r=new Random();
int t = r.nextInt(1);
System.out.println(t);
}
}

public static void main(String[] args) {
// TODO auto-generated method stub
GroupJFrame j = new GroupJFrame();
//test();
}
}

The above implements a simple selection, using the panel operation, and can also be used to access the database, adding more data can achieve more comprehensive functions.

If there are mistakes, please correct me!