Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineJT committed Jun 11, 2019
1 parent e04f046 commit e098637
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 54 deletions.
31 changes: 18 additions & 13 deletions src/com/github/antoinejt/jasc/ConsoleUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
import java.util.*;

final class ConsoleUI {
private static final List<String> commands = new ArrayList<>();
private static final Map<String, OperationType> operators = new HashMap<>();
static {
operators.put("+", OperationType.ADDITION);
operators.put("-", OperationType.SUBSTRACTION);
operators.put("*", OperationType.MULTIPLICATION);
operators.put("/", OperationType.DIVISION);
operators.put("%", OperationType.MODULO);
operators.put("^", OperationType.POWER);

commands.addAll(operators.keySet()); // operators are added here
commands.addAll(Arrays.asList(
"sqrt", "log", "ln", "lb", "cos", "sin", "tan", "arccos", "arcsin", "arctan", "exp", // Functions
"=", "help", "clear", "quit" // Commands
));
}

// TODO Replace that by some txt templates
private static void displayHelp(){
TextFormat.listThings("Available operators (acts on 2 operands) : ",
"+ : Addition operator",
Expand Down Expand Up @@ -67,11 +85,6 @@ public static void useConsole() throws Exception {
String input;
while (true){
input = scanner.next();
List<String> commands = Arrays.asList(
"+", "-", "*", "/", "%", "^", // Operators
"sqrt", "log", "ln", "lb", "cos", "sin", "tan", "arccos", "arcsin", "arctan", "exp", // Functions
"=", "help", "clear", "quit" // Commands
);
boolean hasInputNumber = !commands.contains(input);
if (hasInputNumber){
try {
Expand All @@ -81,14 +94,6 @@ public static void useConsole() throws Exception {
System.err.println("Your input is invalid!");
}
} else {
Map<String, OperationType> operators = new HashMap<>();
operators.put("+", OperationType.ADDITION);
operators.put("-", OperationType.SUBSTRACTION);
operators.put("*", OperationType.MULTIPLICATION);
operators.put("/", OperationType.DIVISION);
operators.put("%", OperationType.MODULO);
operators.put("^", OperationType.POWER);

if (operators.containsKey(input)){
try {
calculatorEngine.operate(operators.get(input));
Expand Down
4 changes: 2 additions & 2 deletions src/com/github/antoinejt/jasc/Constants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.antoinejt.jasc;

final class Constants {
static final String VERSION = "0.3.2";
static final String LAST_UPDATE = "May 3rd 2019";
static final String VERSION = "0.3.3";
static final String LAST_UPDATE = "June 11th 2019";
}
4 changes: 2 additions & 2 deletions src/com/github/antoinejt/jasc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class Main {
public static void main(String[] args){
try {
ConsoleUI.useConsole();
} catch (Exception e) {
e.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
63 changes: 32 additions & 31 deletions src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,36 @@ public List getNumbers(){
List stackContent = null;
try {
stackContent = (List) ReflectUtil.getPrivateField(stack, "stack");
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException | NoSuchFieldException exception) {
exception.printStackTrace();
}
return stackContent;
}

private float[] getOperands(){
float[] ret = new float[2];
float[] operands = new float[2];
for (int i = 0; i < 2; i++){
ret[i] = stack.pop();
operands[i] = stack.pop();
}
return ret;
return operands;
}

private double getFunctionResult(FunctionType functionType) throws OperandException {
if (stack.getSize() > 0){
float calc = stack.pop();
switch(functionType){ // TODO Implement that on ConsoleUI
case SQRT: return Math.sqrt(calc);
case LOG10: return Math.log10(calc);
case LN: return Math.log(calc);
case LOGB: return (int)(Math.log(calc)/Math.log(2)+1e-10); // https://stackoverflow.com/questions/3305059/how-do-you-calculate-log-base-2-in-java-for-integers
case COS: return Math.cos(calc);
case SIN: return Math.sin(calc);
case TAN: return Math.tan(calc);
case ARCCOS: return Math.acos(calc);
case ARCSIN: return Math.asin(calc);
case ARCTAN: return Math.atan(calc);
case EXP: return Math.exp(calc);
float number = stack.pop();
switch(functionType){ // TODO Implement that on ConsoleUI (why?)
case SQRT: return Math.sqrt(number);
case LOG10: return Math.log10(number);
case LN: return Math.log(number);
// TODO Put that in a dedicated function
case LOGB: return (int)(Math.log(number)/Math.log(2)+1e-10); // https://stackoverflow.com/questions/3305059/how-do-you-calculate-log-base-2-in-java-for-integers
case COS: return Math.cos(number);
case SIN: return Math.sin(number);
case TAN: return Math.tan(number);
case ARCCOS: return Math.acos(number);
case ARCSIN: return Math.asin(number);
case ARCTAN: return Math.atan(number);
case EXP: return Math.exp(number);
}
} else {
throw new OperandException("Stack is empty!");
Expand All @@ -59,7 +60,7 @@ public void applyFunction(FunctionType functionType) throws CalculatorException
try {
double result = getFunctionResult(functionType);
if (!Double.isNaN(result)){
stack.push(Double.valueOf(result).floatValue());
stack.push((float) result);
} else {
throw new CalculatorException("Some weird error occurred when applying function. Please contact the application maintainer!");
}
Expand All @@ -70,27 +71,27 @@ public void applyFunction(FunctionType functionType) throws CalculatorException

public void operate(OperationType operation) throws OperandException, CalculatorException {
if (stack.getSize() > 1) {
float[] operand = getOperands();
float calc;
if (operation == OperationType.DIVISION && operand[0] == 0.0f){
float[] operands = getOperands();
float result;
if (operation == OperationType.DIVISION && operands[0] == 0.0f){
System.err.println("Division by zero!");
for(int i = 0; i < 2; i++){
stack.push(operand[1-i]); // Reinject operands into the stack!
stack.push(operands[1-i]); // Reinject operands into the stack!
}
return;
}
switch(operation){
case ADDITION: calc = operand[1] + operand[0]; break;
case SUBSTRACTION: calc = operand[1] - operand[0]; break;
case MULTIPLICATION: calc = operand[1] * operand[0]; break;
case DIVISION: calc = operand[1] / operand[0]; break;
case MODULO: calc = operand[1] % operand[0]; break;
case POWER: calc = (float) Math.pow(operand[1], operand[0]); break;
case ADDITION: result = operands[1] + operands[0]; break;
case SUBSTRACTION: result = operands[1] - operands[0]; break;
case MULTIPLICATION: result = operands[1] * operands[0]; break;
case DIVISION: result = operands[1] / operands[0]; break;
case MODULO: result = operands[1] % operands[0]; break;
case POWER: result = (float) Math.pow(operands[1], operands[0]); break;
default: throw new CalculatorException("The provided operation is not handled!");
}
// I can't imagine how this can happened
// if (Float.isNaN(calc)) return;
stack.push(calc);
// if (Float.isNaN(result)) return;
stack.push(result);
} else {
throw new OperandException("Can't operate without at least 2 operands!");
}
Expand Down
8 changes: 2 additions & 6 deletions src/com/github/antoinejt/jasc/calculator/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@
import java.util.EmptyStackException;

final class Stack<T> {
private int size = 0;
private java.util.Stack<T> stack = new java.util.Stack<>();

T pop(){
if (size > 0){
size--;
if (stack.size() > 0){
return stack.pop();
} else {
throw new EmptyStackException();
}
}

void push(T item){
size++;
stack.push(item);
}

void clear(){
size = 0;
stack = new java.util.Stack<>();
}

int getSize(){
return size;
return stack.size();
}
}

0 comments on commit e098637

Please sign in to comment.