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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
testImplementation platform('org.assertj:assertj-bom:3.25.1')
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation('org.assertj:assertj-core')
testImplementation('org.mockito:mockito-core')
}

tasks.named('test') {
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/leets/land/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package leets.land;

import leets.land.domain.Range;
import leets.land.domain.CharRange;
import leets.land.view.InputView;
import leets.land.view.OutputView;

public class Controller {
private final InputView inputView = new InputView();
private final OutputView outputView = new OutputView();
public void run() {
int version = inputView.getVersionInput();
if (version == 1) {
numberVersion();
} else {
alphaVersion();
}
}
public void numberVersion() {
double randomValue = Math.random();
int randomNumber = (int)(randomValue *100) +1;
int cnt = 0;
int correct = -1;

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.

correctλ₯Ό -1둜 λ‘” μ΄μœ κ°€ λ¬΄μ—‡μΈκ°€μš”?
μ˜λ„λ₯Ό νŒŒμ•…ν•˜κΈ° νž˜λ“  것 κ°™μ•„μš”

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

correctλŠ” μ •λ‹΅μ΄λΌλŠ” 의미의 λ³€μˆ˜μž…λ‹ˆλ‹€.
1둜 λ‘μ—ˆλ‹€κ°€ ν•œλ²ˆμ— 정닡이 λ˜λŠ” ν˜„μƒμ„ λ°©μ§€ν•˜κΈ° μœ„ν•¨μ΄μ—ˆμŠ΅λ‹ˆλ‹€!

Range range = new Range(1, 100);
while (correct != randomNumber) {

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.

μ •λ‹΅κ³Ό λΉ„κ΅ν•˜λŠ” 게 Controller의 μ—­ν• μΈκ°€μš”?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

contoller μ—μ„œ λΆ„λ¦¬ν•˜λ €κ³  ν•˜λŠ”λ° μƒˆλ‘œμš΄ 클래슀λ₯Ό λ§Œλ“€μ–΄μ„œ λ”°λ‘œ κ²€μ¦ν•˜λŠ” 것이 더 쒋은 λ°©ν–₯μΌκΉŒμš”?

correct = inputView.getNumberInput(range);
printUpDown(correct, randomNumber, range); // Call printUpDown function
cnt ++;
}
outputView.printCorrectMessage(cnt);
}

public void printUpDown(int correct, int randomNumber, Range range) {
if (correct < randomNumber) {
System.out.println("UP");
range.setMin(correct + 1);
} else if (correct > randomNumber) {
System.out.println("DOWN");
range.setMax(correct - 1);
}
}

public void alphaVersion() {
char randomAlpha = (char)((Math.random() * 58) + 65);
int cnt = 0;
char correct = 'A'-1;
CharRange range = new CharRange('A', 'z');
while (correct != randomAlpha) {
correct = inputView.getCharInput(range);
printUpDown(correct, randomAlpha, range);
cnt ++;
}
outputView.printCorrectMessage(cnt);
}

public void printUpDown(char correct, char randomAlpha, CharRange range) {
if (correct < randomAlpha) {
System.out.println("UP");
range.setMin((char)(correct + 1));
} else if (correct > randomAlpha) {
System.out.println("DOWN");
range.setMax((char)(correct - 1));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

a->Zκ°€ λ˜λŠ” κ²½μš°λ„ μ•„μŠ€ν‚€ μ½”λ“œλ₯Ό μƒκ°ν•΄μ„œ ν•΄μ£Όμ…”μ•Όλ κ±°κ°™μ•„μš”

}
}
}
4 changes: 2 additions & 2 deletions src/main/java/leets/land/UpdownApplication.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package leets.land;

public class UpdownApplication {

public static void main(String[] args) {
System.out.print("hihi :D");
Controller controller = new Controller();
controller.run();
}
}
31 changes: 31 additions & 0 deletions src/main/java/leets/land/domain/CharRange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package leets.land.domain;

public class CharRange {
private char min;
private char max;

public CharRange(char min, char max) {
this.min = min;
this.max = max;
}

public char getMin() {
return min;
}

public char getMax() {
return max;
}

public void setMin(char min) {
this.min = min;
}

public void setMax(char max) {
this.max = max;
}
Comment on lines +20 to +26

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.

객체가 μΌν•˜κ²Œ ν•˜λŠ” 것이 μ–΄λ–¨κΉŒμš”?


public boolean isInRange(char value) {
return value >= min && value <= max;
}
Comment on lines +28 to +30

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.

객체가 μΌν•˜κ³  μžˆλ„€μš” ! μ’‹μ•„μš”πŸ‘

}
31 changes: 31 additions & 0 deletions src/main/java/leets/land/domain/Range.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package leets.land.domain;

public class Range {
private int min;
private int max;

public Range(int min, int max) {
this.min = min;
this.max = max;
}

public int getMin() {
return min;
}

public void setMin(int min) {
this.min = min;
}

public int getMax() {
return max;
}

public void setMax(int max) {
this.max = max;
}

public boolean isInRange(int value) {
return value >= min && value <= max;
}
}
84 changes: 84 additions & 0 deletions src/main/java/leets/land/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package leets.land.view;

import leets.land.domain.CharRange;
import leets.land.domain.Range;

import java.util.InputMismatchException;
import java.util.Scanner;

public class InputView {
private final Scanner scanner = new Scanner(System.in);

public int getVersionInput() {
System.out.print("버전을 μž…λ ₯ν•΄μ£Όμ„Έμš” (숫자 버전: 1, μ˜μ–΄ 버전: 2) : ");
try {
int version = scanner.nextInt();
validateVersion(version);
return version;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getVersionInput();
} catch (InputMismatchException e) {
System.out.println("[ERROR] 버전을 μž…λ ₯ν•΄μ£Όμ„Έμš” (숫자 버전: 1, μ˜μ–΄ 버전: 2) : ");
scanner.nextLine();
return getVersionInput();
}
}

public void validateVersion(int version) {
if (version != 1 && version != 2) {
throw new IllegalArgumentException("[ERROR] μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ²„μ „μž…λ‹ˆλ‹€.");
}
}

public int getNumberInput(Range range) {
System.out.printf("숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”(%d ~ %d) : ",range.getMin(),range.getMax());
try {
int number = scanner.nextInt();
validateNumberRange(range, number);
return number;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getNumberInput(range);
} catch (InputMismatchException e) {
System.out.println("[ERROR] 숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” ");
scanner.nextLine();
return getNumberInput(range);
}
}

public void validateNumberRange(Range range, int number) {
if (!range.isInRange(number)) {
throw new IllegalArgumentException("[ERROR] λ²”μœ„ λ‚΄μ˜ 숫자λ₯Ό μž…λ ₯ν•˜μ„Έμš”. ");
}
}

public char getCharInput(CharRange range) {
System.out.printf("μ˜μ–΄λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”(%c ~ %c) : ",range.getMin(),range.getMax());
String input = scanner.next();
try {
validCharLength(input);
char character = input.charAt(0);
validateCharRange(range, character);
return character;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return getCharInput(range);
} catch (InputMismatchException e) {
System.out.println("μž…λ ₯ 문자의 νƒ€μž…μ΄ λ§žμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ");
scanner.nextLine();
return getCharInput(range);
}
}
public void validateCharRange(CharRange range, char character) {
if (!range.isInRange(character)) {
throw new IllegalArgumentException("[ERROR] λ²”μœ„ λ‚΄μ˜ μ•ŒνŒŒλ²³μ„ μž…λ ₯ν•˜μ„Έμš”. ");
}
}
public void validCharLength(String s) {
if (s.length() >= 2) {
throw new IllegalArgumentException("[ERROR] μ•ŒνŒŒλ²³ ν•œ κΈ€μžλ§Œ μž…λ ₯ν•΄μ£Όμ„Έμš” ");
}
}
Comment on lines +78 to +82

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.

μ•ŒνŒŒλ²³μ„ μ—¬λŸ¬κ°œλ₯Ό μž…λ ₯ν•˜λŠ” μ˜ˆμ™Έμ²˜λ¦¬λŠ” 생각λͺ»ν–ˆλŠ”λ° 쒋은거 κ°™μŠ΅λ‹ˆλ‹€!


}
9 changes: 9 additions & 0 deletions src/main/java/leets/land/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package leets.land.view;

public class OutputView {
public void printCorrectMessage(int cnt) {
System.out.println("μ •λ‹΅");

System.out.printf("μ‹œλ„ν•œ 횟수 : %d회",cnt);
}
}
14 changes: 12 additions & 2 deletions src/test/java/leets/land/UpdownApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package leets.land;

import leets.land.view.InputView;
import leets.land.view.OutputView;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;


class UpdownApplicationTests {
UpdownApplication app;
private Controller controller;
private InputView inputViewMock;
private OutputView outputViewMock;


@Test
void contextLoads() {
public void setUp() {
controller = new Controller();
}

}
50 changes: 50 additions & 0 deletions src/test/java/leets/land/view/InputViewTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package leets.land.view;

import leets.land.domain.CharRange;
import leets.land.domain.Range;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Random;

import static org.junit.jupiter.api.Assertions.*;

class InputViewTest {
InputView inputView;
Range range;
@BeforeEach
void setUp() {
inputView = new InputView();
}

@DisplayName("μ§€μ • 버전 이외에 버전을 μž…λ ₯ν•˜λ©΄ μ˜ˆμ™Έμ²˜λ¦¬")
@Test
void getVersionInput() {
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, ()->
inputView.validateVersion(0) );
}

@DisplayName("λ²”μœ„ μ™Έμ˜ μˆ«μžκ°€ μž…λ ₯되면 μ˜ˆμ™Έμ²˜λ¦¬")
@Test
void validateNumberRange() {
range = new Range(1,100);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, ()->
inputView.validateNumberRange(range,101) );
}

@DisplayName("λ²”μœ„ μ™Έμ˜ μ•ŒνŒŒλ²³μ΄ μž…λ ₯되면 μ˜ˆμ™Έμ²˜λ¦¬")
@Test
void validateCharRange() {
CharRange charRange = new CharRange('A','z');
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, ()->
inputView.validateCharRange(charRange,',') );
}
@DisplayName("μ•ŒνŒŒλ²³μ΄ λ‘κ°œ μž…λ ₯되면 μ˜ˆμ™Έμ²˜λ¦¬")
@Test
void validCharLength() {
String doubleString = "ab";
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, ()->
inputView.validCharLength(doubleString));
}
}