-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
107 lines (90 loc) · 3.47 KB
/
Copy pathCalculator.java
File metadata and controls
107 lines (90 loc) · 3.47 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.company;
import com.company.commands.Command;
import com.company.exceptions.ArithmeticalException;
import com.company.exceptions.CommandException;
import com.company.exceptions.FactoryException;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.*;
public class Calculator {
private static final Logger log = Logger.getLogger(Calculator.class.getName());
public void run(String[] args) {
try {
log.setUseParentHandlers(false);
FileHandler fh = new FileHandler("src/log.txt");
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
log.addHandler(fh);
} catch (IOException e) {
e.printStackTrace();
}
Calculator calculator = new Calculator();
log.info("Calculator created");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
if (args.length > 0){
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
log.info("Opened file: " + args[0]);
} catch (FileNotFoundException e) {
log.info("Error opening file: " + args[0]);
}
}
calculator.calculate(reader);
}
public void calculate(BufferedReader reader) {
try{
String line;
line = reader.readLine();
while (line != null) {
parserCommand(line);
line = reader.readLine();
}
}
catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException exception) {
log.info("Error: problem with reading stream");
}
finally {
if (null != reader)
{
try
{
reader.close();
}
catch (IOException e)
{
log.warning(e.getMessage());
}
}
}
}
private final AuxiliaryCommand auxiliary = new AuxiliaryCommand();
private void parserCommand(String line) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
int currentIndex = 0;
while ((currentIndex < line.length()) && !Character.isWhitespace(line.charAt(currentIndex))) {
currentIndex++;
}
String commandName = line.substring(0, currentIndex++);
String commandParameters = "";
if (currentIndex < line.length()) {
commandParameters = line.substring(currentIndex);
}
Command command;
try {
command = CommandFactory.getInstance().createCommand(commandName);
}
catch (FactoryException e){
log.warning("Problem with CommandFactory: " + e.getMessage());
return;
}
try{
command.execute(auxiliary, commandParameters);
log.info("Successful command: " + commandName);
}
catch (ArithmeticalException e){
log.warning("Arithmetical Exception: " + e.getMessage());
}
catch (CommandException e){
log.warning("Command Exception: " + e.getMessage());
}
}
}