์ด๋ฒ์๋ Java Enum์ ํ์ฉ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด๊ณ ์ ํ๋ค.
Java Enum์ ํ์ฉ ํ๋ ๊ณผ์ ์ ์์ ๊ตฌํ ์ฝ๋๋ฅผ ํตํด ์ค๋ช ํด๋ณด์ ํ๋ค.
๊ตฌํ ๋ชฉํ : ๊ณ์ฐ๊ธฐ
๊ตฌํ ๊ธฐ๋ฅ : ๋ง์
, ๋บ์
, ๊ณฑ์
, ๋๋์
์ผ๋จ Enum์ ์ฌ์ฉํ๋ค๊ธฐ ๋ณด๋ค ๊ตฌํ์๋ง ์ง์คํ์ฌ ์์ฑ์ ํด๋ณด์
public class CalculatorMachine {
private static final String PLUS = "+";
private static final String MINUS = "-";
private static final String MULTIPLE = "*";
private static final String DIVIDE = "/";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int result = 0;
int firstValue = scanner.nextInt();
String calculator = scanner.next();
int secondValue = scanner.nextInt();
if (PLUS.equals(calculator)) {
result = firstValue + secondValue;
} else if (MINUS.equals(calculator)) {
result = firstValue - secondValue;
} else if (MULTIPLE.equals(calculator)) {
result = firstValue * secondValue;
} else if (DIVIDE.equals(calculator)) {
result = firstValue / secondValue;
}
System.out.println(result);
}
}
scanner๋ฅผ ํตํด ์ฐ์ฐ์์ ์ฐ์ฐ์ ํ ๋๊ฐ์ง intํ ๊ฐ์ ์
๋ ฅ ๋ฐ๊ณ ์ฐ์ฐ์์ ์ข
๋ฅ์ ๋ฐ๋ผ
if-else๋ฅผ ํตํด ๊ตฌ๋ถํ์ฌ ์ฐ์ฐ ํ๋๋ก ๊ตฌํํ๋ค.
์์ธ ์ฒ๋ฆฌ๊ฐ ์ ๊ฒฝ ์ฐ์ด์ง๋ง ์ผ๋จ ๋์ด๊ฐ์
์ด์ Enum์ ์์ฑํ์ฌ ๋ฆฌํฉํ ๋ง์ ํด๋ณด์
public class CalculatorMachine {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double firstValue = scanner.nextDouble();
String operator = scanner.next();
double secondValue = scanner.nextDouble();
CalculatorSymbol operator1 = CalculatorSymbol.findOperator(operator);
Calculator calculator = new Calculator(firstValue, operator1.getSymbol(),secondValue);
System.out.println(calculator.CalculateValue());
}
}
public class Calculator {
public double firstValue;
private final CalculatorSymbol symbol;
private final double secondValue;
public Calculator(double firstValue, String operator, double secondValue) {
this.firstValue = firstValue;
this.symbol = CalculatorSymbol.findOperator(operator);
this.secondValue = secondValue;
}
public double CalculateValue() {
if (CalculatorSymbol.PLUS.getSymbol().equals(symbol.getSymbol())) {
return firstValue + secondValue;
} else if (CalculatorSymbol.MINUS.getSymbol().equals(symbol.getSymbol())) {
return firstValue - secondValue;
} else if (CalculatorSymbol.MULTIPLE.getSymbol().equals(symbol.getSymbol())) {
return firstValue * secondValue;
} else if (CalculatorSymbol.DIVIDE.getSymbol().equals(symbol.getSymbol())) {
return firstValue / secondValue;
}
return 0;
}
}
public enum CalculatorSymbol {
PLUS("+"),
MINUS("-"),
MULTIPLE("*"),
DIVIDE("/");
private final String value;
CalculatorSymbol(String k) {
this.value = k;
}
public String getSymbol() {
return this.value;
}
public static CalculatorSymbol findOperator(String operator) {
return Arrays.stream(CalculatorSymbol.values())
.filter(symbol ->symbol.value.equals(operator))
.findFirst()
.orElseThrow(NullPointerException::new);
}
}
์ง๋๋ฒ ์ค๋ช
ํ๋ ๊ฒ์ฒ๋ผ ๊ธฐ๋ณธ์ ์ธ Enum ์ฌ์ฉ๋ฒ์ ํตํด ๊ตฌํํ๋ค.
Calculator๋ฅผ ์์ฑ์์์ ์
๋ ฅ๋ฐ์ ์ฐ์ฐ์์ ๋ง๋ CalculatorSymbol Enum์ ์์ฑํ๊ณ , ์ด๋ฅผ ๋ฐ๋ผ ์ฐ์ฐ์๋ฅผ ๊ตฌ๋ณํ๋ ๋ก์ง์ ๊ตฌํํ์๋ค.
๊ทธ๋ฆฌ๊ณ ์ฒ์ ๊ตฌํ ํ๋ ์ฝ๋์์ Calculator ์ญํ ์ ํ๋ ์ฝ๋๋ฅผ ๋ฐ๋ก class๋ก ๊ตฌ๋ถํ์ฌ ๊ตฌํํ๋ค.
main๋ฉ์๋ ์์๋ ๊ฐ๋จํ๊ฒ ๋ณด์ด์ง๋ง Calculator class๋ฅผ ๋ณด๋ฉด ์์ง ์กฐ๊ฑด ๋ถ๊ธฐ ๋ก์ง์ด ์ ๊ฒฝ ์ฐ์ธ๋ค.
๋๋ถ์ด CalculatorMachine ํด๋์ค์์ Enumํด๋์ค๊ฐ ๋ณด์ด๋๊ฒ์ด ๋ถํธํด๋ณด์ธ๋ค.
CalculatorMachine์ ๊ณ์ฐ๊ธฐ ํด๋์ค ์ธ๋ฐ ๊ณผ์ฐ ์ฌ๊ธฐ์ CalculatorSymbol Enum ํด๋์ค์์ ์ฐ์ฐ์๋ฅผ ์ฐพ๋ findOperator ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ์ฝ๋๊ฐ ๋ณด์ฌ์ง๋๊ฒ ๋ง๋๊ฑธ๊น? ๋ผ๋ ์์ฌ๋ ๋ ๋ค.
์ด๋ฅผ ์ข ๋ ๊ฐ๋จํ๊ฒ ํํ ํ ์ ์์๊น ??
CalculatorMachine์ Enumํด๋์ค๋ฅผ ์๋ณด์ด๊ฒ ํ ์ ์์๊น?
๋์ค์ ๋ค๋ฃจ๊ฒ ์ง๋ง Enum์ ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ํตํด Enum์ ๋ ํจ๊ณผ์ ์ผ๋ก ํจ์จ์ ์ผ๋ก ์ฌ์ฉ ํ ์ ์๋ค.
๋ค์์ ๋ณด์
import java.util.Scanner;
public class CalculatorMachine {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double firstValue = scanner.nextDouble();
String operator = scanner.next();
double secondValue = scanner.nextDouble();
Calculator calculator = new Calculator(firstValue, operator,secondValue);
System.out.println(calculator.calculateValue());
}
}
public class Calculator {
public double firstValue;
private final CalculatorSymbol symbol;
private final double secondValue;
public Calculator(double firstValue, String operator, double secondValue) {
this.firstValue = firstValue;
this.symbol = CalculatorSymbol.findOperator(operator);
this.secondValue = secondValue;
}
public double calculateValue() {
CalculatorSymbol operator = CalculatorSymbol.findOperator(symbol.getSymbol());
return operator.calculate(firstValue, secondValue);
}
}
public enum CalculatorSymbol {
PLUS("+", (a, b) -> a + b),
MINUS("-",(a, b) -> a - b),
MULTIPLE("*",(a, b) -> a * b),
DIVIDE("/",(a, b) -> a / b);
private final String value;
private final BiFunction<Double, Double, Double> calculateValue;
CalculatorSymbol(String symbol, BiFunction<Double, Double, Double> calculateValue) {
this.value = symbol;
this.calculateValue = calculateValue;
}
public String getSymbol() {
return this.value;
}
public Double calculate(double firstValue, double secondValue) {
return this.calculateValue.apply(firstValue,secondValue);
}
public static CalculatorSymbol findOperator(String operator) {
return Arrays.stream(CalculatorSymbol.values())
.filter(symbol ->symbol.value.equals(operator))
.findFirst()
.orElseThrow(NullPointerException::new);
}
}
ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ฒ ์ ๊ฒฝ ์ฐ์ด์ง๋ง ๊ทธ๋ถ๋ถ์ ์ ์ธํ๊ณ ์ด์ธ์ ์ฝ๋๋ฅผ ๋ณด์!
์ผ๋จ Enum์์ ๋ณ๊ฒฝ๋ ๋ชจ์ต์ ๋ณด๋ฉด PLUS("+", (a,b) -> a+b)๋ฅผ ๋ณด๋ฉด PLUS๋ฅผ ํ๋ฉด ๋ฌด์ธ๊ฐ ๋ parameter๋ฅผ ๋ฐ์ ๋ํ๋ ์ฐ์ฐ์ ํ๋๊ฑฐ ๊ฐ๋ค.
๊ทธ๋ฆฌ๊ณ ๊ทธ ์ฐ์ฐ์ public Double calculator ๋ฉ์๋๊ฐ ํด์ฃผ๋๊ฑฐ ๊ฐ๋ค
์ด๋ ๊ฒ ๋๋ ๊ทธ์ ์๋ Calculator ํน์ CalculatorMachineํด๋์ค์์ ์ด๋ฃจ์ด์ง ์ฐ์ฐ์ด Enum์์ ์ด๋ฃจ์ด์ง๋๊ฒ์ ํ์ธ ํ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ Calculator ํด๋์ค์ CalculatorMachineํด๋์ค์ ์ฝ๋๊ฐ ๋ ๊ฐ๋จํด์ง๊ฒ์ ํ์ธ ํ ์ ์๋ค.
์ด์ฒ๋ผ Enum๊ณผ ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ํ์ฉ ํ๋ฉด class์ ์ญํ ์ ๋ง๊ฒ ๊ตฌํ์ด ๋๊ณ ์ฝ๋ ๋ํ ๋ ๊ฐ๊ฒฐํ๊ฒ ํํ ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
๊ฐ๋จํ ๊ตฌํ ๊ฐ์ ๊ฒฝ์ฐ ๋ฐ๋ก Enum์ ์ฌ์ฉํ์ฌ ๊ตฌํํ ์ ๋ ์๊ฒ ์ง๋ง, ์ฐ์ต์ ํ ๋ ๊ธฐ๋ฅ์๋ง ์ง์คํ์ฌ ๋น ๋ฅด๊ฒ ๊ตฌํํ๊ณ ํ๋์ฉ ๋ฆฌํฉํ ๋ง ํด๋ณธ๋ค๋ฉด Enum์ ์ฌ์ฉํ๋ ์ด์ , ์ฅ์ , ํ์ฉ ๋ฐฉ์์ ๋ ํ์คํ๊ฒ ์ดํด ํ ์ ์์๊ฑฐ๋ผ ์๊ฐํ๋ค.