-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_Beauty_Calculator.java
More file actions
225 lines (196 loc) · 7.85 KB
/
Java_Beauty_Calculator.java
File metadata and controls
225 lines (196 loc) · 7.85 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
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
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import javax.swing.*;
public class Java_Beauty_Calculator extends JFrame {
private final JTextField display = new JTextField("0");
// Calculator state
private BigDecimal current = BigDecimal.ZERO; // accumulated value
private BigDecimal entry = BigDecimal.ZERO; // currently typed number
private String pendingOp = null; // "+", "-", "×", "÷"
private boolean entering = false; // true if user is typing digits
private boolean justEvaluated = false; // for chaining logic
private final MathContext MC = new MathContext(16, RoundingMode.HALF_UP);
// Constructor
public Java_Beauty_Calculator() {
super("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(320, 460);
setLocationRelativeTo(null);
setLayout(new BorderLayout(8, 8));
// Display
display.setEditable(false);
display.setHorizontalAlignment(SwingConstants.RIGHT);
display.setFont(new Font("SF Pro Display", Font.PLAIN, 32));
display.setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
add(display, BorderLayout.NORTH);
// Buttons
String[] keys = {
"C","⌫","%","÷",
"7","8","9","×",
"4","5","6","−",
"1","2","3","+",
"±","0",".","="
};
JPanel grid = new JPanel(new GridLayout(5,4,8,8));
for (String k : keys) {
JButton b = new JButton(k);
b.setFont(new Font("Inter", Font.PLAIN, 22));
b.setFocusPainted(false);
if ("÷×−+".contains(k)) b.setBackground(new Color(245,245,255));
if ("=".equals(k)) { b.setBackground(new Color(65, 105, 225)); b.setForeground(Color.WHITE); }
b.addActionListener(e -> onKey(k));
grid.add(b);
}
grid.setBorder(BorderFactory.createEmptyBorder(0, 12, 12, 12));
add(grid, BorderLayout.CENTER);
// Keyboard support
setupKeyBindings(grid);
setVisible(true);
updateDisplay(entry);
}
private void setupKeyBindings(JPanel grid) {
JComponent root = (JComponent) getContentPane();
int WHEN = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
bind(root, WHEN, "0", KeyStroke.getKeyStroke('0'), () -> onKey("0"));
// ✅ FIXED: capture final variable for lambda
for (char c = '1'; c <= '9'; c++) {
final char key = c;
bind(root, WHEN, String.valueOf(key), KeyStroke.getKeyStroke(key),
() -> onKey(String.valueOf(key)));
}
bind(root, WHEN, "dot", KeyStroke.getKeyStroke('.'), () -> onKey("."));
bind(root, WHEN, "plus", KeyStroke.getKeyStroke('+'), () -> onKey("+"));
bind(root, WHEN, "minus", KeyStroke.getKeyStroke('-'), () -> onKey("−"));
bind(root, WHEN, "mul*", KeyStroke.getKeyStroke('*'), () -> onKey("×"));
bind(root, WHEN, "div/", KeyStroke.getKeyStroke('/'), () -> onKey("÷"));
bind(root, WHEN, "enter", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), () -> onKey("="));
bind(root, WHEN, "eq", KeyStroke.getKeyStroke('='), () -> onKey("="));
bind(root, WHEN, "esc", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), () -> onKey("C"));
bind(root, WHEN, "back", KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), () -> onKey("⌫"));
}
private void bind(JComponent c, int when, String name, KeyStroke ks, Runnable action) {
c.getInputMap(when).put(ks, name);
c.getActionMap().put(name, new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) { action.run(); }
});
}
private void onKey(String k) {
switch (k) {
case "0","1","2","3","4","5","6","7","8","9" -> typeDigit(k.charAt(0));
case "." -> typeDot();
case "+" , "−" , "×" , "÷" -> chooseOperator(k);
case "=" -> evaluate();
case "C" -> clearAll();
case "⌫" -> backspace();
case "±" -> toggleSign();
case "%" -> percent();
}
}
private void typeDigit(char d) {
if (!entering || justEvaluated) {
entry = BigDecimal.ZERO;
entering = true;
justEvaluated = false;
}
String s = entry.stripTrailingZeros().toPlainString();
if ("0".equals(s)) s = "";
s += d;
entry = new BigDecimal(s);
updateDisplay(entry);
}
private void typeDot() {
String s = entering ? entry.stripTrailingZeros().toPlainString() : "0";
if (!s.contains(".")) {
if (!entering || justEvaluated) { s = "0"; entering = true; justEvaluated = false; }
s += ".";
entry = new BigDecimal(s.equals(".") ? "0." : s);
display.setText(s);
}
}
private void chooseOperator(String op) {
if (pendingOp == null) {
current = entering ? entry : current;
} else {
if (entering) current = compute(current, entry, pendingOp);
}
pendingOp = op;
entering = false;
justEvaluated = false;
updateDisplay(current);
}
private void evaluate() {
if (pendingOp != null) {
current = compute(current, entering ? entry : current, pendingOp);
pendingOp = null;
entering = false;
justEvaluated = true;
updateDisplay(current);
} else {
updateDisplay(entering ? entry : current);
}
}
private BigDecimal compute(BigDecimal a, BigDecimal b, String op) {
try {
return switch (op) {
case "+" -> a.add(b, MC);
case "−" -> a.subtract(b, MC);
case "×" -> a.multiply(b, MC);
case "÷" -> {
if (b.compareTo(BigDecimal.ZERO) == 0) { showError("Cannot divide by zero"); yield a; }
yield a.divide(b, MC);
}
default -> b;
};
} catch (ArithmeticException ex) {
showError(ex.getMessage());
return a;
}
}
private void backspace() {
if (!entering) return;
String s = entry.stripTrailingZeros().toPlainString();
if (s.endsWith(".0")) s = s.substring(0, s.length()-2);
if (s.length() > 0) s = s.substring(0, s.length()-1);
if (s.isEmpty() || s.equals("-")) { entry = BigDecimal.ZERO; entering = false; }
else entry = new BigDecimal(s);
updateDisplay(entry);
}
private void toggleSign() {
if (entering) entry = entry.negate(MC);
else current = current.negate(MC);
updateDisplay(entering ? entry : current);
}
private void percent() {
if (pendingOp != null && entering) {
entry = current.multiply(entry, MC).divide(new BigDecimal("100"), MC);
updateDisplay(entry);
} else {
entry = entry.divide(new BigDecimal("100"), MC);
entering = true;
updateDisplay(entry);
}
}
private void clearAll() {
current = BigDecimal.ZERO;
entry = BigDecimal.ZERO;
pendingOp = null;
entering = false;
justEvaluated = false;
updateDisplay(entry);
}
private void updateDisplay(BigDecimal v) {
String s = v.stripTrailingZeros().toPlainString();
if (s.equals("-0")) s = "0";
display.setText(s);
}
private void showError(String msg) {
JOptionPane.showMessageDialog(this, msg, "Error", JOptionPane.ERROR_MESSAGE);
}
// Main method
public static void main(String[] args) {
SwingUtilities.invokeLater(Java_Beauty_Calculator::new);
}
}