Skip to content
Open
115 changes: 100 additions & 15 deletions src/main/java/leets/leets_mate/LeetsMateApplication.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,126 @@
package leets.leets_mate;

import leets.leets_mate.exception.InvalidInputException;

import java.util.*;

public class LeetsMateApplication {
private static final String START_MESSAGE = "[Leets ์˜ค๋Š˜์˜ ์ง์—๊ฒŒ]๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.\n";
private static final String GOODBYE_MESSAGE = "์ถ”์ฒจ์„ ์™„๋ฃŒํ•˜์˜€์Šต๋‹ˆ๋‹ค.\n์ž๋ฆฌ๋ฅผ ์ด๋™ํ•ด ์„œ๋กœ์—๊ฒŒ ์ธ์‚ฌํ•ด์ฃผ์„ธ์š”.";
Comment on lines +8 to +9

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ถœ๋ ฅ ๋ฉ”์‹œ์ง€๋ฅผ System.out.println("์ถ”์ฒจ์„ ์™„๋ฃŒํ•˜์˜€์Šต๋‹ˆ๋‹ค"); ์ด๋Ÿฐ ์‹์ด ์•„๋‹Œ ๋”ฐ๋กœ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜์—ฌ System.out.println(GOODBYE_MESSAGE); ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜์‹  ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!!



public void run(){
Scanner sc = new Scanner(System.in);

System.out.println(START_MESSAGE);
System.out.println("๋ฉค๋ฒ„์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (, ๋กœ ๊ตฌ๋ถ„)");
String members = sc.nextLine();

checkHasNoEnglish(members);
List<String> memberList = parseMembers(members);

int maximumGroupSize = getMaximumGroupSize(sc);


int memberCount = memberNumber(memberList);

checkDataValidity(memberCount, maximumGroupSize);

// ๋™์ž‘ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public void run() {
printResultAndMessage(memberList, maximumGroupSize);
while (askForRestart(sc)) {
System.out.println("--------------------------------");
printResultAndMessage(memberList, maximumGroupSize);
}
sc.close();
}

// ๋ฌธ์ž์—ด๋กœ๋œ ๋ฉค๋ฒ„๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public List<String> parseMembers(String members) {
return new ArrayList<>();
return Arrays.asList(members.split(","));
}
private int getMaximumGroupSize(Scanner sc) {
System.out.println("\n์ตœ๋Œ€ ์ง ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
int maximumGroupSize = Integer.parseInt(sc.nextLine());
if (maximumGroupSize <= 0) {
throw new InvalidInputException("[ERROR] ์ตœ๋Œ€ ์ง ์ˆ˜๋Š” 0๋ณด๋‹ค ์ปค์•ผ ํ•ฉ๋‹ˆ๋‹ค");
}
return maximumGroupSize;
}

// ์ด ๋ฉค๋ฒ„์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
public int memberNumber(List<String> members) {
return 0;
return members.size();
}

// ๋ฉค๋ฒ„ ๋ฌธ์ž์—ด์— ์˜์–ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์˜์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ์˜ˆ์™ธ ์ถœ๋ ฅ
public void checkHasNoEnglish(String members) {
public void checkHasNoEnglish(String members){
if (members.matches(".*[a-zA-Z].*")) {
throw new InvalidInputException("[ERROR] ์ด๋ฆ„์€ ํ•œ๊ธ€๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค");
}
if (members.isEmpty()) {
throw new InvalidInputException("[ERROR] ๋ฉค๋ฒ„์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”");
}
}

// ๋ฉค๋ฒ„์ˆ˜์™€ ์ตœ๋Œ€ ์ง์ˆ˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ํšจํ•œ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์œ ํšจํ•˜์ง€ ์•Š๋‹ค๋ฉด ์˜ˆ์™ธ ์ถœ๋ ฅ
public void checkDataValidity(int memberCount, int maximumGroupSize) {
public void checkDataValidity(int memberCount, int maximumGroupSize){
if (maximumGroupSize <= 0) {
throw new InvalidInputException("[ERROR] ์ตœ๋Œ€ ์ง ์ˆ˜๋Š” 0๋ณด๋‹ค ์ปค์•ผ ํ•ฉ๋‹ˆ๋‹ค");
}
if (maximumGroupSize > memberCount) {
throw new InvalidInputException("[ERROR] ์ตœ๋Œ€ ์ง ์ˆ˜๋Š” ์ด๋ฆ„์˜ ๊ฐฏ์ˆ˜๋ณด๋‹ค ํด ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค");
}
}

// ๋žœ๋ค ์ง๊ฟ ์ถ”์ฒจํ•˜๋Š” ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค.
public List<List<String>> generateRandomGroups(List<String> memberList, int maximumGroupSize) {
return new ArrayList<>();
Collections.shuffle(memberList);

List<List<String>> resultList = new ArrayList<>();

int column = maximumGroupSize;
int row = (memberList.size() + column - 1) / column;


for (int i = 0; i < row; i++) {
resultList.add(new ArrayList<>());
for (int j = i*column; j < column*(i+1); j++) {
if(j<memberList.size()) resultList.get(i).add(memberList.get(j));
}
}
return resultList;
Comment on lines +75 to +85

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋‹ค์ค‘ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹ ๋ง๊ณ  ๋นˆ ArrayList์— member๋ฅผ ๋„ฃ์–ด์ฃผ๋‹ค๊ฐ€ maximumGroupSize๊ฐ€ ๋˜๋ฉด ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ๋ฅผ resultList์— ๋„ฃ๋Š” ๋ฐฉ์‹๋„ ๊ณ ๋ คํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค

}

// ๊ฒฐ๊ณผ๋ฅผ ํ”„๋ฆฐํŠธ ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public void printResult(List<List<String>> result) {
StringBuilder sb = new StringBuilder();

for(int i=0; i<result.size(); i++){
sb.append("[ ");
for(int j=0; j<result.get(i).size(); j++){
sb.append(result.get(i).get(j));
if(j<result.get(i).size()-1) sb.append(" | ");
}
sb.append(" ]");
sb.append("\n");
}
System.out.println(sb);
}
Comment on lines 88 to +101

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringJoiner ์‚ฌ์šฉ๋„ ๊ณ ๋ คํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!

public void printResultAndMessage(List<String> memberList, int maximumGroupSize) {
List<List<String>> lists = generateRandomGroups(memberList, maximumGroupSize);
System.out.println("\n์˜ค๋Š˜์˜ ์ง ์ถ”์ฒœ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.");
printResult(lists);
System.out.println(GOODBYE_MESSAGE);
}
public boolean askForRestart(Scanner sc) {
while (true) {
System.out.print("๋‹ค์‹œ ๊ตฌ์„ฑํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (y or n): ");
String retry = sc.nextLine().trim().toLowerCase();
if (retry.equals("n")) {
System.out.println("์ž๋ฆฌ๋ฅผ ์ด๋™ํ•ด ์„œ๋กœ์—๊ฒŒ ์ธ์‚ฌํ•ด์ฃผ์„ธ์š”.");
return false;
} else if (retry.equals("y")) {
return true;
} else {
throw new InvalidInputException("[ERROR] ์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค. 'y' ๋˜๋Š” 'n'์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
}
}
}

public static void main(String[] args) {
public static void main(String[] args) throws Exception {
LeetsMateApplication app = new LeetsMateApplication();
app.run();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package leets.leets_mate.exception;

public class InvalidInputException extends RuntimeException{
public InvalidInputException(String message) {
super(message);
}

}