Skip to content
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

Yeongsu1000 #1903

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# java-calculator-precourse
# java-calculator-precourse
# 문자열 덧셈 계산기
## 구현할 기능 목록
1) 입력 메소드
- `덧셈할 문자열을 입력해 주세요.` 를 출력한다.
- 사용자로부터 문자열을 입력받는다.
2) 입력값 확인
- 빈 문자열인 경우 0을 출력한다.
- 숫자 하나를 입력할 경우 해당 숫자를 출력한다.
- 커스텀 구분자를 사용했는지 확인한다.
3) 덧셈 계산 메소드

4) 예외 처리
- 잘못된 값을 입력할 경우 `IllegalArgumentException` 을 발생시킨 후 종료한다.
5) 결과
14 changes: 14 additions & 0 deletions src/main/java/IO/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package IO;

import camp.nextstep.edu.missionutils.Console;

public class Input {
public String input;
public void message(){
System.out.println("덧셈할 문자열을 입력해 주세요.");
}
public String input(){
input = Console.readLine();
return input;
}
}
7 changes: 7 additions & 0 deletions src/main/java/IO/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package IO;

public class Output {
public void output(){
System.out.println("결과 : ");
}
}
29 changes: 29 additions & 0 deletions src/main/java/Validate/InputValidate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Validate;

import IO.Input;

public class InputValidate {
public String userInput;

public void validate(){
Input input = new Input();
input.message();
userInput = input.input();
}

// 빈 문자열 검사
public boolean empty() {
return userInput.isEmpty();
}

// 숫자 하나 검사
public boolean oneNumber() {
return userInput.length() == 1;
}

// 커스텀 구문자 검사
public boolean custom() {
return userInput.length() > 4 && userInput.substring(0,2).equals("//") && userInput.substring(3,5).equals("/n");
}

}
18 changes: 18 additions & 0 deletions src/main/java/Validate/NumValidate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Validate;

public class NumValidate {
public int[] validate(String[] inputNum) {
int[] num = new int [inputNum.length];
for (int i = 0; i < inputNum.length; i++) {
try{
num[i] = Integer.parseInt(inputNum[i]);
}catch(IllegalArgumentException e){
System.out.println("수가 아닙니다.");
}
if(num[i] < 0) {
throw new IllegalArgumentException("음수가 입력됐습니다.");
}
}
return num;
}
}
3 changes: 3 additions & 0 deletions src/main/java/calculator/Application.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package calculator;

import IO.Input;
import Validate.InputValidate;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/calculator/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package calculator;

import Validate.InputValidate;
import Validate.NumValidate;

public class Calculator {
public String calculator() {
InputValidate inputValidate = new InputValidate();
inputValidate.validate();
if (inputValidate.empty()) return case1();
if (inputValidate.oneNumber()) return case2(inputValidate.userInput);

String separators = ",:";
if (inputValidate.custom()) {
separators += inputValidate.userInput.charAt(2);
}
return case3(inputValidate.userInput, separators);
}

// 빈 문자열
public String case1() {
return "0";
}

// 숫자 하나
public String case2(String userInput) {
return userInput;
}

// 구문자
public String case3(String userInput, String separators) {

String[] input = userInput.split("[" + separators + "]");
int[] num = new int[input.length];
int sum = 0;
// 구문자로 나눈 숫자가 유효한지 검사
NumValidate numValidate = new NumValidate();
num = numValidate.validate(input);

for (int i = 0; i < num.length; i++) {
sum += num[i];
}
String result = String.valueOf(sum);
return result;
}


}