Skip to content

Commit

Permalink
Fix README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh18RW committed Dec 3, 2024
1 parent e4e4747 commit 9ea8279
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
45 changes: 22 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
```
29 changes: 27 additions & 2 deletions src/main/java/ru/corvinella/App.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 7 additions & 2 deletions src/main/java/ru/corvinella/math/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 9ea8279

Please sign in to comment.