Skip to content

Latest commit

ย 

History

History
186 lines (159 loc) ยท 7.64 KB

2020-11-26-Java-Enum-Deepening.md

File metadata and controls

186 lines (159 loc) ยท 7.64 KB

Java Enum ํ™œ์šฉ

์ด๋ฒˆ์—๋Š” 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์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ , ์žฅ์ , ํ™œ์šฉ ๋ฐฉ์•ˆ์„ ๋” ํ™•์‹คํ•˜๊ฒŒ ์ดํ•ด ํ•  ์ˆ˜ ์žˆ์„๊ฑฐ๋ผ ์ƒ๊ฐํ•œ๋‹ค.