Skip to content
Snippets Groups Projects
Commit 1a8ffd76 authored by Sondre Bolland's avatar Sondre Bolland
Browse files

Removed Operator interface and classes dependence. Operators are hardcoded

parent 2e906771
No related branches found
No related tags found
No related merge requests found
package no.uib.inf101.calculator; package no.uib.inf101.calculator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -17,41 +18,8 @@ import no.uib.inf101.calculator.operations.Subtraction; ...@@ -17,41 +18,8 @@ import no.uib.inf101.calculator.operations.Subtraction;
*/ */
public class Calculator { public class Calculator {
/**
* A map of operator symbols to their corresponding Operator implementations
*/
private Map<String, Operator> operators;
public Calculator() { public Calculator() {
this.operators = new HashMap<>();
addOperators();
}
/**
* Adds the default operators such as Addition, Subtraction, Multiplication to the calculator.
*/
private void addOperators() {
addOperator(new Addition());
addOperator(new Subtraction());
addOperator(new Multiplication());
}
/**
* Adds a single operator to the calculator.
* @param operator an implementation of the `Operator` interface
*/
private void addOperator(Operator operator) {
operators.put(operator.getSymbol(), operator);
}
/**
* Retrieves an operator by its symbol.
*
* @param symbol the symbol of the operator (e.g., "+", "-", "*")
* @return the `Operator` object corresponding to the symbol, or null if not found
*/
public Operator getOperator(String symbol) {
return operators.get(symbol);
} }
/** /**
...@@ -60,7 +28,8 @@ public class Calculator { ...@@ -60,7 +28,8 @@ public class Calculator {
* @return a list of operator symbols * @return a list of operator symbols
*/ */
public List<String> getOperatorSymbols() { public List<String> getOperatorSymbols() {
return new ArrayList<String>(operators.keySet()); String[] operators = new String[]{"+", "-", "*"};
return new ArrayList<String>(Arrays.asList(operators));
} }
/** /**
...@@ -77,8 +46,6 @@ public class Calculator { ...@@ -77,8 +46,6 @@ public class Calculator {
Expression operand1 = expression.getOperand1(); Expression operand1 = expression.getOperand1();
Expression operand2 = expression.getOperand2(); Expression operand2 = expression.getOperand2();
String operatorSymbol = expression.getOperator(); String operatorSymbol = expression.getOperator();
if (!operators.containsKey(operatorSymbol))
throw new IllegalArgumentException("The operator is not supported by the caluclator: " + operatorSymbol);
return evaluate(evaluate(operand1), evaluate(operand2), operatorSymbol); return evaluate(evaluate(operand1), evaluate(operand2), operatorSymbol);
} }
...@@ -93,7 +60,16 @@ public class Calculator { ...@@ -93,7 +60,16 @@ public class Calculator {
* @throws NullPointerException if the operator is not found in the calculator * @throws NullPointerException if the operator is not found in the calculator
*/ */
private double evaluate(double num1, double num2, String operatorSymbol) { private double evaluate(double num1, double num2, String operatorSymbol) {
return operators.get(operatorSymbol).calculate(num1, num2); if (operatorSymbol.equals("+")) {
return num1 + num2;
}
if (operatorSymbol.equals("-")) {
return num1 - num2;
}
if (operatorSymbol.equals("*")) {
return num1 * num2;
}
throw new IllegalArgumentException("The operator is not supported by the calculator: " + operatorSymbol);
} }
} }
...@@ -9,7 +9,6 @@ import java.awt.GridLayout; ...@@ -9,7 +9,6 @@ import java.awt.GridLayout;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment