diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d042880..c901b92 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -8,14 +8,13 @@ - - - + + + { @@ -54,19 +58,19 @@ - { - "keyToString": { - "Application.Main.executor": "Run", - "RunOnceActivity.ShowReadmeOnStart": "true", - "git-widget-placeholder": "main", - "ignore.virus.scanning.warn.message": "true", - "kotlin-language-version-configured": "true", - "last_opened_file_path": "C:/Users/ekaql/OneDrive/바탕 화면/개발/Kotlin & Spring/Java_cata/Java_문법_종합반_권준석/Java_문법_종합반_권준석/Calculator_Machine2", - "onboarding.tips.debug.path": "C:/Users/ekaql/OneDrive/바탕 화면/개발/팀 프로젝트/untitled/src/Main.java", - "settings.editor.selected.configurable": "preferences.general", - "애플리케이션.Main.executor": "Run" + +}]]> @@ -83,7 +87,19 @@ - + + + diff --git a/README.md b/README.md index 42d877a..f94fc4d 100644 --- a/README.md +++ b/README.md @@ -1 +1,38 @@ -# Calculat +# 계산기 프로젝트 + + +## 프로젝트 설명 + +Java 문법 종합반의 강의 3주차까지(최소)듣고 그것을 기반으로 사칙연산이 가능한 계산기를 코딩 + +## 요구사항 + +### 필수 요구사항(여기까지 구현) +- 정수를 입력받기 +- 사칙 연산 입력받기 +- 위 두개의 변수로 사칙 연산 +- 연산된 값 출력 +- 무한 반복문을 사용 +- "exit"입력 받으면 시스템 종료 +- 위 내용을 클래스/메소드로 찢어서 작성 +- 캡슐화(Getter & Setter) + +### 도전 요구사항 +- double 타입의 값을 전달 받아도 연산이 수행(제네릭) +- Scanner로 입력받은 값보다 큰 결과값 들을 출력(람다식 & 스트림) +- Enum 타입을 활용 + +## 사용방법 +1. 코드를 실행시킨다 +2. 콘솔에서 1번째 숫자를 입력받는다 +3. 콘솔에서 연산자를 입력받는다 +4. 콘솔에서 2번째 숫자를 입력받는다 +5. 위 숫자와 연산자로 연산후 출력된다 +6. 어디서나 'exit'을 입력 받으면 시스템이 종료된다 + +## 예외 사항 +- 숫자를 입력 받을때 정규식을 통해 숫자만 입력 받는다 +- 연산자를 입력 받을때 정규식을 통해 연산자만 입력 받는다 + +## 참조 자료 +- 스파르타 코딩 클럽 Java 문법 종합반 1~5주차 강의 diff --git a/src/DTO.java b/src/DTO.java index 7b6e813..73e0f5c 100644 --- a/src/DTO.java +++ b/src/DTO.java @@ -3,7 +3,6 @@ public class DTO { private int firstNumber=0; private int secondNumber=0; - private String algorithm=""; public int getFirstNumber(){ @@ -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; - } - } diff --git a/src/Four_Basic/multiplication.java b/src/Four_Basic/multiplication.java index 52bf6a5..2f37e15 100644 --- a/src/Four_Basic/multiplication.java +++ b/src/Four_Basic/multiplication.java @@ -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; } } diff --git a/src/InfoInput.java b/src/InfoInput.java index 01b30f8..1bedf9d 100644 --- a/src/InfoInput.java +++ b/src/InfoInput.java @@ -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); diff --git a/src/InoutExceotion.java b/src/InoutExceotion.java index 903da7c..9592e35 100644 --- a/src/InoutExceotion.java +++ b/src/InoutExceotion.java @@ -1,5 +1,6 @@ public class InoutExceotion extends Exception{ public InoutExceotion(String word) { - super(word + "을 입력해주세요"); + super("==================\n"+word + "을 입력해주세요\n=================="); + } } diff --git a/src/Main.java b/src/Main.java index 1ed18ab..50aabcc 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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()); } } @@ -24,7 +24,7 @@ public void Lv_1() { String algorithm = ""; int answer = 0; - for (; ; ) { + while(true) { System.out.println("계산할 숫자를 입력해주세요"); System.out.println("-------------"); @@ -124,4 +124,3 @@ public void Lv_1() { } } } - diff --git a/src/Regular.java b/src/Regular.java index 5eb6bdd..b359629 100644 --- a/src/Regular.java +++ b/src/Regular.java @@ -1,4 +1,5 @@ import java.util.regex.Pattern; + import Four_Basic.*; public class Regular { @@ -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; } @@ -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(); }