Skip to content
Merged
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
48 changes: 32 additions & 16 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# Calculat
# 계산기 프로젝트


## 프로젝트 설명

Java 문법 종합반의 강의 3주차까지(최소)듣고 그것을 기반으로 사칙연산이 가능한 계산기를 코딩

## 요구사항

### 필수 요구사항(여기까지 구현)
- 정수를 입력받기
- 사칙 연산 입력받기
- 위 두개의 변수로 사칙 연산
- 연산된 값 출력
- 무한 반복문을 사용
- "exit"입력 받으면 시스템 종료
- 위 내용을 클래스/메소드로 찢어서 작성
- 캡슐화(Getter & Setter)

### 도전 요구사항
- double 타입의 값을 전달 받아도 연산이 수행(제네릭)
- Scanner로 입력받은 값보다 큰 결과값 들을 출력(람다식 & 스트림)
- Enum 타입을 활용

## 사용방법
1. 코드를 실행시킨다
2. 콘솔에서 1번째 숫자를 입력받는다
3. 콘솔에서 연산자를 입력받는다
4. 콘솔에서 2번째 숫자를 입력받는다
5. 위 숫자와 연산자로 연산후 출력된다
6. 어디서나 'exit'을 입력 받으면 시스템이 종료된다

## 예외 사항
- 숫자를 입력 받을때 정규식을 통해 숫자만 입력 받는다
- 연산자를 입력 받을때 정규식을 통해 연산자만 입력 받는다

## 참조 자료
- 스파르타 코딩 클럽 Java 문법 종합반 1~5주차 강의
9 changes: 0 additions & 9 deletions src/DTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public class DTO {
private int firstNumber=0;
private int secondNumber=0;
private String algorithm="";


public int getFirstNumber(){
Expand All @@ -22,13 +21,5 @@ public void setSecondNumber(int number){
this.secondNumber = number;
}

public String getAlgorithm(){
return algorithm;
}

public void setAlgorithm(String mark){
this.algorithm = mark;
}


}
4 changes: 2 additions & 2 deletions src/Four_Basic/multiplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package Four_Basic;

public class multiplication extends operationing{
public class multiplication extends operationing {

@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber*secondNumber;
return firstNumber * secondNumber;
}
}
8 changes: 3 additions & 5 deletions src/InfoInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

public class InfoInput {

public static boolean input() throws Exception{
public static boolean input() throws Exception {
Regular regular = new Regular();
Scanner sc =new Scanner(System.in);
Scanner sc = new Scanner(System.in);


System.out.println("===============");
System.out.println("숫자를 입력해주세요");
String FirstInput = sc.nextLine();
regular.CheckFirstNum(FirstInput);

System.out.println("===============");
System.out.println("연산자를 입력해주세요");
String operator = sc.nextLine();
regular.CheckSign(operator);

System.out.println("===============");
System.out.println("숫자를 입력해주세요");
String secondInput = sc.nextLine();
regular.CheckSecondNum(secondInput);
Expand Down
3 changes: 2 additions & 1 deletion src/InoutExceotion.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public class InoutExceotion extends Exception{
public InoutExceotion(String word) {
super(word + "을 입력해주세요");
super("==================\n"+word + "을 입력해주세요\n==================");

}
}
7 changes: 3 additions & 4 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ public class Main {
public static void main(String[] args) {
boolean End = false;

while (!End){
while (!End) {
try {
End = InfoInput.input();
}catch (Exception e) {
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
Expand All @@ -24,7 +24,7 @@ public void Lv_1() {
String algorithm = "";
int answer = 0;

for (; ; ) {
while(true) {
System.out.println("계산할 숫자를 입력해주세요");
System.out.println("-------------");

Expand Down Expand Up @@ -124,4 +124,3 @@ public void Lv_1() {
}
}
}

38 changes: 20 additions & 18 deletions src/Regular.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.util.regex.Pattern;

import Four_Basic.*;

public class Regular {
Expand All @@ -7,42 +8,43 @@ public class Regular {

private final Calculator calculator = new Calculator();

public Regular CheckFirstNum(String FirstNum) throws Exception{
ExitSign(FirstNum);
if(!Pattern.matches(number,FirstNum)) {
throw new InoutExceotion("정수");
//정규식을 통한 첫번째 숫자 검사
public Regular CheckFirstNum(String FirstNum) throws Exception {
ExitSign(FirstNum); // 종료 메서드
if (!Pattern.matches(number, FirstNum)) { // 보내진 문자열이 졍규식에 포함되있는지 확인
throw new InoutExceotion("정수"); // 예외처리시 입력할 타입
}
this.calculator.DTO.setFirstNumber(Integer.parseInt(FirstNum));
this.calculator.DTO.setFirstNumber(Integer.parseInt(FirstNum)); // 검사가 통과된 숫자 setter로 저장

return this;
return this;
}

public Regular CheckSecondNum(String SecondNum) throws Exception{
public Regular CheckSecondNum(String SecondNum) throws Exception {
ExitSign(SecondNum);
if(!Pattern.matches(number,SecondNum)) {
if (!Pattern.matches(number, SecondNum)) {
throw new InoutExceotion("정수");
}
this.calculator.DTO.setSecondNumber(Integer.parseInt(SecondNum));

return this;
return this;
}

public Regular CheckSign(String operation) throws Exception{
public Regular CheckSign(String operation) throws Exception {
ExitSign(operation);
if(!Pattern.matches(this.operation,operation)) {
if (!Pattern.matches(this.operation, operation)) {
throw new InoutExceotion("사칙 연산자");
}
switch (operation){
case "+" :
switch (operation) {
case "+":
this.calculator.setOperation(new addition());
break;
case "-" :
case "-":
this.calculator.setOperation(new subtraction());
break;
case "*" :
case "*":
this.calculator.setOperation(new multiplication());
break;
case "/" :
case "/":
this.calculator.setOperation(new division());
break;
}
Expand All @@ -51,13 +53,13 @@ public Regular CheckSign(String operation) throws Exception{
}

public void ExitSign(String sign) {
if(sign.equals("exit")){
if (sign.equals("exit")) {
System.out.println("시스템 종료");
System.exit(0);
}
}

public int execut(){
public int execut() {
return calculator.calculate();
}

Expand Down