Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/main/java/org/fundacionjala/coding/yury/Calc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.fundacionjala.coding.yury;

import java.util.LinkedList;

/**
* Created by JRPNHP48 Team.
*/

public final class Calc {
private int contDigit = -1;
private int contOperator = 0;
private double num1;
private double num2;
private LinkedList<String> copy;

/**
* This constructor.
*/
public Calc() {
copy = new LinkedList();
}

/**
* This method evaluated expression.
*
* @param rpn operation.
* @return result of operation.
*/
public double evaluate(String rpn) {
if (rpn.isEmpty()) {
return 0;
}
String[] token = rpn.split(" ");
for (int i = 0; i < token.length; i++) {
switch (token[i]) {
case "+":
changeVariable();
copy.add((String.valueOf(num2 + num1)));
break;
case "-":
changeVariable();
copy.add((String.valueOf(num2 - num1)));
break;
case "*":
changeVariable();
copy.add((String.valueOf(num2 * num1)));
break;
case "/":
changeVariable();
copy.add((String.valueOf(num2 / num1)));
break;
default:
copy.add(token[i]);
break;
}
}
return !isValidRpnExpression(rpn) ? Double.parseDouble(copy.getLast()) : Double.parseDouble(copy.get(0));
}

/**
* this method change the variable format.
*/
private void changeVariable() {
num1 = Double.parseDouble(copy.pollLast());
num2 = Double.parseDouble(copy.pollLast());
}

/**
* This method validate expression.
*
* @param rpn operation.
* @return if expresion is validate.
*/
public boolean isValidRpnExpression(String rpn) {
for (String token : rpn.split(" ")) {
if (isNumeric(token)) {
contDigit++;
} else if (isValidOperator(token)) {
contOperator++;
if (contOperator > contDigit) {
return false;
}
} else {
return false;
}
}
return contOperator == contDigit;
}

/**
* @param string operator.
* @return boolean.
*/
private static boolean isValidOperator(String string) {
return string.equals("+") || string.equals("-") || string.equals("*") || string.equals("/");
}

/**
* this method verify that a number is digit.
*
* @param string for evaluate.
* @return boolean.
*/
private static boolean isNumeric(String string) {
try {
Integer.parseInt(string);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
69 changes: 69 additions & 0 deletions src/test/java/org/fundacionjala/coding/yury/CalcTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.fundacionjala.coding.yury;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Test.
*/
public class CalcTest {

private Calc calc = new Calc();

/**
* Test should Work With Empty String.
*/
@Test
public void shouldWorkWithEmptyString() {
assertEquals("Should work with empty string", 0, calc.evaluate(""), 0);
}

/**
* Test should Parse Numbers.
*/
@Test
public void shouldParseNumbers() {
assertEquals("Should parse numbers", 3, calc.evaluate("1 2 3"), 0);
}

/**
* Test should Parse Float Numbers.
*/
@Test
public void shouldParseFloatNumbers() {
assertEquals("Should parse float numbers", 3.5, calc.evaluate("1 2 3.5"), 0);
}

/**
* Test should Support Addition.
*/
@Test
public void shouldSupportAddition() {
assertEquals("Should support addition", 4, calc.evaluate("1 3 +"), 0);
}

/**
* Test should Support Multiplication.
*/
@Test
public void shouldSupportMultiplication() {
assertEquals("Should support multiplication", 3, calc.evaluate("1 3 *"), 0);
}

/**
* Test should Support Substraction.
*/
@Test
public void shouldSupportSubstraction() {
assertEquals("Should support substraction", -2, calc.evaluate("1 3 -"), 0);
}

/**
* Test should Support Division.
*/
@Test
public void shouldSupportDivision() {
assertEquals("Should support division", 2, calc.evaluate("4 2 /"), 0);
}
}