【java】随机分组

阅读 23

2022-04-19

一、界面

插入的名单的数据格式(name+score)

1.初始界面

2.文件导入或粘贴名单后

3.开始分组

 

二、代码

package week8;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.function.Consumer;

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class RandomGroup extends Application {

	public static class Candidate {
		private final SimpleIntegerProperty number;
		private final SimpleStringProperty name;
		private final SimpleIntegerProperty score;
		private final SimpleIntegerProperty groupNo;

		private Candidate(int number, String name, int score, int groupNo) {
			this.number = new SimpleIntegerProperty(number);
			this.name = new SimpleStringProperty(name);
			this.score = new SimpleIntegerProperty(score);
			this.groupNo = new SimpleIntegerProperty(groupNo);
		}

		public int getNumber() {
			return this.number.get();
		}

		public void setNumber(int number) {
			this.number.set(number);
			;
		}

		public String getName() {
			return this.name.get();
		}

		public void setName(String name) {
			this.name.set(name);
		}

		public int getScore() {
			return this.score.get();
		}

		public void setScore(int score) {
			this.score.set(score);
			;
		}

		public int getGroupNo() {
			return this.groupNo.get();
		}

		public void setGroupNo(int groupNo) {
			this.groupNo.set(groupNo);

		}

		@Override
		public String toString() {
			return "Candidate [number =" + getNumber() + ",name =" + getName() + ",score = " + getScore()
					+ ",groupNo = " + getGroupNo() + "]";
		}
	}

	private int canNum = 0;
	private final int canMax = 100;
	Candidate[] oldCans = new Candidate[canMax];
	Candidate[] newCans = new Candidate[canMax];
	ObservableList<Candidate> oldList = FXCollections.observableArrayList();
	ObservableList<Candidate> newList = FXCollections.observableArrayList();

	// 控件
	TableView<Candidate> table = new TableView<>();
	Button btLoad = new Button("导入名单");
	Button btPaste = new Button("粘贴名单");

	CheckBox cbHasSeed = new CheckBox("种子分组");
	Button btDeleteSeed = new Button("清除种子");

	Button btGroupAdd = new Button("+");
	Button btGroupMin = new Button("-");
	Label lbGroupNum = new Label("5");

	Button btClear = new Button("清除分组");
	Button btGroup = new Button("开始分组");
	Button btCopy = new Button("复制分组");

	// 表格栏
	TableColumn idCol = new TableColumn("number");
	TableColumn nameCol = new TableColumn("name");
	TableColumn scoreCol = new TableColumn("score");
	TableColumn groupCol = new TableColumn("groupNo");

	// 粘贴名单
	TextArea ta = new TextArea();
	Button btYes = new Button("确定");
	Button btNo = new Button("取消");

	Label lbWarn = new Label();

	public static void main(String[] args) {
		Application.launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		table.setEditable(true);
		idCol.setVisible(false);
		nameCol.setVisible(false);
		scoreCol.setVisible(false);
		groupCol.setVisible(false);
		idCol.setMinWidth(50);
		nameCol.setMinWidth(140);
		scoreCol.setMinWidth(140);
		groupCol.setMinWidth(140);
		idCol.setCellValueFactory(new PropertyValueFactory<>("number"));
		nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
		scoreCol.setCellValueFactory(new PropertyValueFactory<>("score"));
		groupCol.setCellValueFactory(new PropertyValueFactory<>("groupNo"));
		table.getColumns().addAll(idCol, nameCol, scoreCol, groupCol);

		Font font1 = new Font("Arial", 12);
		btLoad.setFont(font1);
		btPaste.setFont(font1);
		cbHasSeed.setFont(font1);
		btDeleteSeed.setFont(font1);
		btGroupAdd.setFont(font1);
		btGroupMin.setFont(font1);
		lbGroupNum.setFont(font1);
		btClear.setFont(font1);
		btGroup.setFont(font1);
		btCopy.setFont(font1);

		Color color1 = new Color(0.8, 0.2, 0.6, 1);
		btLoad.setTextFill(color1);
		btPaste.setTextFill(color1);
		btDeleteSeed.setTextFill(color1);
		btGroupAdd.setTextFill(color1);
		btGroupMin.setTextFill(color1);
		btClear.setTextFill(color1);
		btGroup.setTextFill(color1);
		btCopy.setTextFill(color1);
		cbHasSeed.setTextFill(color1);
		lbGroupNum.setTextFill(color1);
		btLoad.setStyle("-fx-border-color:#abbfff");
		btPaste.setStyle("-fx-border-color:#abbfff");
		btDeleteSeed.setStyle("-fx-border-color:#abbfff");
		btGroupAdd.setStyle("-fx-border-color:#abbfff");
		btGroupMin.setStyle("-fx-border-color:#abbfff");
		btClear.setStyle("-fx-border-color:#abbfff");
		btGroup.setStyle("-fx-border-color:#abbfff");
		btCopy.setStyle("-fx-border-color:#abbfff");
		table.setStyle("-fx-border-color:#abbfff");
		btLoad.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));
		btPaste.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));
		btDeleteSeed.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));
		btGroupAdd.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));
		btGroupMin.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));
		btClear.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));
		btGroup.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));
		btCopy.setBackground(new Background(new BackgroundFill(Color.GHOSTWHITE, null, null)));
		table.setBackground(new Background(new BackgroundFill(Color.HONEYDEW, null, null)));

		HBox hb = new HBox(5, btGroupMin, lbGroupNum, btGroupAdd);
		hb.setAlignment(Pos.CENTER);

		VBox vb = new VBox(10, new Label("名单设置"), btLoad, btPaste, new Label("种子设置"), cbHasSeed, btDeleteSeed,
				new Label("分组数量"), hb, new Label(" "), btClear, btGroup, btCopy);
		vb.setPrefWidth(80);
		vb.setAlignment(Pos.TOP_CENTER);

		BorderPane pane = new BorderPane();
		pane.setCenter(table);
		pane.setRight(vb);
		pane.setPadding(new Insets(10));
		pane.setBackground(new Background(new BackgroundFill(Color.AZURE, null, null)));

		primaryStage.setTitle("随机分组");
		primaryStage.setScene(new Scene(pane, 600, 400));
		primaryStage.show();

		// 粘贴名单
		ta.setPromptText("请将名单粘贴在文本区内!");
		ta.setPrefColumnCount(10);
		ta.setPrefRowCount(8);
		ta.setWrapText(true);
		btYes.setPrefSize(50, 30);
		btNo.setPrefSize(50, 30);

		HBox choose = new HBox(20, btYes, btNo);
		VBox vbox1 = new VBox(10, ta, choose);
		choose.setAlignment(Pos.CENTER);
		vbox1.setAlignment(Pos.CENTER);
		vbox1.setPadding(new Insets(10));
		vbox1.setBackground(new Background(new BackgroundFill(Color.ALICEBLUE, null, null)));

		Stage stage1 = new Stage();
		stage1.setScene(new Scene(vbox1, 300, 200));
		stage1.setTitle("粘贴名单");

		// 未粘贴提示
		HBox hbox1 = new HBox(lbWarn);
		hbox1.setPadding(new Insets(10));
		hbox1.setAlignment(Pos.CENTER_LEFT);
		hbox1.setBackground(new Background(new BackgroundFill(Color.ALICEBLUE, null, null)));

		Font font2 = new Font("", 14);
		lbWarn.setFont(font2);

		Stage stage2 = new Stage();
		stage2.setScene(new Scene(hbox1, 400, 50));

		// 导入名单
		btLoad.setOnAction(e -> {
			table.getItems().remove(0, canNum);
			FileChooser fc = new FileChooser();
			File f = fc.showOpenDialog(primaryStage);
			fileDeal(f);
		});

		// 粘贴名单
		btPaste.setOnAction(e -> {
			ta.setText(null);
			stage1.show();
		});
		btYes.setOnAction(e -> {
			if (ta.getText() != null) {
				table.getItems().remove(0, canNum);
				File a = new File("临时文件.txt");
				try {
					FileWriter fw = new FileWriter(a);
					fw.write(ta.getText());
					fw.flush();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				fileDeal(a);
				a.delete();
				stage1.close();
			} else {
				lbWarn.setText("文本区错误,请重新粘贴名单!");
				stage2.setTitle("错误提示");
				stage2.show();
			}
		});
		btNo.setOnAction(e -> {
			stage1.close();
		});

		// 清除种子
		btDeleteSeed.setOnAction(e -> {
			int groupNum = Integer.valueOf(lbGroupNum.getText());
			if (canNum / groupNum >= 2) {
				for (int i = 0; i < canNum - 1; i++) {
					for (int j = 0; j < canNum - i - 1; j++) {
						if (oldCans[j].getScore() < oldCans[j + 1].getScore()) {
							Candidate temp = oldCans[j];
							oldCans[j] = oldCans[j + 1];
							oldCans[j + 1] = temp;
						}
					}
				}
				for (int i = 0; i < canNum; i++)
					oldList.add(oldCans[i]);
				table.getItems().remove(0, canNum);
				table.setItems(oldList);
				table.getItems().remove(0, groupNum);
				canNum = table.getItems().size();
			} else {
				lbWarn.setText("单组人数存在仅有一人的情况,不可清除种子!");
				stage2.setTitle("错误提示");
				stage2.show();
			}
		});

		// 分组数量+
		btGroupAdd.setOnAction(e -> {
			int i = Integer.valueOf(lbGroupNum.getText());
			if (i < canNum) {
				lbGroupNum.setText(String.valueOf(i + 1));
			}
		});
		// 分组数量-
		btGroupMin.setOnAction(e -> {
			int i = Integer.valueOf(lbGroupNum.getText());
			if (i > 2) {
				lbGroupNum.setText(String.valueOf(i - 1));
			}
		});

		// 清除分组
		btClear.setOnAction(e -> {
			table.getItems().remove(0, canNum);
			table.setItems(null);
			for (int i = 0; i < canNum; i++) {
				oldList.add(oldCans[i]);
				newCans[i] = oldCans[i];
			}
			table.setItems(oldList);
		});

		// 开始分组
		btGroup.setOnAction(e -> {
			table.getItems().remove(0, canNum);
			int groupNum = Integer.valueOf(lbGroupNum.getText());
			ArrayList<Integer> list = new ArrayList<>();

			if (cbHasSeed.isSelected()) {
				for (int i = 0; i < groupNum; i++) {
					list.add(i);
				}

				for (int i = 0; i < canNum - 1; i++) {
					for (int j = 0; j < canNum - i - 1; j++) {
						if (newCans[j].getScore() < newCans[j + 1].getScore()) {
							Candidate temp = newCans[j];
							newCans[j] = newCans[j + 1];
							newCans[j + 1] = temp;
						}
					}
				}

				int index = 0;
				while (index < canNum) {
					Collections.shuffle(list);
					for (int i = 0; i < groupNum && index < canNum; i++) {
						newCans[index].setGroupNo(list.get(i) % groupNum + 1);
						index++;
					}
				}
			} else {
				for (int i = 0; i < canNum; i++) {
					list.add(i);
				}
				Collections.shuffle(list);

				for (int j = 0; j < canNum; j++) {
					newCans[j].setGroupNo(list.get(j) % groupNum + 1);
				}
			}

			for (int i = 0; i < canNum - 1; i++) {
				for (int j = 0; j < canNum - i - 1; j++) {
					if (newCans[j].getGroupNo() > newCans[j + 1].getGroupNo()) {
						Candidate temp = newCans[j];
						newCans[j] = newCans[j + 1];
						newCans[j + 1] = temp;
					}
				}
			}
			for (int i = 0; i < canNum; i++)
				newList.add(newCans[i]);
			table.setItems(newList);
		});

		// 复制分组
		btCopy.setOnAction(e -> {
			if (newCans[0].getGroupNo() != 0) {
				for (int i = 0; i < canNum - 1; i++) {
					for (int j = 0; j < canNum - i - 1; j++) {
						if (newCans[j].getGroupNo() > newCans[j + 1].getGroupNo()) {
							Candidate temp = newCans[j];
							newCans[j] = newCans[j + 1];
							newCans[j + 1] = temp;
						}
					}
				}
				String s = "";
				for (int i = 0; i < canNum; i++) {
					s = s + newCans[i].getGroupNo() + ". " + newCans[i].getName() + "\n";
				}

				StringSelection stsel = new StringSelection(s);
				Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);
				lbWarn.setText("已将分组结果复制到剪切板!");
				stage2.setTitle("成功提示");
			}else {
				lbWarn.setText("您还未分组!");
				stage2.setTitle("错误提示");
			}
			stage2.show();
		});
	}

	private void fileDeal(File x) {
		try (Scanner b = new Scanner(x)) {
			String text = "";
			while (b.hasNextLine())
				text += b.nextLine() + "\n";
			textAnalyze(text);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		table.setItems(oldList);
	}

	private void textAnalyze(String text) {
		canNum = 0;
		Consumer con = s -> {
			String list[] = ((String) s).split(",");
			oldCans[canNum] = new Candidate(canNum + 1, list[0], Integer.parseInt(list[1]), 0);
			newCans[canNum] = new Candidate(canNum + 1, list[0], Integer.parseInt(list[1]), 0);
			oldList.add(oldCans[oldList.size()]);
			idCol.setVisible(true);
			nameCol.setVisible(true);
			groupCol.setVisible(true);
			scoreCol.setVisible(true);
			canNum++;
		};

		for (String s : text.split("\n"))
			con.accept(s);
		oldList.forEach(System.out::println);
	}
}

精彩评论(0)

0 0 举报