-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
45 lines (39 loc) · 1.85 KB
/
Main.java
File metadata and controls
45 lines (39 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package taboola.israelrozen.solution_1;
import taboola.israelrozen.solution_1.exceptions.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
public static void main(String[] args) throws IOException {
Calculator calculator = new Calculator();
ExpressionValidator exprValidator=new ExpressionValidator();
System.out.print("Text based calculator:\n" +
" [+] Supported operations : addition, subtraction, multiplication, pre-increment, and post-increment.\n" +
" [+] Keep inserting matematical expressions with the supported operations only .\n" +
" [+] Seperate operations and variables with a white-space .\n\n");
while (true) {
BufferedReader buffer= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter expression: ");
String exp = buffer.readLine();
if (exp.isEmpty()) break;// print results
try {
// Validation layer
// handle the validations and raise invalid operations and expressions exceptions
if(exprValidator.ValidExpression(exp)){
// calculation layer
// handle the calculations and will raise undefined variabled exceptions
calculator.calculate(exp);
}
} catch (InvalidExpressionException e) {
System.out.println("Invalid expression.");
} catch (UndefinedVariableException e) {
System.out.println("Variable is not defined.");
}
catch (UnsupportedOperationException e) {
System.out.println("Operation is not supported.");
}
}
;
System.out.print(calculator.getResult());
}
}