Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineJT committed Apr 26, 2019
1 parent e73b3e4 commit a72595d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/com/github/antoinejt/jasc/ConsoleUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.util.*;

public final class ConsoleUI {
final class ConsoleUI {
private static void displayHelp(){
TextFormat.listThings("Available operators (acts on 2 operands) : ",
"+ : Addition operator",
Expand Down Expand Up @@ -84,14 +84,14 @@ public static void useConsole() throws Exception {
} else {
boolean isFunction = false;
switch(input){
case "=": calculatorEngine.getNumbers().forEach(val -> System.out.println(val)); break;
case "=": calculatorEngine.getNumbers().forEach(System.out::println); break;
case "help": displayHelp(); break;
case "clear": calculatorEngine.clear(); break;
case "quit": System.exit(0);
default: isFunction = true;
}
if (isFunction){
FunctionType functionType = null;
FunctionType functionType;
switch(input){ // TODO Maybe implement that with a simple private enum
case "sqrt": functionType = FunctionType.SQRT; break;
case "log": functionType = FunctionType.LOG10; break;
Expand Down
6 changes: 3 additions & 3 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;

public final class Constants {
public static final String VERSION = "0.2";
public static final String LAST_UPDATE = "April 27th 2019";
final class Constants {
static final String VERSION = "0.2.1";
static final String LAST_UPDATE = "April 27th 2019";
}
19 changes: 8 additions & 11 deletions src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@
import java.util.List;

public final class CalculatorEngine {
private Stack stack = new Stack<Float>();
private final Stack<Float> stack = new Stack<>();

public void addNumber(float number){
Float f = new Float(number);
stack.push(f);
stack.push(number);
}

public void clear(){
stack.clear();
}

public List<Float> getNumbers(){
List<Float> list = null;
public List getNumbers(){
List list = null;
try {
list = (List) ReflectUtil.getPrivateField(stack, "stack");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
return list;
Expand All @@ -31,14 +28,14 @@ public List<Float> getNumbers(){
private float[] getOperands(){
float[] ret = new float[2];
for (int i = 0; i < 2; i++){
ret[i] = (Float) stack.pop();
ret[i] = stack.pop();
}
return ret;
}

private double getFunctionResult(FunctionType functionType) throws OperandException {
if (stack.getSize() > 0){
float calc = (float) stack.pop();
float calc = stack.pop();
switch(functionType){ // TODO Implement that on ConsoleUI
case SQRT: return Math.sqrt(calc);
case LOG10: return Math.log10(calc);
Expand All @@ -58,7 +55,7 @@ private double getFunctionResult(FunctionType functionType) throws OperandExcept
return Double.NaN;
}

public void applyFunction(FunctionType functionType) throws OperandException, CalculatorException {
public void applyFunction(FunctionType functionType) throws CalculatorException {
try {
double result = getFunctionResult(functionType);
if (!Double.isNaN(result)){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.antoinejt.jasc.calculator;

public final class CalculatorException extends Exception { // TODO Maybe to remove and replace
public CalculatorException(String msg){
final class CalculatorException extends Exception { // TODO Maybe to remove and replace
CalculatorException(String msg){
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.antoinejt.jasc.calculator;

public final class OperandException extends Exception {
public OperandException(String msg){
OperandException(String msg){
super(msg);
}
}
10 changes: 5 additions & 5 deletions src/com/github/antoinejt/jasc/calculator/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.util.EmptyStackException;

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

public T pop(){
T pop(){
if (size > 0){
size--;
return stack.pop();
Expand All @@ -15,17 +15,17 @@ public T pop(){
}
}

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

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

public int getSize(){
int getSize(){
return size;
}
}
2 changes: 1 addition & 1 deletion src/com/github/antoinejt/jasc/util/TextFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public final class TextFormat {
public static final class FormattedText {
private String formattedText;
private final String formattedText;

private FormattedText(String formattedText){
this.formattedText = formattedText;
Expand Down

0 comments on commit a72595d

Please sign in to comment.