-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCalculator.java
More file actions
164 lines (150 loc) · 6.61 KB
/
Copy pathCalculator.java
File metadata and controls
164 lines (150 loc) · 6.61 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
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Calculator {
int boardWidth = 360;
int boardHeight = 540;
Color customLightGray = new Color(212, 212, 210);
Color customDarkGray = new Color(80, 80, 80);
Color customBlack = new Color(28, 28, 28);
Color customOrange = new Color(255, 149, 0);
String[] buttonValues = {
"AC", "+/-", "%", "÷",
"7", "8", "9", "×",
"4", "5", "6", "-",
"1", "2", "3", "+",
"0", ".", "√", "="
};
String[] rightSymbols = {"÷", "×", "-", "+", "="};
String[] topSymbols = {"AC", "+/-", "%"};
JFrame frame = new JFrame("Calculator");
JLabel displayLabel = new JLabel();
JPanel displayPanel = new JPanel();
JPanel buttonsPanel = new JPanel();
//A+B, A-B, A*B, A/B
String A = "0";
String operator = null;
String B = null;
Calculator() {
// frame.setVisible(true);
frame.setSize(boardWidth, boardHeight);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
displayLabel.setBackground(customBlack);
displayLabel.setForeground(Color.white);
displayLabel.setFont(new Font("Arial", Font.PLAIN, 80));
displayLabel.setHorizontalAlignment(JLabel.RIGHT);
displayLabel.setText("0");
displayLabel.setOpaque(true);
displayPanel.setLayout(new BorderLayout());
displayPanel.add(displayLabel);
frame.add(displayPanel, BorderLayout.NORTH);
buttonsPanel.setLayout(new GridLayout(5, 4));
buttonsPanel.setBackground(customBlack);
frame.add(buttonsPanel);
for (int i = 0; i < buttonValues.length; i++) {
JButton button = new JButton();
String buttonValue = buttonValues[i];
button.setFont(new Font("Arial", Font.PLAIN, 30));
button.setText(buttonValue);
button.setFocusable(false);
button.setBorder(new LineBorder(customBlack));
if (Arrays.asList(topSymbols).contains(buttonValue)) {
button.setBackground(customLightGray);
button.setForeground(customBlack);
}
else if (Arrays.asList(rightSymbols).contains(buttonValue)) {
button.setBackground(customOrange);
button.setForeground(Color.white);
}
else {
button.setBackground(customDarkGray);
button.setForeground(Color.white);
}
buttonsPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String buttonValue = button.getText();
if (Arrays.asList(rightSymbols).contains(buttonValue)) {
if (buttonValue == "=") {
if (A != null) {
B = displayLabel.getText();
double numA = Double.parseDouble(A);
double numB = Double.parseDouble(B);
if (operator == "+") {
displayLabel.setText(removeZeroDecimal(numA+numB));
}
else if (operator == "-") {
displayLabel.setText(removeZeroDecimal(numA-numB));
}
else if (operator == "×") {
displayLabel.setText(removeZeroDecimal(numA*numB));
}
else if (operator == "÷") {
displayLabel.setText(removeZeroDecimal(numA/numB));
}
clearAll();
}
}
else if ("+-×÷".contains(buttonValue)) {
if (operator == null) {
A = displayLabel.getText();
displayLabel.setText("0");
B = "0";
}
operator = buttonValue;
}
}
else if (Arrays.asList(topSymbols).contains(buttonValue)) {
if (buttonValue == "AC") {
clearAll();
displayLabel.setText("0");
}
else if (buttonValue == "+/-") {
double numDisplay = Double.parseDouble(displayLabel.getText());
numDisplay *= -1;
displayLabel.setText(removeZeroDecimal(numDisplay));
}
else if (buttonValue == "%") {
double numDisplay = Double.parseDouble(displayLabel.getText());
numDisplay /= 100;
displayLabel.setText(removeZeroDecimal(numDisplay));
}
}
else { //digits or .
if (buttonValue == ".") {
if (!displayLabel.getText().contains(buttonValue)) {
displayLabel.setText(displayLabel.getText() + buttonValue);
}
}
else if ("0123456789".contains(buttonValue)) {
if (displayLabel.getText() == "0") {
displayLabel.setText(buttonValue);
}
else {
displayLabel.setText(displayLabel.getText() + buttonValue);
}
}
}
}
});
frame.setVisible(true);
}
}
void clearAll() {
A = "0";
operator = null;
B = null;
}
String removeZeroDecimal(double numDisplay) {
if (numDisplay % 1 == 0) {
return Integer.toString((int) numDisplay);
}
return Double.toString(numDisplay);
}
}