์ค๋์ Step3(๋์ ๊ณผ์ )๋ฅผ ์งํํด๋ณด์๋ค.
OperatorType.java ๊ตฌํ ๊ณผ์
Step2๊น์ง์ ๊ณ์ฐ๊ธฐ์์๋
์ฐ์ฐ์๋ฅผ char ํ์
(+, -, *, /)์ผ๋ก ์
๋ ฅ๋ฐ์
switch ๋ฌธ์ผ๋ก ๋ถ๊ธฐ ์ฒ๋ฆฌํ๋ค.
switch (operator) {
case '+': ...
case '-': ...
}
์ด ๋ฐฉ์์ ๋์์๋ ๋ฌธ์ ๊ฐ ์์ง๋ง
์ฐ์ฐ์ ์ข
๋ฅ๊ฐ ๋์ด๋๋ฉด ๊ด๋ฆฌ๊ฐ ์ด๋ ค์์ง๊ณ ,
์๋ชป๋ ์ฐ์ฐ์ ์
๋ ฅ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ๋ถ์ฐ๋๋ฉฐ
ํ๋์ ๊ฐ๋
์ผ๋ก ๊ด๋ฆฌํ๋ค๋ ๋๋์ด ๋ถ์กฑํ๋ค๊ณ ์๊ฐํ๋ค.
๊ทธ๋์ Step3์์๋
Enum์ ํ์ฉํด ์ฐ์ฐ์ ํ์
์ ๊ด๋ฆฌํ๋๋ก ์๊ตฌํ๊ณ ์๋ค.
OperatorType enum ์ค๊ณ
์ฌ์น์ฐ์ฐ์ ๊ณ ์ ๋ ๊ฐ์ ๊ฐ์ง๋ฏ๋ก
Enum์ ์ฌ์ฉํ๊ธฐ์ ์ ํฉํ๋ค๊ณ ํ๋จํ๋ค.
package com.example.calculator.step3;
public enum OperatorType {
ADD('+'),
SUB('-'),
MUL('*'),
DIV('/');
private final char symbol;
OperatorType(char symbol) {
this.symbol = symbol;
}
public char getSymbol() {
return symbol;
}
public static OperatorType fromSymbol(char symbol) {
for (OperatorType type : values()) {
if (type.symbol == symbol) return type;
}
throw new IllegalArgumentException("์ง์ํ์ง ์๋ ์ฐ์ฐ์์
๋๋ค.");
}
}
enum ์์ ์ ์
ADD('+'),
SUB('-'),
MUL('*'),
DIV('/');
- ์ฌ์น์ฐ์ฐ์ ์์ด ์ด๋ฆ์ผ๋ก ์ ์ํ๋ค.
- ์ฐ์ฐ์๋ ์ค์ ์ ๋ ฅ๋๋ ๊ธฐํธ๋ฅผ ํจ๊ป ๊ฐ์ง๋ค.
ํ๋์ ์์ฑ์
private final char symbol;
OperatorType(char symbol) {
this.symbol = symbol;
}
- enum๋ ํด๋์ค์ด๊ธฐ ๋๋ฌธ์ ํ๋์ ์์ฑ์๋ฅผ ๊ฐ์ง ์ ์๋ค.
- ๊ฐ ์ฐ์ฐ ํ์ ์ ์์ ์ด ์ด๋ค ๊ธฐํธ์ ์ฐ๊ฒฐ๋์ด ์๋์ง ์ ์ ์๋ค.
fromSymbol ๋ฉ์๋
public static OperatorType fromSymbol(char symbol) {
for (OperatorType type : values()) {
if (type.symbol == symbol) return type;
}
throw new IllegalArgumentException("์ง์ํ์ง ์๋ ์ฐ์ฐ์์
๋๋ค.");
}
- ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ฐ์ฐ์๋ฅผ ๋์๋๋ OperatorType์ผ๋ก ๋ณํํ๋ ์ญํ ์ ํ๋ค.
์
๋ ฅ์ char์ง๋ง
๋ด๋ถ ๊ณ์ฐ ๋ก์ง์ enum ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๊ฒ ๋ง๋ ๋ค.
์ด ๊ตฌ์กฐ ๋๋ถ์
์๋ชป๋ ์ฐ์ฐ์ ์
๋ ฅ์ ๋ํ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ ๊ณณ์์ ๊ด๋ฆฌํ ์ ์๊ณ ,
๊ณ์ฐ ๋ก์ง์์๋ enum๋ง ์ ๊ฒฝ ์ฐ๋ฉด ๋๋ค.
๊ธฐ์กด Step2์ calculate ๋ฉ์๋๋ ๋ค์๊ณผ ๊ฐ์ ํํ์๋ค.
calculate(int num1, int num2, char operator)
Step3์์๋ ๋ค์๊ณผ ๊ฐ์ ํ๋ฆ์ผ๋ก ๋ณ๊ฒฝํ๋ค.
- ์ฐ์ฐ์๋ฅผ char๋ก ์ ๋ ฅ๋ฐ๊ณ
- OperatorType.fromSymbol()๋ก enum์ผ๋ก ๋ณํํ ๋ค
- ๊ณ์ฐ๊ธฐ ํด๋์ค์ enum์ ์ ๋ฌํ๋ค.
Enum ์ ์ฉ์ ์ฅ์
- ์ฐ์ฐ์ ํ์ ์ ํ๋์ ๊ฐ๋ ์ผ๋ก ๋ฌถ์ด ๊ด๋ฆฌํ ์ ์๋ค.
- switch ๋ถ๊ธฐ์ ๊ธฐ์ค์ด ๋ช ํํด์ง๋ค.
- ์๋ชป๋ ์ฐ์ฐ์ ์์ธ ์ฒ๋ฆฌ๊ฐ ๊น๋ํด์ง๋ค.
- ์ดํ ์ฐ์ฐ์ด ์ถ๊ฐ๋๋๋ผ๋ ํ์ฅ์ฑ์ด ์ข์์ง๋ค.
ํธ๋ฌ๋ธ์ํ
OperatorType.java๋ฅผ ์ปค๋ฐํ ๋ค
git push origin main์ ์คํํ์ง๋ง ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
ํด๊ฒฐ์ ์๋ํ๊ธฐ ์ํด
git pull origin main์ ์คํํ์ง๋ง
์ด ์ญ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.

ํด๊ฒฐ ๋ฐฉ๋ฒ: rebase ์ฌ์ฉ

rebase ๊ณผ์ ์์ ์ถฉ๋์ ๋ฐ์ํ์ง ์์๊ณ ,
์ ์์ ์ผ๋ก push๊น์ง ์๋ฃํ ์ ์์๋ค.
Calculator.java ๊ตฌํ
calculator.java๋ฅผ ์์ ํ์ฌ
Enum ๊ธฐ๋ฐ ์ฐ์ฐ, ์ ๋ค๋ฆญ์ผ๋ก ๋ค์ํ ์ซ์ ํ์
์ฒ๋ฆฌ,
Stream / Lambda๋ฅผ ํ์ฉํ ์กฐํ ๊ธฐ๋ฅ๊น์ง ๊ตฌํํ๋ค.
Step2์ Step3 ๋น๊ต
Step2
- calculate(int, int, char)
- ๊ฒฐ๊ณผ ์ ์ฅ: List<Integer>
Step3
- ์ฐ์ฐ์: OperatorType(Enum)
- ์ซ์ ํ์ ํ์ฅ: <T extends Number>
- ๊ฒฐ๊ณผ ์กฐ๊ฑด ์กฐํ: Stream, Lambda ์ฌ์ฉ
๊ฒฐ๊ณผ ์ ์ฅ ์ปฌ๋ ์
private final List<Double> results = new ArrayList<>();
- Step2์์๋ List<Integer>์์ง๋ง Step3์์๋ Double๋ก ๋ณ๊ฒฝํ๋ค.
- final์ ์ฌ์ฉํด ๋ฆฌ์คํธ ์์ฒด๋ ๋ณ๊ฒฝํ์ง ๋ชปํ๋๋ก ํ๋ค.
calculate ๋ฉ์๋ (์ ๋ค๋ฆญ)
public <T extends Number> double calculate(T num1, T num2, OperatorType operator)
- T extends Number๋ก ์ค์ ํด ๋ค์ํ ์ซ์ ํ์ ์ ๋ฐ์ ์ ์๋ค.
doubleValue()
double a = num1.doubleValue();
double b = num2.doubleValue();
- Number๊ฐ ์ ๊ณตํ๋ doubleValue()๋ฅผ ํตํด ๊ณ์ฐ ๊ฐ๋ฅํ ์ํ๋ก ๋ณํํ๋ค.
- ์ ๋ค๋ฆญ ํ์ T๋ ์ง์ ์ฐ์ฐ์ด ๋ถ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ ๋ณํ์ด ํ์ํ๋ค.
Enum ๊ธฐ๋ฐ ์ฐ์ฐ
switch (operator) {
case ADD: ...
}
- Step2์์๋ char ๊ธฐ์ค์ด์์ง๋ง
- Step3์์๋ OperatorType(enum)์ ๊ธฐ์ค์ผ๋ก ๋ถ๊ธฐํ๋ค.
์์ธ ์ฒ๋ฆฌ
if (b == 0) {
throw new IllegalArgumentException("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.");
}
- 0์ผ๋ก ๋๋๋ ์ํฉ์ ๋ฐฉ์งํ๋ค.
- ์์ธ๋ App์์ ์ก์ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋๋ก ๊ตฌ์ฑํ๋ค.
๊ฒฐ๊ณผ ์ ์ฅ
results.add(result);
- Step2์ ๋์ผํ๊ฒ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ ๋ฆฌ์คํธ์ ์ ์ฅํ๋ค.
Stream / Lambda ์กฐํ
return results.stream()
.filter(r -> r > value)
.collect(Collectors.toList());
- stream()์ผ๋ก ๋ฆฌ์คํธ๋ฅผ ํ๋ฆ์ผ๋ก ๋ณํ
- filter๋ก ์กฐ๊ฑด์ ๋ง๋ ๊ฐ๋ง ์ถ์ถ
- collect๋ก ๋ค์ ๋ฆฌ์คํธ๋ก ๋ฐํ
Calculator.java ์ ์ฒด ์ฝ๋
package com.example.calculator.step3;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Calculator {
private final List<Double> results = new ArrayList<>();
public <T extends Number> double calculate(T num1, T num2, OperatorType operator) {
double result = 0;
double a = num1.doubleValue();
double b = num2.doubleValue();
switch (operator) {
case ADD:
result = a + b;
break;
case SUB:
result = a - b;
break;
case MUL:
result = a * b;
break;
case DIV:
if (b == 0) {
throw new IllegalArgumentException("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.");
}
result = a / b;
break;
default:
throw new IllegalArgumentException("์ง์ํ์ง ์๋ ์ฐ์ฐ์์
๋๋ค.");
}
results.add(result);
return result;
}
public List<Double> getResults() {
return new ArrayList<>(results);
}
public void setResults(List<Double> newResults) {
results.clear();
results.addAll(newResults);
}
public void removeResult() {
if (!results.isEmpty()) {
results.remove(0);
}
}
public List<Double> getResultsGreaterThan(double value) {
return results.stream()
.filter(r -> r > value)
.collect(Collectors.toList());
}
}
App.java ๊ตฌํ ๊ณผ์
calculator ์ธ์คํด์ค ์์ฑ
Calculator calculator = new Calculator();
- App์ ์ ๋ ฅ๊ณผ ์ถ๋ ฅ๋ง ๋ด๋นํ๋ค.
๋ฐ๋ณต ์คํ ๊ตฌ์กฐ
while (true) { ... }
exit ์ ๋ ฅ ์ ๊น์ง ๊ณ์ฐ์ด ๋ฐ๋ณต๋๋ค.
OperatorType ๋ณํ
OperatorType opType = OperatorType.fromSymbol(operator);
- ์ฌ์ฉ์ ์ ๋ ฅ(char)์ enum์ผ๋ก ๋ณํํด ๋ด๋ถ ๋ก์ง์ ํต์ผํ๋ค.
calculate ํธ์ถ (์ ๋ค๋ฆญ)
double result = calculator.calculate(num1, num2, opType);
- int๋ฟ ์๋๋ผ double ํ์ ๋ ์ฒ๋ฆฌํ ์ ์๋ค.
์์ธ ์ฒ๋ฆฌ ํธ๋ฌ๋ธ์ํ

์ค๋ฅ(0์ผ๋ก ๋๋๊ธฐ, ์๋ชป๋ ์ฐ์ฐ์)๊ฐ ๋ฐ์ํด๋
ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ๋์ง ์๊ณ
๊ธฐ์ค๊ฐ์ ๊ณ์ ๋ฌผ์ด๋ณด๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
catch (IllegalArgumentException e) {
System.out.println("์ค๋ฅ: " + e.getMessage());
continue;
}
- catch์์ continue๋ฅผ ์ฌ์ฉํด
์ค๋ฅ ๋ฐ์ ์ ๊ธฐ์ค๊ฐ ์ ๋ ฅ ๋ก์ง์ด ์คํ๋์ง ์๋๋ก ์์ ํ๋ค.

๊ธฐ์ค๊ฐ ์กฐํ (๋๋ค, ์คํธ๋ฆผ)
System.out.println(calculator.getResultsGreaterThan(value));
App.java ์ ์ฒด ์ฝ๋
package com.example.calculator.step3;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Calculator calculator = new Calculator();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("์ฒซ ๋ฒ์งธ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์:");
int num1 = sc.nextInt();
System.out.print("๋ ๋ฒ์งธ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์:");
int num2 = sc.nextInt();
System.out.print("์ฌ์น์ฐ์ฐ ๊ธฐํธ๋ฅผ ์
๋ ฅํ์ธ์: ");
char operator = sc.next().charAt(0);
try {
OperatorType opType = OperatorType.fromSymbol(operator);
double result = calculator.calculate(num1, num2, opType);
System.out.println("๊ฒฐ๊ณผ: " + result);
} catch (IllegalArgumentException e) {
System.out.println("์ค๋ฅ: " + e.getMessage());
continue;
}
System.out.println("์ ์ฅ๋ ๊ฒฐ๊ณผ ๋ชฉ๋ก: " + calculator.getResults());
System.out.print("๊ธฐ์ค๊ฐ์ ์
๋ ฅํ์ธ์: ");
double value = sc.nextDouble();
System.out.println("๊ธฐ์ค๊ฐ๋ณด๋ค ํฐ ๊ฒฐ๊ณผ: " + calculator.getResultsGreaterThan(value));
System.out.println("๊ฐ์ฅ ๋จผ์ ์ ์ฅ๋ ๊ฒฐ๊ณผ๋ฅผ ์ญ์ ํ์๊ฒ ์ต๋๊น? (remove ์
๋ ฅ ์ ์ญ์ )");
String cmd = sc.next();
if (cmd.equalsIgnoreCase("remove")) {
calculator.removeResult();
System.out.println("์ญ์ ํ ๊ฒฐ๊ณผ ๋ชฉ๋ก: " + calculator.getResults());
}
System.out.println("๋ ๊ณ์ฐํ์๊ฒ ์ต๋๊น? (exit ์
๋ ฅ ์ ์ข
๋ฃ)");
String answer = sc.next();
if (answer.equals("exit")) break;
}
sc.close();
}
}

ํ๋ก์ฐ์ฐจํธ
๋ฉฐ์น ์ ์ ํ๋ก์ฐ์ฐจํธ๋ฅผ ๋ฐฐ์ฐ๋ฉด์ ๊ฐ๋จํ ์ค์ต์ ์งํํ๋๋ฐ,
์ด๋ฒ ๊ณ์ฐ๊ธฐ ๊ณผ์ ๋ฅผ ์ ๋ฆฌํ๋ ๋ฐ๋ ํ๋ก์ฐ์ฐจํธ๋ฅผ ํ์ฉํด๋ณด์๋ค.

๊ณผ์ ์ดํ (ํ๊ณ )
Step1, Step2๊น์ง๋ ๋น๊ต์ ์์ํ๊ฒ ์งํํ์ง๋ง
Step3๋ถํฐ ๋์ด๋๊ฐ ํ ์ฌ๋ผ๊ฐ๋ค๊ณ ๋๊ผ๋ค.
Enum, ์ ๋ค๋ฆญ, ์คํธ๋ฆผ, ๋๋ค ๋ชจ๋
๊ฐ์์์ ๋ถ๋ช
ํ ๋ค์ ๋ด์ฉ์ด์์ง๋ง
๋ง์ ์ ์ฉํ๋ ค๋ ์ด๋์, ์ด๋ค ๋ฐฉ์์ผ๋ก ์จ์ผ ํ ์ง ๊ณ์ ๊ณ ๋ฏผํ๊ฒ ๋์๋ค.
ํนํ ์ ๋ค๋ฆญ ์ ์ฉ ๊ณผ์ ์์
int → double๋ก ๋จ์ ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์๋๋ผ
<T extends Number>๋ก ํ์ฅํ๋ ๋ถ๋ถ์ด ๊ฐ์ฅ ์ด๋ ค์ ๋ค.
์ฒ์์๋ ๊ดํธ ์ฐ์ ์์, ์ ๊ณฑ, ์ ๊ณฑ๊ทผ ๊ธฐ๋ฅ๊น์ง ํ์ฅํด๋ณด๋ ค ํ์ง๋ง
์์ ๋ฌธ์์ด์ ํ ํฐํํ๊ณ ์ฐ์ฐ ์ฐ์ ์์๋ฅผ ์ฒ๋ฆฌํ๋ ๋ก์ง์ด
์๊ฐ๋ณด๋ค ๋์ด๋๊ฐ ๋์ ์ด๋ฒ ๊ณผ์ ์์๋ ๊ตฌํํ์ง ๋ชปํ๋ค.
๋์ Step3 ์์ฑ์ ์ง์คํ๊ณ ,
๋ค์์๋ ์ ๊ธฐ๋ฅ๋ค์ ๊ตฌํํ๊ณ
GUI ๊ธฐ๋ฐ ๊ณ์ฐ๊ธฐ๋ก๋ ํ์ฅํด๋ณด๊ณ ์ถ๋ค๋ ๋ชฉํ๊ฐ ์๊ฒผ๋ค.
๊ณผ์ ๋ฅผ ์์ํ ๋๋ AI๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ํด๊ฒฐํด๋ณด๋ ค๊ณ ํ์ง๋ง
๋งํ๋ ๋ถ๋ถ์ด ๋ง์ ๊ฒฐ๊ตญ ๋์์ ๋ฐ๊ฒ ๋์๋ค.
๋ค๋ง ์ฝ๋๋ฅผ ๊ทธ๋๋ก ์์ฒญํ๊ธฐ๋ณด๋ค๋
๊ฐ๋
์ด๋ ํด๊ฒฐ ๋ฐฉํฅ์ ๋ํ ํํธ๋ฅผ ์ป๋ ์ฉ๋๋ก ์ฌ์ฉํ๊ณ ,
๊ทธ ์ดํ์๋ ์ง์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์์ ํ๋ฉฐ ์ดํดํ๋ ค๊ณ ๋
ธ๋ ฅํ๋ค.
์ด๋ฒ ๊ณ์ฐ๊ธฐ ๊ณผ์ ๋ฅผ ํตํด
์๋ฐ ๋ฌธ๋ฒ์ ๋จ์ํ ์๋ ๊ฒ๊ณผ
์ค์ ๋ก ์ฌ์ฉํ๋ ๊ฒ์ ์ฐจ์ด๋ฅผ ํ์คํ ๋๋ ์ ์์๋ค.
์์ง ์์ ํ ์ต์ํ๋ค๊ณ ๋ ๋งํ ์ ์์ง๋ง,
์ ์ด๋ ๊ฒ ์์ฑํ๋์ง๋ ์ค๋ช
ํ ์ ์๋ ์ํ๊ฐ ๋์๋ค๋ ์ ์์
์๋ฏธ ์๋ ๊ณผ์ ์๋ค๊ณ ์๊ฐํ๋ค.
'๐ฅ ๋ด์ผ๋ฐฐ์์บ ํ > ๐ ๊ณผ์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [๋ด์ผ๋ฐฐ์์บ ํ Spring_3๊ธฐ] CH2 ์ปค๋จธ์ค ๊ณผ์ Step2 (0) | 2026.01.28 |
|---|---|
| [๋ด์ผ๋ฐฐ์์บ ํ Spring_3๊ธฐ] CH2 ์ปค๋จธ์ค ๊ณผ์ Step1 (0) | 2026.01.28 |
| [๋ด์ผ๋ฐฐ์์บ ํ Spring_3๊ธฐ] CH2 ๊ณ์ฐ๊ธฐ ๊ณผ์ Step2 (0) | 2026.01.27 |
| [๋ด์ผ๋ฐฐ์์บ ํ Spring_3๊ธฐ] CH2 ๊ณ์ฐ๊ธฐ ๊ณผ์ Step1 (0) | 2026.01.27 |
| [๋ด์ผ๋ฐฐ์์บ ํ Spring_3๊ธฐ] CH1 ๋ง์ง ๊ด๋ฆฌ API ์ค๊ณํ๊ธฐ (0) | 2026.01.27 |