diff --git a/README.md b/README.md index 2ebf1fa..c1ed9f2 100644 --- a/README.md +++ b/README.md @@ -39,19 +39,18 @@ Library contains: **Doesn't check for valid sequence at all. Only critical misspells.** 3. Calculator class. ```java - public class App { - public static void calculate(Expression expression) { - Calculator calculator = new Calculator(); - double result; - try { - result = calculator.calculate((SequenceExpression) expression); - } catch (CalculatorException e) { - // process exception - } - - System.out.println(result); - } - } + public class App { + public static void calculate(Expression expression) { + double result = 0; + try { + result = Calculator.getInstance().calculate((SequenceExpression) expression); + } catch (CalculatorException e) { + // process exception + } + + System.out.println(result); + } + } ``` You needn't instantiate Calculator every use, you can make it once. 4. Number class, but I don't recommend you to use it. @@ -85,21 +84,21 @@ import java.util.Scanner; public class App { public static void main(String[] args) throws ParserIllegalTokenValueException, ParserUnknownEntityException, ExpressionException, CalculatorException { - Scanner scanner = new Scanner(System.in); - System.out.print("Please enter an expression: "); + Scanner scanner = new Scanner(System.in); + System.out.print("Please enter an expression: "); - String expression = scanner.nextLine(); + String expression = scanner.nextLine(); - Parser parser = new Parser(expression); - parser.parse(); + Parser parser = new Parser(expression); + parser.parse(); - ExpressionsTree expressionsTree = new ExpressionsTree(parser.getResult()); - expressionsTree.build(); + ExpressionsTree expressionsTree = new ExpressionsTree(parser.getResult()); + expressionsTree.build(); - Calculator calculator = new Calculator(); - double result = calculator.calculate((SequenceExpression) expressionsTree.getRoot()); + double result = Calculator.getInstance() + .calculate((SequenceExpression) expressionsTree.getRoot()); - System.out.printf("%s = %f%n", expression, result); + System.out.printf("%s = %f%n", expression, result); } } ``` \ No newline at end of file diff --git a/src/main/java/ru/corvinella/App.java b/src/main/java/ru/corvinella/App.java index 9ebd22f..f37f2aa 100644 --- a/src/main/java/ru/corvinella/App.java +++ b/src/main/java/ru/corvinella/App.java @@ -1,10 +1,35 @@ package ru.corvinella; +import ru.corvinella.expressions.ExpressionsTree; +import ru.corvinella.expressions.entries.SequenceExpression; +import ru.corvinella.expressions.exceptions.ExpressionException; +import ru.corvinella.math.Calculator; +import ru.corvinella.math.exceptions.CalculatorException; +import ru.corvinella.parser.Parser; +import ru.corvinella.parser.exceptions.ParserIllegalTokenValueException; +import ru.corvinella.parser.exceptions.ParserUnknownEntityException; + +import java.util.Scanner; + public class App { /** * Main method for testing. */ - public static void main(String[] args) { - // nothing here... + public static void main(String[] args) throws ParserIllegalTokenValueException, ParserUnknownEntityException, ExpressionException, CalculatorException { + Scanner scanner = new Scanner(System.in); + System.out.print("Please enter an expression: "); + + String expression = scanner.nextLine(); + + Parser parser = new Parser(expression); + parser.parse(); + + ExpressionsTree expressionsTree = new ExpressionsTree(parser.getResult()); + expressionsTree.build(); + + double result = Calculator.getInstance() + .calculate((SequenceExpression) expressionsTree.getRoot()); + + System.out.printf("%s = %f%n", expression, result); } } diff --git a/src/main/java/ru/corvinella/math/Calculator.java b/src/main/java/ru/corvinella/math/Calculator.java index e729d70..22e8216 100644 --- a/src/main/java/ru/corvinella/math/Calculator.java +++ b/src/main/java/ru/corvinella/math/Calculator.java @@ -8,16 +8,21 @@ */ public class Calculator { private static final Calculator instance; - private static final SequenceCalculator sequenceCalculator; static { instance = new Calculator(); - sequenceCalculator = new SequenceCalculator(); } + + private final SequenceCalculator sequenceCalculator; + public static Calculator getInstance() { return instance; } + private Calculator() { + sequenceCalculator = new SequenceCalculator(); + } + public Double calculate(SequenceExpression expression) throws CalculatorException { return sequenceCalculator.calculate(expression); }