Java로 간단한 계산기 프로그램 구현

song yuheon·2023년 8월 27일
0

Java

목록 보기
21/46
post-thumbnail
post-custom-banner

계산기 구현

Step1 ~ Step2:
Calculator 클래스 내에서 switch 문을 사용하여 사칙연산 구현.

Step3:
각 연산(AddOperation, SubstractOperation 등) 별도 클래스로 분리. Calculator 클래스는 해당 연산 클래스들을 주입 받아 사용.

Step4:
Calculator 클래스는 단일 AbstractOperation 객체만을 주입 받아 사용. 연산 변경을 위해 setter 메서드 사용.


Step 별 구현 코드

Step1 ~ Step2

Main.java
public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        System.out.println(calculator.calculate("*",13,20));
        System.out.println(calculator.calculate("/",13,20));
        System.out.println(calculator.calculate("-",13,20));
        System.out.println(calculator.calculate("+",13,20));
        System.out.println(calculator.calculate("%",13,20));
    }

}


Calculator.java
public class Calculator {
    double calculate(String sign,int num1,int num2){
        AbstractOperation re;
        switch (sign) {
            case "+":
                return num1+num2;

            case "-":
                return num1-num2;

            case "/":
                return num1/num2;

            case "*":
                return num1*num2;
            case "%":
                return num1%num2;

        }
        return 0;
    }
}

Step3

Main.java
public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator(new AddOperation(),new SubstractOperation(),
                new MultiplyOperation(),new DivideOperation());
        System.out.println(calculator.calculate("*",13,20));
        System.out.println(calculator.calculate("/",13,20));
        System.out.println(calculator.calculate("-",13,20));
        System.out.println(calculator.calculate("+",13,20));
        System.out.println(calculator.calculate("%",13,20));
    }

}

Calculator.java
public class Calculator {
    AddOperation addOperation;
    SubstractOperation substractOperation;
    MultiplyOperation multiplyOperation;
    DivideOperation divideOperation;

    Calculator(AddOperation addOperation,SubstractOperation substractOperation,
               MultiplyOperation multiplyOperation, DivideOperation divideOperation){
        this.addOperation=addOperation;
        this.substractOperation=substractOperation;
        this.multiplyOperation=multiplyOperation;
        this.divideOperation=divideOperation;
    }
    double calculate(String sign,int num1,int num2){
        AbstractOperation re;
        switch (sign) {
            case "+":
                return addOperation.operate(num1,num2);
            case "-":
                return substractOperation.operate(num1,num2);
            case "/":
                return divideOperation.operate(num1,num2);
            case "*":
                return multiplyOperation.operate(num1,num2);
            case "%":
                return num1%num2;

        }
        return 0;
    }
}

Step4

Main.java
public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator(new AddOperation());
        System.out.println(calculator.calculate(13,20));
        calculator.setter(new SubstractOperation());
        System.out.println(calculator.calculate(13,20));
        calculator.setter((new DivideOperation()));
        System.out.println(calculator.calculate(13,20));
        calculator.setter(new MultiplyOperation());
        System.out.println(calculator.calculate(13,20));
        
    }

}



Calculator.java
public class Calculator {
    AbstractOperation abstractOperation;
    Calculator(AbstractOperation abstractOperation){
        this.abstractOperation = abstractOperation;
    }
    public void setter(AbstractOperation abstractOperation){
        this.abstractOperation = abstractOperation;
    }
    double calculate(int num1,int num2){
        return abstractOperation.operate(num1,num2);
    }
}

profile
backend_Devloper
post-custom-banner

0개의 댓글