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
25 changes: 19 additions & 6 deletions .idea/workspace.xml

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

11 changes: 6 additions & 5 deletions src/Calculator.java
Original file line number Diff line number Diff line change
@@ -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; // 연산한 값을 리턴
}

}
3 changes: 2 additions & 1 deletion src/DTO.java
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/Four_Basic/addition.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Four_Basic;

public class addition extends operationing{
//덧셈 연산을 위한 오버라이딩
@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber+secondNumber;
Expand Down
2 changes: 1 addition & 1 deletion src/Four_Basic/division.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Four_Basic;

public class division extends operationing {

//나눗셈 연산을 위한 오버라이딩
@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber / secondNumber;
Expand Down
2 changes: 1 addition & 1 deletion src/Four_Basic/multiplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package Four_Basic;

public class multiplication extends operationing {

//곱셈 연산을 위한 오버라이딩
@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber * secondNumber;
Expand Down
1 change: 1 addition & 0 deletions src/Four_Basic/operationing.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Four_Basic;

public abstract class operationing {
// 다형성을 활용한 메서드 오버라이딩
public abstract int operation(int firstNumber, int secondNumber);
}
1 change: 1 addition & 0 deletions src/Four_Basic/subtraction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Four_Basic;

public class subtraction extends operationing {
//뺄셈 연산을 위한 오버라이딩
@Override
public int operation(int firstNumber, int secondNumber) {
return firstNumber - secondNumber;
Expand Down
12 changes: 8 additions & 4 deletions src/InfoInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions 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) {
// 예외 처리시 던져주는 word을 입력 받아 경고문 출력
super("==================\n"+word + "을 입력해주세요\n==================");

}
Expand Down
27 changes: 21 additions & 6 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@

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;
String algorithmTest = "";
String algorithm = "";
int answer = 0;

//실질적인 시스템의 운용
while(true) {
System.out.println("계산할 숫자를 입력해주세요");
System.out.println("-------------");

//첫번째 숫자를 입력받고 숫자인지 검사
System.out.println("숫자를 입력해주세요");
try {
firstNumber = sc.nextInt();
Expand All @@ -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;
Expand All @@ -49,6 +57,7 @@ public void Lv_1() {
break;
}

//연산자에 따른 연산 입력과 연산자인지 검사
System.out.println("연산를 입력해주세요");
algorithmTest = sc.nextLine();
switch (algorithmTest) {
Expand All @@ -68,6 +77,7 @@ public void Lv_1() {
algorithm = algorithmTest;
break;

// 연산자가 아닌것을 검사하는 부분
default:
System.out.println("연산를 입력해주세요");
algorithm = sc.nextLine();
Expand All @@ -78,7 +88,7 @@ public void Lv_1() {

}


// 두번째 숫자를 입력받고 숫자인지 검사
System.out.println("숫자를 입력해주세요");
try {
secondNumber = sc.nextInt();
Expand All @@ -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;
Expand All @@ -117,6 +130,8 @@ public void Lv_1() {
break;
default:
}

//연산에 따른 값 출력
System.out.println("-----");
System.out.println("| " + answer + " |");
System.out.println("-----");
Expand Down
32 changes: 18 additions & 14 deletions src/Regular.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Four_Basic.*;

public class Regular {
//숫자와 문자를 검사할 수 있게 미리 정한 정규식
private static final String operation = "[+\\-*/]";
private static final String number = "^[0-9]*$";

Expand All @@ -18,47 +19,50 @@ 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;
}
return this;

}

//종료 메서드
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();
}
Expand Down