Grouping program v2.0



Title


Enter number of groups and names of students.
Press 0 to stop entering.
names: entered names
shuffled names: self-explanatory
modulus o or x: self-explanatory
quotient: number of names to push into each group

Program is restarted automatically after. 

Updates from v1.0
1. now supports smart grouping mode.
ex: dividing 19 people to 4 groups.
result: 3 groups of 5 people and 1 group of 4 people.
2. automatic program reboot.

Loader class

Main class

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;


public class Main {

private Scanner sc;

int groups;

int quotient;

ArrayList<String> students;

ArrayList<ArrayList<String>> result;


public Main() {

this.main();

}


public void main() {

this.printTitle();

this.print("Press Enter to Continue...");

try {

System.in.read();

} catch (IOException e) {

e.printStackTrace();

}

this.print("How many groups?");


this.groups = Integer.parseInt(input());


this.print("Enter the names of the students");

this.print("Enter 0 when done");


this.students = new ArrayList<String>();


while (true) {

String name = input();

if (name.equals("0")) {

break;

}

this.students.add(name);

System.out.println(students);

}

this.grouping();

}


private void grouping() {

if (groups > students.size()) {

this.print("group too big");

} else if (groups == students.size()) {

this.print("are you kidding me?");

} else {

System.out.println("names: " + students);

Collections.shuffle(students);

System.out.println("shuffled names: " + students);


this.quotient = students.size() / groups;

if (students.size() % groups != 0) {

System.out.println("modulus o");

this.quotient++;

} else {

System.out.println("modulus x");

}

System.out.println("quotient: " + quotient);

this.result = new ArrayList<ArrayList<String>>();


for (int i = 0; i < groups; i++) {

ArrayList<String> names = new ArrayList<String>();


for (int j = 0; j < quotient; j++) {

try {

names.add(students.get(0));

this.students.remove(students.get(0));

} catch (Exception e) {

}

}

this.result.add(names);

}

System.out.println("final result: " + result);

this.main();

}

}


private void printTitle() {

System.out.println("****************************\r\n" + "\r\n" + "        Grouping v2.0\r\n" + "\r\n"

+ "       developed by Byul\r\n" + "****************************");

}


private void print(String s) {

System.out.println(s);

}


private String input() {

this.sc = new Scanner(System.in);

return sc.next();

}

}









댓글

이 블로그의 인기 게시물

substring 예제: 문자열을 입력 받아 한 글자씩 회전시켜 모두 출력하는 프로그램을 작성하라

단체 채팅 구현