-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[문자열 덧셈 계산기] 심규선 미션 제출합니다. #3
base: main
Are you sure you want to change the base?
Changes from 17 commits
c2da9ed
71d4d66
9b3087a
0ed76bf
a4a5e43
b8100a9
84b427c
e1c9b02
16f127d
8ebeb44
3e32b1a
ca103e3
3c0c4bb
ee7f4e5
829fb98
c09784f
b1d1c7b
f766733
c158903
166f009
8f8b0e2
1f7cdef
cdfa01b
a2a0898
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
# java-calculator-precourse | ||
# java-calculator-precourse | ||
|
||
--- | ||
## 🛠️구현할 기능 목록 | ||
|
||
### 사용자가 입력한 문자열 읽는 기능 | ||
- [x] camp.nextstep.edu.missionutils에서 제공하는 Console API를 사용하여 구현 | ||
### 커스텀 구분자 추출 기능 | ||
- [x] 커스텀 구분자가 있을시, 구분자 List에 추가 | ||
- [x] 커스텀 구분자는 1개로 제한 (추후에 리팩토링 예정) | ||
### 숫자 추출 기능 | ||
- [x] StringBuilder와 정규 표현식을 이용하여 숫자만 추출 | ||
- [x] 음수 포함 시 에러 발생 | ||
### 계산 기능 | ||
- [x] 추출한 숫자 바탕으로 덧셈 연산 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
package calculator; | ||
|
||
import calculator.controller.CalculatorController; | ||
import camp.nextstep.edu.missionutils.Console; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
// TODO: 프로그램 구현 | ||
CalculatorController calculatorController = new CalculatorController(); | ||
calculatorController.run(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package calculator.controller; | ||
|
||
import calculator.model.Calculator; | ||
import calculator.model.Extractor; | ||
import calculator.view.InputView; | ||
import calculator.view.OutputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CalculatorController { | ||
|
||
private final Calculator calculator; | ||
private final Extractor extractor; | ||
private final InputView inputView; | ||
private final OutputView outputView; | ||
|
||
public CalculatorController() { | ||
this.calculator = new Calculator(); | ||
this.extractor = new Extractor(); | ||
this.inputView = new InputView(); | ||
this.outputView = new OutputView(); | ||
} | ||
|
||
public void run() { | ||
List<Long> numbers = new ArrayList<>(); | ||
|
||
String input = inputView.read(); | ||
inputView.close(); | ||
|
||
extractor.extractCustomDelimiter(input); | ||
extractor.extractNumbers(input, numbers); | ||
|
||
long result = calculator.calculateSum(numbers); | ||
|
||
outputView.print(result); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package calculator.model; | ||
|
||
import java.util.List; | ||
|
||
public class Calculator { | ||
|
||
public long calculateSum(List<Long> numbers) { | ||
long sum = 0; | ||
for(Long number : numbers) { | ||
sum += number; | ||
} | ||
return sum; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package calculator.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Extractor { | ||
|
||
private List<Character> delimiters = new ArrayList<>(Arrays.asList(',', ':')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 의견 감사합니다 👏 |
||
private boolean isCustomDelimiter = false; | ||
|
||
public void extractCustomDelimiter(String input) { | ||
if(input.startsWith("//") && input.substring(3,5).equals("\\n")){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 서비스에서 지속적으로 사용되는 상수들을 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enum을 적용하면 나중에 유지보수하기 편할거 같네요 !! |
||
delimiters.add(input.charAt(2)); | ||
isCustomDelimiter = true; | ||
} | ||
} | ||
|
||
public void extractNumbers(String input, List<Long> numbers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 참고하겠습니다 👍 |
||
|
||
input = removeCustomDelimiter(input); | ||
|
||
String regex = makeRegex(); | ||
|
||
// 구분자를 기준으로 문자열을 숫자들로 분리 | ||
// 이때 구분자가 아닌 문자가 포함되어 있을 수 있는데, 숫자인지 확인하는 메소드에서 걸러짐 | ||
String[] tokens = input.split(regex); | ||
for (String token : tokens) { | ||
if (!token.isEmpty()) { | ||
Long number = validateIsNumber(token); | ||
validateNegativeNumber(number); | ||
numbers.add(number); | ||
} | ||
} | ||
} | ||
|
||
//커스텀 구분자를 제거하는 메소드 | ||
private String removeCustomDelimiter(String input) { | ||
if(isCustomDelimiter) { | ||
input = input.substring(5); | ||
} | ||
return input; | ||
} | ||
|
||
//구분자를 생성하는 메소드 | ||
private String makeRegex() { | ||
StringBuilder regex = new StringBuilder("["); | ||
for (char delimiter : delimiters) { | ||
regex.append("\\").append(delimiter); | ||
} | ||
regex.append("]"); | ||
|
||
return regex.toString(); | ||
} | ||
|
||
//분리한 문자열이 숫자인지 확인하는 메소드 | ||
private Long validateIsNumber(String input) { | ||
try{ | ||
return Long.parseLong(input); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("잘못된 입력입니다."); | ||
} | ||
} | ||
|
||
//음수인지 확인하는 메소드 | ||
private void validateNegativeNumber(Long number) { | ||
if(number < 0) { | ||
throw new IllegalArgumentException("음수가 포함되어 있습니다."); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package calculator.view; | ||
|
||
import camp.nextstep.edu.missionutils.Console; | ||
|
||
public class InputView { | ||
|
||
public String read() { | ||
System.out.println("덧셈할 문자열을 입력해 주세요."); | ||
return Console.readLine(); | ||
} | ||
|
||
public void close() { | ||
Console.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package calculator.view; | ||
|
||
public class OutputView { | ||
|
||
public void print(long result) { | ||
System.out.println("결과 : " + result); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import 목록 정리 한번 하시면 좋을 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확인했습니다 😊