diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index c901b92..84abe2f 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,17 +5,18 @@
-
-
-
-
-
+
+
+
+
+
+
@@ -65,12 +66,24 @@
"git-widget-placeholder": "junseok",
"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",
+ "last_opened_file_path": "C:/Users/ekaql/OneDrive/바탕 화면/개발/Kotlin & Spring/프로젝트/calculator_Ver_02",
"onboarding.tips.debug.path": "C:/Users/ekaql/OneDrive/바탕 화면/개발/팀 프로젝트/untitled/src/Main.java",
+ "project.structure.last.edited": "Modules",
+ "project.structure.proportion": "0.0",
+ "project.structure.side.proportion": "0.0",
"settings.editor.selected.configurable": "preferences.general",
"애플리케이션.Main.executor": "Run"
}
}]]>
+
+
+
+
+
+
+
+
+
diff --git a/src/Calculator.java b/src/Calculator.java
index 5e7921c..3922022 100644
--- a/src/Calculator.java
+++ b/src/Calculator.java
@@ -1,26 +1,27 @@
import Four_Basic.operationing;
public class Calculator {
+ //Getter & Setter을 담은 클레스
DTO DTO = new DTO();
+ //연산에 따른 연산 부모 클래스
private operationing operation;
+ //시랭을 위한 기본 연산자
public Calculator() {
}
- public Calculator(operationing o) {
- this.operation = o;
- }
-
+ //연산자를 통해 연산을 바꾸는 메소드
public void setOperation(operationing o) {
this.operation = o;
}
+ //DTO에 담긴 숫자를 가져와 연산
public int calculate() {
int number = 0;
number = operation.operation(DTO.getFirstNumber(), DTO.getSecondNumber());
- return number;
+ return number; // 연산한 값을 리턴
}
}
diff --git a/src/DTO.java b/src/DTO.java
index 73e0f5c..75e3e17 100644
--- a/src/DTO.java
+++ b/src/DTO.java
@@ -1,10 +1,11 @@
import org.w3c.dom.css.CSSImportRule;
public class DTO {
+ // 중요한 변수 캡슐화
private int firstNumber=0;
private int secondNumber=0;
-
+ // 캡슐화한 변수을 활용하기 위한 Getter / Setter
public int getFirstNumber(){
return firstNumber;
}
diff --git a/src/Four_Basic/addition.java b/src/Four_Basic/addition.java
index 7cfcdee..47a8316 100644
--- a/src/Four_Basic/addition.java
+++ b/src/Four_Basic/addition.java
@@ -1,6 +1,7 @@
package Four_Basic;
public class addition extends operationing{
+ //덧셈 연산을 위한 오버라이딩
@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber+secondNumber;
diff --git a/src/Four_Basic/division.java b/src/Four_Basic/division.java
index 1c00fb6..9cff38c 100644
--- a/src/Four_Basic/division.java
+++ b/src/Four_Basic/division.java
@@ -1,7 +1,7 @@
package Four_Basic;
public class division extends operationing {
-
+ //나눗셈 연산을 위한 오버라이딩
@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber / secondNumber;
diff --git a/src/Four_Basic/multiplication.java b/src/Four_Basic/multiplication.java
index 2f37e15..f3feaf5 100644
--- a/src/Four_Basic/multiplication.java
+++ b/src/Four_Basic/multiplication.java
@@ -1,7 +1,7 @@
package Four_Basic;
public class multiplication extends operationing {
-
+ //곱셈 연산을 위한 오버라이딩
@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber * secondNumber;
diff --git a/src/Four_Basic/operationing.java b/src/Four_Basic/operationing.java
index e001ae4..534a3f7 100644
--- a/src/Four_Basic/operationing.java
+++ b/src/Four_Basic/operationing.java
@@ -1,5 +1,6 @@
package Four_Basic;
public abstract class operationing {
+ // 다형성을 활용한 메서드 오버라이딩
public abstract int operation(int firstNumber, int secondNumber);
}
diff --git a/src/Four_Basic/subtraction.java b/src/Four_Basic/subtraction.java
index f4e1a4e..1b01a9c 100644
--- a/src/Four_Basic/subtraction.java
+++ b/src/Four_Basic/subtraction.java
@@ -1,6 +1,7 @@
package Four_Basic;
public class subtraction extends operationing {
+ //뺄셈 연산을 위한 오버라이딩
@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber - secondNumber;
diff --git a/src/InfoInput.java b/src/InfoInput.java
index 1bedf9d..f00af5b 100644
--- a/src/InfoInput.java
+++ b/src/InfoInput.java
@@ -2,25 +2,29 @@
public class InfoInput {
- public static boolean input() throws Exception {
+ public static void input() throws Exception {
+ //정규식 검사를 위한 객체 생성
Regular regular = new Regular();
- Scanner sc = new Scanner(System.in);
+ //콘솔로 입력 받기 위한 스케너 객체 생성
+ Scanner sc = new Scanner(System.in);
+ //첫번째 숫자 입력
System.out.println("숫자를 입력해주세요");
String FirstInput = sc.nextLine();
regular.CheckFirstNum(FirstInput);
+ //연산자 입력
System.out.println("연산자를 입력해주세요");
String operator = sc.nextLine();
regular.CheckSign(operator);
+ //두번째 숫자 입력
System.out.println("숫자를 입력해주세요");
String secondInput = sc.nextLine();
regular.CheckSecondNum(secondInput);
+ //연산 값 출력
System.out.println(regular.execut());
-
- return false;
}
}
diff --git a/src/InoutExceotion.java b/src/InoutExceotion.java
index 9592e35..b123ec9 100644
--- a/src/InoutExceotion.java
+++ b/src/InoutExceotion.java
@@ -1,5 +1,6 @@
public class InoutExceotion extends Exception{
public InoutExceotion(String word) {
+ // 예외 처리시 던져주는 word을 입력 받아 경고문 출력
super("==================\n"+word + "을 입력해주세요\n==================");
}
diff --git a/src/Main.java b/src/Main.java
index 50aabcc..d741d17 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -2,21 +2,25 @@
public class Main {
- public static void main(String[] args) {
- boolean End = false;
- while (!End) {
+ public static void main(String[] args) {
+ //무한 반복문
+ while (true) {
try {
- End = InfoInput.input();
+ //연산의 시작
+ InfoInput.input();
} catch (Exception e) {
+ //연산중 오류가 뜨면 예외처리된 메세지 출력
System.out.println(e.getMessage());
}
}
}
public void Lv_1() {
+ //입력을 위한 스케너 객체 인스턴스화
Scanner sc = new Scanner(System.in);
+ // 전역 변수들
String numberCheck = "";
int firstNumber = 0;
int secondNumber = 0;
@@ -24,10 +28,12 @@ public void Lv_1() {
String algorithm = "";
int answer = 0;
+ //실질적인 시스템의 운용
while(true) {
System.out.println("계산할 숫자를 입력해주세요");
System.out.println("-------------");
+ //첫번째 숫자를 입력받고 숫자인지 검사
System.out.println("숫자를 입력해주세요");
try {
firstNumber = sc.nextInt();
@@ -40,7 +46,9 @@ public void Lv_1() {
System.out.println("0보다 큰 수를 입력해세요");
break;
}
+ // 문자가 입력 받았을 시 예외 사항
} catch (InputMismatchException e) {
+ // 그중 "exit"을 입력 받으면 종료
if (sc.nextLine().equals("exit")) {
System.out.println("시스템을 종료합니다.");
break;
@@ -49,6 +57,7 @@ public void Lv_1() {
break;
}
+ //연산자에 따른 연산 입력과 연산자인지 검사
System.out.println("연산를 입력해주세요");
algorithmTest = sc.nextLine();
switch (algorithmTest) {
@@ -68,6 +77,7 @@ public void Lv_1() {
algorithm = algorithmTest;
break;
+ // 연산자가 아닌것을 검사하는 부분
default:
System.out.println("연산를 입력해주세요");
algorithm = sc.nextLine();
@@ -78,7 +88,7 @@ public void Lv_1() {
}
-
+ // 두번째 숫자를 입력받고 숫자인지 검사
System.out.println("숫자를 입력해주세요");
try {
secondNumber = sc.nextInt();
@@ -91,14 +101,17 @@ public void Lv_1() {
System.out.println("0보다 큰 수를 입력해세요");
}
+ // 문자가 입력 받았을 시 예외 사항
} catch (InputMismatchException e) {
- System.out.println("숫자만 입력해주세요");
+ // 그중 "exit"을 입력 받으면 종료
if (sc.nextLine().equals("exit")) {
System.out.println("시스템을 종료합니다.");
break;
}
+ System.out.println("숫자만 입력해주세요");
}
+ //연산자에 따른 연산
switch (algorithm) {
case "+":
answer = firstNumber + secondNumber;
@@ -117,6 +130,8 @@ public void Lv_1() {
break;
default:
}
+
+ //연산에 따른 값 출력
System.out.println("-----");
System.out.println("| " + answer + " |");
System.out.println("-----");
diff --git a/src/Regular.java b/src/Regular.java
index b359629..87ecec7 100644
--- a/src/Regular.java
+++ b/src/Regular.java
@@ -3,6 +3,7 @@
import Four_Basic.*;
public class Regular {
+ //숫자와 문자를 검사할 수 있게 미리 정한 정규식
private static final String operation = "[+\\-*/]";
private static final String number = "^[0-9]*$";
@@ -18,33 +19,34 @@ public Regular CheckFirstNum(String FirstNum) throws Exception {
return this;
}
-
+ //정규식을 통한 두번째 숫자 검사
public Regular CheckSecondNum(String SecondNum) throws Exception {
- ExitSign(SecondNum);
- if (!Pattern.matches(number, SecondNum)) {
- throw new InoutExceotion("정수");
+ ExitSign(SecondNum);// 종료 메서드
+ if (!Pattern.matches(number, SecondNum)) { // 보내진 문자열이 졍규식에 포함되있는지 확인
+ throw new InoutExceotion("정수"); // 예외처리시 입력할 타입
}
- this.calculator.DTO.setSecondNumber(Integer.parseInt(SecondNum));
+ this.calculator.DTO.setSecondNumber(Integer.parseInt(SecondNum)); // 검사가 통과된 숫자 setter로 저장
return this;
}
+ //정규식을 통한 연산자 검사
public Regular CheckSign(String operation) throws Exception {
- ExitSign(operation);
+ ExitSign(operation);// 종료 메서드
if (!Pattern.matches(this.operation, operation)) {
- throw new InoutExceotion("사칙 연산자");
+ 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;
}
@@ -52,13 +54,15 @@ public Regular CheckSign(String operation) throws Exception {
}
+ //종료 메서드
public void ExitSign(String sign) {
- if (sign.equals("exit")) {
+ if (sign.equals("exit")) { // exit문자열이 있는지 확인
System.out.println("시스템 종료");
- System.exit(0);
+ System.exit(0); // 시스템 종료메서드 0은 문제없이 종료한다는 의미
}
}
+ //출력 메서드
public int execut() {
return calculator.calculate();
}