1
1
package com .example .kalkulator .activities ;
2
2
3
+ import android .content .Context ;
3
4
import android .os .Bundle ;
5
+ import android .os .Vibrator ;
6
+ import android .view .View ;
7
+ import android .widget .Button ;
4
8
import androidx .appcompat .app .AppCompatActivity ;
5
9
import com .example .kalkulator .R ;
10
+ import com .example .kalkulator .listeners .DigitOnClickListener ;
11
+ import com .example .kalkulator .listeners .OperationOnClickListener ;
12
+ import com .example .kalkulator .utils .CalculatorHandler ;
13
+
14
+ import static com .example .kalkulator .utils .CalculatorHandler .*;
6
15
7
16
public class AdvancedCalculatorActivity extends AppCompatActivity
8
17
{
9
18
19
+ private Button btn0 , btn1 , btn2 , btn3 , btn4 , btn5 , btn6 , btn7 , btn8 , btn9 ,
20
+ btnComma , btnAllClear , btnPlus , btnMinus , btnMultiply , btnDivide , btnEquals ,
21
+ btnPlusMinus , btnClearOrClearAll , btnXToTheYPower , btnXSquared ,
22
+ btnSin , btnCos , btnTan , btnLn , btnSqrt , btnLog , btnPercent ;
23
+
10
24
@ Override
11
25
protected void onCreate (Bundle savedInstanceState )
12
26
{
13
27
super .onCreate (savedInstanceState );
14
28
setContentView (R .layout .activity_advanced_calculator );
29
+
30
+ CalculatorHandler .setVibrator ((Vibrator ) getSystemService (Context .VIBRATOR_SERVICE ));
31
+ CalculatorHandler .setValueTextView (findViewById (R .id .value_text_view ));
32
+ CalculatorHandler .setOperationTextView (findViewById (R .id .operation_text_view ));
33
+ CalculatorHandler .setPrevValueTextView (findViewById (R .id .previous_value_text_view ));
34
+ CalculatorHandler .setNewValueFlag (false );
35
+
36
+ btn0 = findViewById (R .id .btn0 );
37
+ btn1 = findViewById (R .id .btn1 );
38
+ btn2 = findViewById (R .id .btn2 );
39
+ btn3 = findViewById (R .id .btn3 );
40
+ btn4 = findViewById (R .id .btn4 );
41
+ btn5 = findViewById (R .id .btn5 );
42
+ btn6 = findViewById (R .id .btn6 );
43
+ btn7 = findViewById (R .id .btn7 );
44
+ btn8 = findViewById (R .id .btn8 );
45
+ btn9 = findViewById (R .id .btn9 );
46
+
47
+ btnComma = findViewById (R .id .btn_comma );
48
+ btnAllClear = findViewById (R .id .btn_all_clear );
49
+ btnPlus = findViewById (R .id .btn_plus );
50
+ btnMinus = findViewById (R .id .btn_minus );
51
+ btnMultiply = findViewById (R .id .btn_multiply );
52
+ btnDivide = findViewById (R .id .btn_divide );
53
+ btnEquals = findViewById (R .id .btn_equals );
54
+ btnPlusMinus = findViewById (R .id .btn_plus_minus );
55
+ btnClearOrClearAll = findViewById (R .id .btn_clear_or_clear_all );
56
+ btnXToTheYPower = findViewById (R .id .btn_x_to_the_y_power );
57
+ btnXSquared = findViewById (R .id .btn_x_squared );
58
+ btnSin = findViewById (R .id .btn_sin );
59
+ btnCos = findViewById (R .id .btn_cos );
60
+ btnTan = findViewById (R .id .btn_tan );
61
+ btnLn = findViewById (R .id .btn_ln );
62
+ btnSqrt = findViewById (R .id .btn_sqrt );
63
+ btnLog = findViewById (R .id .btn_log );
64
+ btnPercent = findViewById (R .id .btn_percent );
65
+
66
+ btn0 .setOnClickListener (new DigitOnClickListener (0 ));
67
+ btn1 .setOnClickListener (new DigitOnClickListener (1 ));
68
+ btn2 .setOnClickListener (new DigitOnClickListener (2 ));
69
+ btn3 .setOnClickListener (new DigitOnClickListener (3 ));
70
+ btn4 .setOnClickListener (new DigitOnClickListener (4 ));
71
+ btn5 .setOnClickListener (new DigitOnClickListener (5 ));
72
+ btn6 .setOnClickListener (new DigitOnClickListener (6 ));
73
+ btn7 .setOnClickListener (new DigitOnClickListener (7 ));
74
+ btn8 .setOnClickListener (new DigitOnClickListener (8 ));
75
+ btn9 .setOnClickListener (new DigitOnClickListener (9 ));
76
+
77
+ btnComma .setOnClickListener (this ::commaOnClick );
78
+ btnAllClear .setOnClickListener (this ::allClearOnClick );
79
+ btnPlus .setOnClickListener (new OperationOnClickListener (CHAR_PLUS ));
80
+ btnMinus .setOnClickListener (new OperationOnClickListener (CHAR_MINUS ));
81
+ btnMultiply .setOnClickListener (new OperationOnClickListener (CHAR_MULTIPLY ));
82
+ btnDivide .setOnClickListener (new OperationOnClickListener (CHAR_DIVIDE ));
83
+ btnEquals .setOnClickListener (this ::equalsOnClick );
84
+ btnPlusMinus .setOnClickListener (this ::plusMinusOnClick );
85
+ btnClearOrClearAll .setOnClickListener (this ::clearOrClearAllOnClick );
86
+ btnXToTheYPower .setOnClickListener (new OperationOnClickListener (CHAR_POWER ));
87
+ btnXSquared .setOnClickListener (this ::xSquaredOnClick );
88
+ btnSin .setOnClickListener (this ::sinOnClick );
89
+ btnCos .setOnClickListener (this ::cosOnClick );
90
+ btnTan .setOnClickListener (this ::tanOnClick );
91
+ btnLn .setOnClickListener (this ::lnOnClick );
92
+ btnSqrt .setOnClickListener (this ::sqrtOnClick );
93
+ btnLog .setOnClickListener (this ::logOnClick );
94
+ btnPercent .setOnClickListener (this ::percentOnClick );
95
+ }
96
+
97
+ public void percentOnClick (View v )
98
+ {
99
+ if (!prevValueTextView .getText ().toString ().isEmpty ())
100
+ {
101
+ double prevValue = Double .parseDouble (prevValueTextView .getText ().toString ().replace (',' , '.' ));
102
+ double currValue = Double .parseDouble (valueTextView .getText ().toString ().replace (',' , '.' ));
103
+ char previousOperation = operationTextView .getText ().charAt (0 );
104
+
105
+ currValue = ((previousOperation == CHAR_PLUS ) || (previousOperation == CHAR_MINUS )) ? (currValue / 100.0F * prevValue ) : currValue ;
106
+ currValue = ((previousOperation == CHAR_MULTIPLY ) || (previousOperation == CHAR_DIVIDE ) || (previousOperation == CHAR_POWER )) ? (currValue / 100.0F ) : currValue ;
107
+
108
+ double result = OperationOnClickListener .calculate (prevValue , currValue );
109
+
110
+ prevValueTextView .setText ("" );
111
+ operationTextView .setText ("" );
112
+ valueTextView .setText (DECIMAL_FORMAT .format (result ).replace ('.' , ',' ));
113
+
114
+ newValueFlag = true ;
115
+ }
116
+
117
+ makeStandardVibration ();
118
+ }
119
+
120
+ private void logOnClick (View view )
121
+ {
122
+ String valueText = valueTextView .getText ().toString ();
123
+
124
+ Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
125
+
126
+ currValue = Math .log10 (currValue );
127
+
128
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
129
+
130
+ makeStandardVibration ();
131
+ }
132
+
133
+ private void sqrtOnClick (View view )
134
+ {
135
+ String valueText = valueTextView .getText ().toString ();
136
+
137
+ Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
138
+
139
+ currValue = Math .sqrt (currValue );
140
+
141
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
142
+
143
+ makeStandardVibration ();
144
+ }
145
+
146
+ private void lnOnClick (View view )
147
+ {
148
+ String valueText = valueTextView .getText ().toString ();
149
+
150
+ Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
151
+
152
+ currValue = Math .log (currValue );
153
+
154
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
155
+
156
+ makeStandardVibration ();
157
+ }
158
+
159
+ private void sinOnClick (View view )
160
+ {
161
+ String valueText = valueTextView .getText ().toString ();
162
+
163
+ Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
164
+
165
+ currValue = Math .sin (currValue );
166
+
167
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
168
+
169
+ makeStandardVibration ();
170
+ }
171
+
172
+ private void cosOnClick (View view )
173
+ {
174
+ String valueText = valueTextView .getText ().toString ();
175
+
176
+ Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
177
+
178
+ currValue = Math .cos (currValue );
179
+
180
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
181
+
182
+ makeStandardVibration ();
183
+ }
184
+
185
+ private void tanOnClick (View view )
186
+ {
187
+ String valueText = valueTextView .getText ().toString ();
188
+
189
+ Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
190
+
191
+ currValue = Math .tan (currValue );
192
+
193
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
194
+
195
+ makeStandardVibration ();
196
+ }
197
+
198
+ private void xSquaredOnClick (View view )
199
+ {
200
+ String valueText = valueTextView .getText ().toString ();
201
+
202
+ Double currValue = Double .parseDouble (valueText .replace (',' , '.' ));
203
+
204
+ currValue = Math .pow (currValue , 2.0 );
205
+
206
+ valueTextView .setText (DECIMAL_FORMAT .format (currValue ).replace ('.' , ',' ));
207
+
208
+ makeStandardVibration ();
209
+ }
210
+
211
+ private void clearOrClearAllOnClick (View view )
212
+ {
213
+ String valueText = valueTextView .getText ().toString ();
214
+
215
+ if (valueText .equals ("0" ))
216
+ {
217
+ operationTextView .setText ("" );
218
+ prevValueTextView .setText ("" );
219
+ }
220
+ else
221
+ {
222
+ valueTextView .setText ("0" );
223
+ }
224
+
225
+ makeStandardVibration ();
226
+ }
227
+
228
+ public void plusMinusOnClick (View v )
229
+ {
230
+ String valueText = valueTextView .getText ().toString ();
231
+
232
+ if (valueText .charAt (0 ) == '-' )
233
+ {
234
+ valueText = valueText .substring (1 );
235
+ }
236
+ else if (!valueText .equals ("0" ))
237
+ {
238
+ valueText = "-" + valueText ;
239
+ }
240
+
241
+ valueTextView .setText (valueText );
242
+
243
+ makeStandardVibration ();
244
+ }
245
+
246
+ public void allClearOnClick (View v )
247
+ {
248
+ valueTextView .setText ("0" );
249
+ prevValueTextView .setText ("" );
250
+ operationTextView .setText ("" );
251
+
252
+ makeStandardVibration ();
253
+ }
254
+
255
+ public void equalsOnClick (View v )
256
+ {
257
+ if (!prevValueTextView .getText ().toString ().isEmpty ())
258
+ {
259
+ double prevValue = Double .parseDouble (prevValueTextView .getText ().toString ().replace (',' , '.' ));
260
+ double currValue = Double .parseDouble (valueTextView .getText ().toString ().replace (',' , '.' ));
261
+
262
+ double result = OperationOnClickListener .calculate (prevValue , currValue );
263
+
264
+ prevValueTextView .setText ("" );
265
+ operationTextView .setText ("" );
266
+ valueTextView .setText (DECIMAL_FORMAT .format (result ).replace ('.' , ',' ));
267
+
268
+ newValueFlag = true ;
269
+ }
270
+
271
+ makeStandardVibration ();
272
+ }
273
+
274
+ public void commaOnClick (View v )
275
+ {
276
+ String text = valueTextView .getText ().toString ();
277
+ long numberOfDigits = text .chars ().filter (Character ::isDigit ).count ();
278
+
279
+ if ((!text .contains ("," )) && (numberOfDigits > 0 ) && (numberOfDigits < VALUE_TEXT_VIEW_MAX_SIZE ))
280
+ {
281
+ valueTextView .setText (valueTextView .getText () + "," );
282
+ }
283
+
284
+ makeStandardVibration ();
15
285
}
16
286
}
0 commit comments