1
1
package com .example .kalkulator .activities ;
2
2
3
- import android .content .Context ;
4
3
import android .os .Bundle ;
5
- import android .os .Vibrator ;
6
4
import android .view .View ;
7
5
import android .widget .Button ;
8
6
import android .widget .Toast ;
9
- import androidx .appcompat .app .AppCompatActivity ;
10
7
import com .example .kalkulator .R ;
11
- import com .example .kalkulator .listeners .DigitOnClickListener ;
12
8
import com .example .kalkulator .listeners .OperationOnClickListener ;
13
- import com .example .kalkulator .utils .CalculatorHandler ;
14
9
15
10
import static com .example .kalkulator .utils .CalculatorHandler .*;
16
11
17
- public class AdvancedCalculatorActivity extends AppCompatActivity
12
+ public class AdvancedCalculatorActivity extends SimpleCalculatorActivity
18
13
{
19
-
20
- private Button btn0 , btn1 , btn2 , btn3 , btn4 , btn5 , btn6 , btn7 , btn8 , btn9 ,
21
- btnComma , btnAllClear , btnPlus , btnMinus , btnMultiply , btnDivide , btnEquals ,
22
- btnPlusMinus , btnClearOrClearAll , btnXToTheYPower , btnXSquared ,
23
- btnSin , btnCos , btnTan , btnLn , btnSqrt , btnLog , btnPercent ;
14
+ private Button btnXToTheYPower , btnXSquared , btnSin , btnCos , btnTan ,
15
+ btnLn , btnSqrt , btnLog , btnPercent ;
24
16
25
17
@ Override
26
18
protected void onCreate (Bundle savedInstanceState )
27
19
{
20
+ savedInstanceState = (savedInstanceState == null ) ? (new Bundle ()) : savedInstanceState ;
21
+ savedInstanceState .putBoolean ("advCalcCreation" , true );
28
22
super .onCreate (savedInstanceState );
29
- setContentView (R .layout .activity_advanced_calculator );
30
-
31
- CalculatorHandler .setContextForToastMessages (this );
32
- CalculatorHandler .setVibrator ((Vibrator ) getSystemService (Context .VIBRATOR_SERVICE ));
33
- CalculatorHandler .setValueTextView (findViewById (R .id .value_text_view ));
34
- CalculatorHandler .setOperationTextView (findViewById (R .id .operation_text_view ));
35
- CalculatorHandler .setPrevValueTextView (findViewById (R .id .previous_value_text_view ));
36
- CalculatorHandler .setNewValueFlag (false );
37
-
38
- if (savedInstanceState != null )
39
- {
40
- valueTextView .setText (savedInstanceState .getString ("valueText" ));
41
- operationTextView .setText (savedInstanceState .getString ("opText" ));
42
- prevValueTextView .setText (savedInstanceState .getString ("prevValueText" ));
43
- newValueFlag = savedInstanceState .getBoolean ("newValueFlag" );
44
- }
45
23
46
- btn0 = findViewById (R .id .btn0 );
47
- btn1 = findViewById (R .id .btn1 );
48
- btn2 = findViewById (R .id .btn2 );
49
- btn3 = findViewById (R .id .btn3 );
50
- btn4 = findViewById (R .id .btn4 );
51
- btn5 = findViewById (R .id .btn5 );
52
- btn6 = findViewById (R .id .btn6 );
53
- btn7 = findViewById (R .id .btn7 );
54
- btn8 = findViewById (R .id .btn8 );
55
- btn9 = findViewById (R .id .btn9 );
56
-
57
- btnComma = findViewById (R .id .btn_comma );
58
- btnAllClear = findViewById (R .id .btn_all_clear );
59
- btnPlus = findViewById (R .id .btn_plus );
60
- btnMinus = findViewById (R .id .btn_minus );
61
- btnMultiply = findViewById (R .id .btn_multiply );
62
- btnDivide = findViewById (R .id .btn_divide );
63
- btnEquals = findViewById (R .id .btn_equals );
64
- btnPlusMinus = findViewById (R .id .btn_plus_minus );
65
- btnClearOrClearAll = findViewById (R .id .btn_clear_or_clear_all );
66
24
btnXToTheYPower = findViewById (R .id .btn_x_to_the_y_power );
67
25
btnXSquared = findViewById (R .id .btn_x_squared );
68
26
btnSin = findViewById (R .id .btn_sin );
@@ -73,26 +31,6 @@ protected void onCreate(Bundle savedInstanceState)
73
31
btnLog = findViewById (R .id .btn_log );
74
32
btnPercent = findViewById (R .id .btn_percent );
75
33
76
- btn0 .setOnClickListener (new DigitOnClickListener (0 ));
77
- btn1 .setOnClickListener (new DigitOnClickListener (1 ));
78
- btn2 .setOnClickListener (new DigitOnClickListener (2 ));
79
- btn3 .setOnClickListener (new DigitOnClickListener (3 ));
80
- btn4 .setOnClickListener (new DigitOnClickListener (4 ));
81
- btn5 .setOnClickListener (new DigitOnClickListener (5 ));
82
- btn6 .setOnClickListener (new DigitOnClickListener (6 ));
83
- btn7 .setOnClickListener (new DigitOnClickListener (7 ));
84
- btn8 .setOnClickListener (new DigitOnClickListener (8 ));
85
- btn9 .setOnClickListener (new DigitOnClickListener (9 ));
86
-
87
- btnComma .setOnClickListener (this ::commaOnClick );
88
- btnAllClear .setOnClickListener (this ::allClearOnClick );
89
- btnPlus .setOnClickListener (new OperationOnClickListener (CHAR_PLUS ));
90
- btnMinus .setOnClickListener (new OperationOnClickListener (CHAR_MINUS ));
91
- btnMultiply .setOnClickListener (new OperationOnClickListener (CHAR_MULTIPLY ));
92
- btnDivide .setOnClickListener (new OperationOnClickListener (CHAR_DIVIDE ));
93
- btnEquals .setOnClickListener (this ::equalsOnClick );
94
- btnPlusMinus .setOnClickListener (this ::plusMinusOnClick );
95
- btnClearOrClearAll .setOnClickListener (this ::clearOrClearAllOnClick );
96
34
btnXToTheYPower .setOnClickListener (new OperationOnClickListener (CHAR_POWER ));
97
35
btnXSquared .setOnClickListener (this ::xSquaredOnClick );
98
36
btnSin .setOnClickListener (this ::sinOnClick );
@@ -104,16 +42,6 @@ protected void onCreate(Bundle savedInstanceState)
104
42
btnPercent .setOnClickListener (this ::percentOnClick );
105
43
}
106
44
107
- @ Override
108
- protected void onSaveInstanceState (Bundle outState )
109
- {
110
- super .onSaveInstanceState (outState );
111
- outState .putString ("valueText" , valueTextView .getText ().toString ());
112
- outState .putString ("opText" , operationTextView .getText ().toString ());
113
- outState .putString ("prevValueText" , prevValueTextView .getText ().toString ());
114
- outState .putBoolean ("newValueFlag" , newValueFlag );
115
- }
116
-
117
45
public void percentOnClick (View v )
118
46
{
119
47
if (!prevValueTextView .getText ().toString ().isEmpty ())
@@ -131,7 +59,14 @@ public void percentOnClick(View v)
131
59
132
60
if (formattedOutput .equals (INFINITY_SYMBOL ) || formattedOutput .equals (MINUS_NAN ) || formattedOutput .equals (NAN ))
133
61
{
134
- Toast .makeText (this , "Cannot divide by 0" , Toast .LENGTH_SHORT ).show ();
62
+ if (operationTextView .getText ().charAt (0 ) == CHAR_POWER )
63
+ {
64
+ Toast .makeText (this , "The output is too long" , Toast .LENGTH_SHORT ).show ();
65
+ }
66
+ else
67
+ {
68
+ Toast .makeText (this , "Cannot divide by 0" , Toast .LENGTH_SHORT ).show ();
69
+ }
135
70
}
136
71
else if (!isOutputTooLong (formattedOutput ))
137
72
{
@@ -277,89 +212,4 @@ private void xSquaredOnClick(View view)
277
212
278
213
makeStandardVibration ();
279
214
}
280
-
281
- private void clearOrClearAllOnClick (View view )
282
- {
283
- String valueText = valueTextView .getText ().toString ();
284
-
285
- if (valueText .equals ("0" ))
286
- {
287
- operationTextView .setText ("" );
288
- prevValueTextView .setText ("" );
289
- }
290
- else
291
- {
292
- valueTextView .setText ("0" );
293
- }
294
-
295
- makeStandardVibration ();
296
- }
297
-
298
- public void plusMinusOnClick (View v )
299
- {
300
- String valueText = valueTextView .getText ().toString ();
301
-
302
- if (valueText .charAt (0 ) == '-' )
303
- {
304
- valueText = valueText .substring (1 );
305
- }
306
- else if (!valueText .equals ("0" ))
307
- {
308
- valueText = "-" + valueText ;
309
- }
310
-
311
- valueTextView .setText (valueText );
312
-
313
- makeStandardVibration ();
314
- }
315
-
316
- public void allClearOnClick (View v )
317
- {
318
- valueTextView .setText ("0" );
319
- prevValueTextView .setText ("" );
320
- operationTextView .setText ("" );
321
-
322
- makeStandardVibration ();
323
- }
324
-
325
- public void equalsOnClick (View v )
326
- {
327
- if (!prevValueTextView .getText ().toString ().isEmpty ())
328
- {
329
- double prevValue = Double .parseDouble (prevValueTextView .getText ().toString ().replace (',' , '.' ));
330
- double currValue = Double .parseDouble (valueTextView .getText ().toString ().replace (',' , '.' ));
331
-
332
- double result = OperationOnClickListener .calculate (prevValue , currValue );
333
-
334
- String formattedOutput = DECIMAL_FORMAT .format (result ).replace ('.' , ',' );
335
-
336
- if (formattedOutput .equals (INFINITY_SYMBOL ))
337
- {
338
- Toast .makeText (this , "Cannot divide by 0" , Toast .LENGTH_SHORT ).show ();
339
- }
340
- else if (!isOutputTooLong (formattedOutput ))
341
- {
342
- prevValueTextView .setText ("" );
343
- operationTextView .setText ("" );
344
- valueTextView .setText (formattedOutput );
345
-
346
- newValueFlag = true ;
347
- }
348
- }
349
-
350
- makeStandardVibration ();
351
- }
352
-
353
- public void commaOnClick (View v )
354
- {
355
- String text = valueTextView .getText ().toString ();
356
- long numberOfDigits = text .chars ().filter (Character ::isDigit ).count ();
357
-
358
- if ((!text .contains ("," )) && (numberOfDigits > 0 ) && (numberOfDigits < VALUE_TEXT_VIEW_MAX_SIZE ))
359
- {
360
- valueTextView .setText (valueTextView .getText () + "," );
361
- }
362
-
363
- makeStandardVibration ();
364
- }
365
215
}
0 commit comments