Grouping program v3.0


Update: 

    1. can now restart program anytime by pressing 1.

    2. removed unnecessary information printed to the user.

Refer to version 2.0 for 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.print("Enter 1 to restart");

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


while (true) {

String name = input();

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

break;

} else if (name.equals("1")) {

this.main();

}

this.students.add(name);

System.out.println(students);

}

this.grouping();

}


private void grouping() {

if (groups > students.size()) {

this.print("group too big");

this.main();

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

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

this.main();

} 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 v3.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 예제: 문자열을 입력 받아 한 글자씩 회전시켜 모두 출력하는 프로그램을 작성하라

단체 채팅 구현