본문 바로가기

JAVA

JAVA 계산기 만들기

Step 1 더하기, 빼기, 나누기, 곱하기 연산을 수행할 수 있는 Calculator 클래스를 만든다.

if, switch를 이용해서 연산자의 타입을 확인하고 해당하는 타입의 연산을 수행후 결과값 반환

 

Caculator.java

package task;

public class Calculator {

    public double calculate(String operator, int firstNumber, int secondNumber) {
        double answer = 0;

        if(operator.equals("+")){
            answer = firstNumber + secondNumber;
        }else if(operator.equals("-")){
            answer = firstNumber - secondNumber;
        }else if(operator.equals("*")){
            answer = firstNumber * secondNumber;
        }else if(operator.equals("/")){
            answer = firstNumber / secondNumber;
        }

        return answer;
    }

}

 

Main.java

package task;

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

 

 

Step 2 나머지 연산자를 수행할 수 있게 Calculator 클래스 내부코드 변경

 

Calculator.java

package task;

public class Calculator {

    public double calculate(String operator, int firstNumber, int secondNumber) {
        double answer = 0;

        if(operator.equals("+")){
            answer = firstNumber + secondNumber;
        }else if(operator.equals("-")){
            answer = firstNumber - secondNumber;
        }else if(operator.equals("*")){
            answer = firstNumber * secondNumber;
        }else if(operator.equals("/")){
            answer = firstNumber / secondNumber;
        }else if(operator.equals("%")) {
            answer = firstNumber % secondNumber;
        }

        return answer;
    }
}

 

Main.java

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

 

 

 

Step 3

AddOperation(더하기), SubstractOperation(빼기), MultiplyOperation(곱하기), DivideOperation(나누기) 연산 클래스를 만든 후 클래스 간의 관계를 고려하여 Calculator 클래스와 관계를 맺는다

 

각각의 타입에 맞게 operate메서드 구현

 

Calculator.java

package task;

public class Calculator {

    private final AddOperation addOperation;
    private final SubstractOperation substractOperation;
    private final MultiplyOperation multiplyOperation;
    private final DivideOperation divideOperation;

    public Calculator(AddOperation addOperation, SubstractOperation substractOperation,
                      MultiplyOperation multiplyOperation, DivideOperation divideOperation) {
        this.addOperation = addOperation;
        this.substractOperation = substractOperation;
        this.multiplyOperation = multiplyOperation;
        this.divideOperation = divideOperation;
    }

    public double calculate(String operator, int firstNumber, int secondNumber) {
        double answer = 0;

        if(operator.equals("+")){
            answer = addOperation.operate(firstNumber, secondNumber);
        }else if(operator.equals("-")){
            answer = substractOperation.operate(firstNumber, secondNumber);
        }else if(operator.equals("*")){
            answer = multiplyOperation.operate(firstNumber, secondNumber);
        }else if(operator.equals("/")) {
            answer = divideOperation.operate(firstNumber, secondNumber);
        }
        return answer;
    }
}

 

AddOperation.java

public class AddOperation {
    public double operate(int firstNumber, int secondNumber){
        return firstNumber + secondNumber;
    }
}

 

SubstractOperation.java

public class SubstractOperation {
    public double operate(int firstNumber, int secondNumber){
        return firstNumber - secondNumber;
    }
}

 

MultiplyOperation.java

public class MultiplyOperation {
    public double operate(int firstNumber, int secondNumber){
        return firstNumber * secondNumber;
    }
}

 

DivideOperation.java

public class DivideOperation {
    public double operate(int firstNumber, int secondNumber){
        return firstNumber / secondNumber;
    }
}

 

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("+", 10, 20));		//test
        System.out.println(calculator.calculate("*", 10, 20));		//test
    }
}

 

Step 4

AddOperation(더하기), SubstractOperation(빼기), MultiplyOperation(곱하기), DivideOperation(나누기) 연산 클래스들을 AbstractOperation(추상 클래스)를 사용하여 추상화하고 Calculator 클래스의 내부 코드를 변경

 

AddOperation, SubstractOperation, MultiplyOperation, DivideOperation 클래스들은 AbstractOperation 클래스를 상속받고 각각의 연산 타입에 맞게 operate를 오버라이딩

Calculator 클래스는 4개의 연산 클래스들이 상속받고 있는 AbstractOperation 클래스만을 포함

생성자 혹은 Setter를 사용하여 연산을 수행할 연산 클래스의 객체를 AbstractOperation 클래스 타입의 필드에 주입

calculate 메서드에서는 더 이상 연산자 타입을 받아 구분할 필요 없이 주입 받은 연산 클래스의 operate 메서드를 통해 바로 연산을 수행

 

Calculator.java

package task;

public class Calculator {

    private AbstractOperation operation;

    public Calculator(AbstractOperation operation) {
        this.operation = operation;
    }

    public void setOperation(AbstractOperation operation) {
        this.operation = operation;
    }

    public double calculate(int firstNumber, int secondNumber) {
        double answer = 0;
        answer = operation.operate(firstNumber, secondNumber);
        return answer;
    }
}

 

AddOperation.java

package task;

public class AddOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber){
        return firstNumber + secondNumber;
    }
}

 

AbstractOperation.java

package task;

public abstract class AbstractOperation {
    public abstract double operate(int firstNumber, int secondNumber);
}

 

SubstractOperation.java

package task;

public class SubstractOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber){
        return firstNumber - secondNumber;
    }
}

 

MultiplyOperation.java

package task;

public class MultiplyOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber){
        return firstNumber * secondNumber;
    }
}

 

DivideOperation.java

package task;

public class DivideOperation extends AbstractOperation{
    @Override
    public double operate(int firstNumber, int secondNumber){
        return firstNumber / secondNumber;
    }
}

 

Main.java

package task;

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

 

'JAVA' 카테고리의 다른 글

240422 자바 기초, if문,for문  (0) 2024.04.22