From a72595d61f4601ec4cc558dd6d4fea4304064a76 Mon Sep 17 00:00:00 2001 From: Antoine James Tournepiche Date: Sat, 27 Apr 2019 01:53:45 +0200 Subject: [PATCH] Improve code quality --- src/com/github/antoinejt/jasc/ConsoleUI.java | 6 +++--- src/com/github/antoinejt/jasc/Constants.java | 6 +++--- .../jasc/calculator/CalculatorEngine.java | 19 ++++++++----------- .../jasc/calculator/CalculatorException.java | 4 ++-- .../jasc/calculator/OperandException.java | 2 +- .../antoinejt/jasc/calculator/Stack.java | 10 +++++----- .../antoinejt/jasc/util/TextFormat.java | 2 +- 7 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/com/github/antoinejt/jasc/ConsoleUI.java b/src/com/github/antoinejt/jasc/ConsoleUI.java index ccbc735..573e254 100644 --- a/src/com/github/antoinejt/jasc/ConsoleUI.java +++ b/src/com/github/antoinejt/jasc/ConsoleUI.java @@ -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", @@ -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; diff --git a/src/com/github/antoinejt/jasc/Constants.java b/src/com/github/antoinejt/jasc/Constants.java index e946131..9e71a47 100644 --- a/src/com/github/antoinejt/jasc/Constants.java +++ b/src/com/github/antoinejt/jasc/Constants.java @@ -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"; } diff --git a/src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java b/src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java index e52c31e..07d1495 100644 --- a/src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java +++ b/src/com/github/antoinejt/jasc/calculator/CalculatorEngine.java @@ -5,24 +5,21 @@ import java.util.List; public final class CalculatorEngine { - private Stack stack = new Stack(); + private final Stack 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 getNumbers(){ - List 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; @@ -31,14 +28,14 @@ public List 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); @@ -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)){ diff --git a/src/com/github/antoinejt/jasc/calculator/CalculatorException.java b/src/com/github/antoinejt/jasc/calculator/CalculatorException.java index a59b712..20acb19 100644 --- a/src/com/github/antoinejt/jasc/calculator/CalculatorException.java +++ b/src/com/github/antoinejt/jasc/calculator/CalculatorException.java @@ -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); } } diff --git a/src/com/github/antoinejt/jasc/calculator/OperandException.java b/src/com/github/antoinejt/jasc/calculator/OperandException.java index 315b32e..f5b9044 100644 --- a/src/com/github/antoinejt/jasc/calculator/OperandException.java +++ b/src/com/github/antoinejt/jasc/calculator/OperandException.java @@ -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); } } diff --git a/src/com/github/antoinejt/jasc/calculator/Stack.java b/src/com/github/antoinejt/jasc/calculator/Stack.java index 79b2f2a..8782318 100644 --- a/src/com/github/antoinejt/jasc/calculator/Stack.java +++ b/src/com/github/antoinejt/jasc/calculator/Stack.java @@ -2,11 +2,11 @@ import java.util.EmptyStackException; -public final class Stack { +final class Stack { private int size = 0; private java.util.Stack stack = new java.util.Stack<>(); - public T pop(){ + T pop(){ if (size > 0){ size--; return stack.pop(); @@ -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; } } diff --git a/src/com/github/antoinejt/jasc/util/TextFormat.java b/src/com/github/antoinejt/jasc/util/TextFormat.java index 4df0530..0d666bd 100644 --- a/src/com/github/antoinejt/jasc/util/TextFormat.java +++ b/src/com/github/antoinejt/jasc/util/TextFormat.java @@ -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;