-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
134 lines (108 loc) · 3.15 KB
/
main.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.Scanner;
import java.util.ArrayList;
import java.nio.file.Paths;
public class main {
public static class Timer {
private final long start;
Timer(){
start = System.currentTimeMillis();
}
public long Get(){
return System.currentTimeMillis() - start;
}
}
public static class Reader {
ArrayList<String> container = new ArrayList<String>();
Reader(String path){
this.read(path);
}
public void read(String path){
container.clear();
Scanner scanner;
try {
scanner = new Scanner(Paths.get(path));
while(scanner.hasNext())
container.add(scanner.next());
scanner.close();
}
catch (Exception e) {
System.out.println(e);
}
}
public ArrayList<String> Get(){
return container;
}
}
public static class Calculator {
private float total_value;
private float value;
Calculator(){
total_value = 0;
value = 0;
}
/**
* <p>Clear all fiedls<p>
*/
private void clear(){
total_value = 0;
value = 0;
}
public void calculate(String command){
switch (command) {
case ("+"):
this.summation();
break;
case ("-"):
this.subtraction();
break;
case ("*"):
this.multiplication();
break;
case ("/"):
this.division();
break;
case ("c"):
this.clear();
break;
default:
float v;
try {
v = Float.parseFloat(command);
this.set_value(v);
}
catch (NumberFormatException e) {
System.out.println("Command unknown!");
}
break;
}
}
private void set_value(float value) {
this.value = value;
}
private void summation(){
total_value += value;
}
private void subtraction(){
total_value -= value;
}
private void multiplication(){
total_value *= value;
}
private void division(){
total_value /= value;
}
public float GetTotalValue(){
return total_value;
}
}
public static void main(String[] args) {
//Timer t = new Timer();
Calculator c = new Calculator();
Reader file = new Reader("C:\\Users\\Vanya\\Documents\\JavaProject\\Calculator\\calculator.in");
ArrayList<String> list = file.Get();
for (String string : list) {
c.calculate(string);
System.out.println(c.GetTotalValue());
}
}
}