Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 101 additions & 8 deletions src/main/java/leets/leets_mate/LeetsMateApplication.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,134 @@
package leets.leets_mate;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.SQLOutput;
import java.util.*;
import java.io.*;
import java.util.regex.Pattern;

public class LeetsMateApplication {

// ๋™์ž‘ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public void run() {
public void run() throws Exception {

System.out.println("[Leets ์˜ค๋Š˜์˜ ์ง์—๊ฒŒ]๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.\n");

//๋ฉค๋ฒ„ ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ธฐ ๋ฐ ํŒŒ์‹ฑ
System.out.println("๋ฉค๋ฒ„์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (, ๋กœ ๊ตฌ๋ถ„)");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();

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.

๋ฌด์Šจ ์ธํ’‹์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋ช…์ด๋ฉด ์ข‹์„๊ฑฐ ๊ฐ™์•„์š”! ์ง€๊ธˆ์€ input? ๋ฌด์Šจ input์ด์ง€? ํ•˜๊ณ  ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ด์•ผํ•ด์„œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค!

List<String> memberList = parseMembers(input);

//์ตœ๋Œ€ ์ง ์ˆ˜ ์ž…๋ ฅ ๋ฐ›๊ธฐ ๋ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
System.out.println("\n์ตœ๋Œ€ ์ง ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
int maximumGroupSize = 0;
try{
maximumGroupSize = Integer.parseInt(sc.nextLine());
}
catch (Exception e){
throw new Exception("[ERROR] ์ตœ๋Œ€ ์ง ์ˆ˜๋Š” ์ˆซ์ž์—ฌ์•ผํ•ฉ๋‹ˆ๋‹ค");
}
checkDataValidity(memberNumber(memberList), maximumGroupSize);

char re = 'y';

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.

์˜๋ฏธ๊ฐ€ ๋ช…ํ™•ํ•œ ๋ณ€์ˆ˜๋ช…์ด๋ฉด ์ข‹์„๊ฑฐ ๊ฐ™์•„์š”!

while(re == 'y'){
//๋žœ๋ค ์ง ์ƒ์„ฑ ๋ฐ ์ถœ๋ ฅ
System.out.println("\n์˜ค๋Š˜์˜ ์ง ์ถ”์ฒœ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.");
List<List<String>> result = generateRandomGroups(memberList, maximumGroupSize);
printResult(result);
System.out.println("\n์ถ”์ฒœ์„ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค.");

//์žฌ๊ตฌ์„ฑ
System.out.print("๋‹ค์‹œ ๊ตฌ์„ฑํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? (y or n): ");
re = sc.nextLine().charAt(0);
if (re != 'y' && re != 'n') {
throw new Exception("[ERROR]'y' ๋˜๋Š” 'n'์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
}
if(re == 'y'){
System.out.print("--------------------------------");
}
}
System.out.println("์ž๋ฆฌ๋ฅผ ์ด๋™ํ•ด ์„œ๋กœ์—๊ฒŒ ์ธ์‚ฌํ•ด์ฃผ์„ธ์š”.");

}

// ๋ฌธ์ž์—ด๋กœ๋œ ๋ฉค๋ฒ„๋“ค์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
public List<String> parseMembers(String members) {
return new ArrayList<>();
public List<String> parseMembers(String members) throws Exception {
checkHasNoEnglish(members);
List<String> memberList = Arrays.asList(members.split(","));
return memberList;
}

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

// ๋ฉค๋ฒ„ ๋ฌธ์ž์—ด์— ์˜์–ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์˜์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ์˜ˆ์™ธ ์ถœ๋ ฅ
public void checkHasNoEnglish(String members) {
public void checkHasNoEnglish(String members) throws Exception {
// ๋ฉค๋ฒ„ ๋ฌธ์ž์—ด์— ํ•œ๊ธ€, ์ปด๋งˆ(,), ๊ณต๋ฐฑ๋งŒ ํ—ˆ์šฉ
if(!Pattern.matches("^[ใ„ฑ-ใ…Ž๊ฐ€-ํžฃ,\\s]*$", members)){
//์˜ˆ์™ธ ๋ฐœ์ƒ
throw new Exception("[ERROR] ์ด๋ฆ„์€ ํ•œ๊ธ€๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค");
}
}

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

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.

์ €๋Š” ์ง ์ˆ˜๊ฐ€ 0์ด ๋  ์ˆ˜ ์—†๋‹ค๋Š” ์ ์„ ๋†“์ณค์—ˆ๋Š”๋ฐ, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ž˜ ํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

throw new Exception("[ERROR] ์ตœ๋Œ€ ์ง ์ˆ˜๋Š” 1 ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค\n");

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.

์ง ์ˆ˜ ์ž…๋ ฅ๊ฐ’์ด 0์ดํ•˜์ธ๊ฑด ์ƒ๊ฐ ๋ชปํ–ˆ๋Š”๋ฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์•„์ฃผ ์ข‹์Šต๋‹ˆ๋‹ค!! :)

}
}

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

Collections.shuffle(memberList);

List<String> mate = new ArrayList<>();
List<List<String>> result = new ArrayList<>();

int memberCount = memberNumber(memberList);

for(int i = 0; i<memberCount; i++){
mate.add(memberList.get(i));

if(mate.size() == maximumGroupSize
|| i == memberCount - 1){
result.add(new ArrayList<>(mate));
mate = new ArrayList<>();
}
}

return result;
}

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

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.

Iterator ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•œ ๋ฌธ์ž์—ด ์ˆœํšŒ์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ ๋กœ์ง์„ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•ด ์ค€ ๋ถ€๋ถ„์ด ์ •๋ง ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

System.out.print("[ ");
Iterator<String> iterator = mate.iterator();

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.

Iterator๋ฅผ ์ด์šฉํ•ด์„œ ์ง์„ ๋งŒ๋“œ์‹  ์ ์ด ์ธ์ƒ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค!

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.

Iterator ๋ฐฉ์‹๋„ ์ข‹๋„ค์š”!


while (iterator.hasNext()) {
String name = iterator.next();
System.out.print(name);

if (iterator.hasNext()) {
System.out.print(" | ");
}
}

System.out.print(" ]\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
Expand Up @@ -18,7 +18,7 @@ void setUp() {
}

@Test
void ์ž…๋ ฅ๋ฐ›์€_๋ฌธ์ž์—ด์„_ํŒŒ์‹ฑํ•˜์—ฌ_๋ฆฌ์ŠคํŠธ๋กœ_๋งŒ๋“ ๋‹ค() {
void ์ž…๋ ฅ๋ฐ›์€_๋ฌธ์ž์—ด์„_ํŒŒ์‹ฑํ•˜์—ฌ_๋ฆฌ์ŠคํŠธ๋กœ_๋งŒ๋“ ๋‹ค() throws Exception {
String members = "๋ฆฌ์ธ ์—,์˜ค์‹ ,๊ฑธ,ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค";
List<String> actual = app.parseMembers(members);
assertThat(actual).containsExactly("๋ฆฌ์ธ ์—", "์˜ค์‹ ", "๊ฑธ", "ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค");
Expand Down