-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity
258 lines (229 loc) · 6.7 KB
/
MainActivity
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editTextInput;
private TextView textViewOutput,textViewLog;
private final String sAdd = "+",sSub="-",sDiv="/",sMul="X";
private Button calcButton,clearButton;
private Button addButton,subButton,divButton,mulButton;
Boolean bOperSel = false;
Boolean bJustCalc = false; //Set to true just after a calculation has occurred
enum currentOperSelected
{
NONE,
DIV,
MUL,
ADD,
SUB
}
currentOperSelected operator = currentOperSelected.NONE;
List<Button> buttonList = new ArrayList<>(4);
List<Double> calcList = new ArrayList<>();
double dTotal =0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Input box
editTextInput = findViewById(R.id.input_left);
editTextInput.addTextChangedListener(inputTextWatcher);
//Buttons
calcButton = findViewById((R.id.calculate_button));
calcButton.setOnClickListener(this);
clearButton = findViewById(R.id.clear_button);
clearButton.setOnClickListener(this);
addButton = findViewById(R.id.addition_button);
addButton.setOnClickListener((this));
buttonList.add(addButton);
subButton = findViewById(R.id.subtraction_button);
subButton.setOnClickListener(this);
buttonList.add(subButton);
divButton = findViewById((R.id.division_button));
divButton.setOnClickListener(this);
buttonList.add(divButton);
mulButton = findViewById((R.id.multiplication_button));
mulButton.setOnClickListener(this);
buttonList.add(mulButton);
//Text View
textViewOutput = findViewById(R.id.output);
textViewLog = findViewById(R.id.calculate_log);
setOperators(false);
calcButton.setEnabled(false);
clearButton.setEnabled(false);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.addition_button: {
add();
break;
}
case R.id.subtraction_button: {
subtract();
break;
}
case R.id.division_button:{
divide();
break;
}
case R.id.multiplication_button:{
multiply();
break;
}
case R.id.calculate_button:{
calculate();
break;
}
case R.id.clear_button: {
clear();
break;
}
}
}
private TextWatcher inputTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (count > 0 && !bOperSel) { setOperators(true); }
}
@Override
public void afterTextChanged(Editable s) {
}
};
void add()
{
operator = currentOperSelected.ADD;
getInputFromUser(sAdd);
}
void subtract()
{
operator = currentOperSelected.SUB;
getInputFromUser(sSub);
}
void multiply()
{
operator = currentOperSelected.MUL;
getInputFromUser(sMul);
}
void divide()
{
operator = currentOperSelected.DIV;
getInputFromUser(sDiv);
}
void calculate()
{
getInputFromUser("");
if (operator == currentOperSelected.DIV)
{
if (calcList.size() > 2)
{
dTotal /= calcList.get(calcList.size()-1);
}
else {
dTotal = calcList.get(0) / calcList.get(1);
}
}
else if (operator == currentOperSelected.MUL)
{
if (calcList.size() > 2)
{
dTotal *= calcList.get(calcList.size()-1);
}
else {
dTotal = calcList.get(0) * calcList.get(1);
}
}
else if (operator == currentOperSelected.ADD)
{
if (calcList.size() > 2)
{
dTotal += calcList.get(calcList.size()-1);
}
else {
dTotal = calcList.get(0) + calcList.get(1);
}
}
else if (operator == currentOperSelected.SUB)
{
if (calcList.size() > 2)
{
dTotal -= calcList.get(calcList.size()-1);
}
else {
dTotal = calcList.get(0) - calcList.get(1);
}
}
if (calcList.size() > 2)
{
clearButton.setEnabled(true);
}
setOperators(true);
bJustCalc = true;
editTextInput.setEnabled(false);
String sTotal = Double.toString(dTotal);
textViewOutput.setText(sTotal);
operator = currentOperSelected.NONE;
}
void setOperators(Boolean bVal)
{
for (Button b: buttonList)
{
b.setEnabled(bVal);
}
if (bVal)
{
calcButton.setEnabled(false);
}
else {
calcButton.setEnabled(true);
}
}
void clear()
{
setOperators(false);
dTotal = 0.0;
operator = currentOperSelected.NONE;
calcButton.setEnabled(false);
calcList.clear();
bOperSel = false;
bJustCalc = false;
textViewOutput.setText("");
textViewLog.setText("");
editTextInput.setText("");
if (!editTextInput.isEnabled()) {
editTextInput.setEnabled(true);
}
}
void getInputFromUser(String sOperator)
{
if (!editTextInput.isEnabled()){
editTextInput.setEnabled(true);
}
if (!bJustCalc)
{
String sUserInput = editTextInput.getText().toString();
double val = Double.parseDouble(sUserInput);
textViewLog.setText(textViewLog.getText()+sUserInput);
calcList.add(val);
editTextInput.setText("");
}
textViewLog.setText(textViewLog.getText()+sOperator);
bOperSel = true;
setOperators(false);
bJustCalc = false;
}
}