-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
216 lines (179 loc) · 6.87 KB
/
Copy pathCalculator.java
File metadata and controls
216 lines (179 loc) · 6.87 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
package problema3;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class Calculator {
//dimensiunile ferestrei
int boardWidth = 360;
int boardHeight = 540;
//definim niste culori
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);
JLabel displayLabel = new JLabel(); //ecranul
JPanel displayPanel = new JPanel(); //panou
JPanel buttonsPanel = new JPanel();
//A+B,A-B,A/B,A*B
String A="0";
String operator=null;
String B=null;
String[] buttonValues = {
"AC", "+/-", "%", "÷",
"7", "8", "9", "×",
"4", "5", "6", "-",
"1", "2", "3", "+",
"0", ".", "√", "="
};
String[] rightSymbols = {"÷", "×", "-", "+", "="};
String[] topSymbols = {"AC", "+/-", "%"};
JFrame frame = new JFrame("Calculator");
Calculator(){
//frame.setVisible(true); //ca sa vedem fereastra
frame.setSize(boardWidth,boardHeight);
frame.setLocationRelativeTo(null); //centreaza fereastra
frame.setResizable(false); //comanda este pusa ca utilizatorul sa nu poata modifica dimensiunea
//ferestrei cand este deschisa tragand de marginea acestuia
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //cand da pe close programul se va termina
frame.setLayout(new BorderLayout()); //pentru a plasa componentele N/S/E/V
displayLabel.setBackground(customBlack);
displayLabel.setForeground(Color.white); //culoarea textului
displayLabel.setFont(new Font("Arial", Font.PLAIN, 80)); //seteaza fontul textului din ecranul pc-ului la Arial, normal, marimea 80 px
displayLabel.setHorizontalAlignment(JLabel.RIGHT); //afisam textul in partea dreapta
displayLabel.setText("0"); //un text default
displayLabel.setOpaque(true);
displayPanel.setLayout(new BorderLayout());
displayPanel.add(displayLabel); // punem text labelul in int panel1
frame.add(displayPanel, BorderLayout.NORTH); //adaugam panel1 in fereastra noastra
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)); // seteaza culoarea marginilor butoanelor ca fiind neagra
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() {
@Override
public void actionPerformed(ActionEvent e) { //action e va fii un click al mouse-ului
JButton button = (JButton) e.getSource(); //butonul care va fii apasat, evenimentul este un click, sursa click ului este jbutton
String buttonValue = button.getText(); // luam simbolul ca sa vedem dupa ce buton a fost apasat
if(Arrays.asList(rightSymbols).contains(buttonValue)) {
if(buttonValue.equals("=")) {
if(A != null) {
B = displayLabel.getText();
double numA = Double.parseDouble(A);
double numB = Double.parseDouble(B);
if(operator.equals("÷") && numB == 0) {
JOptionPane.showMessageDialog(frame,"Nu poti imparti la zero!","Eroare!!",JOptionPane.ERROR_MESSAGE);
clearAll();
displayLabel.setText("0");
return;
}
if(operator.equals("+")) {
displayLabel.setText(removeZeroDecimal(numA + numB));
}
else if(operator.equals("-")) {
displayLabel.setText(removeZeroDecimal(numA - numB));
}
else if(operator.equals("×")) {
displayLabel.setText(removeZeroDecimal(numA * numB));
}
else if(operator.equals("÷")) {
displayLabel.setText(removeZeroDecimal(numA / numB));
}
clearAll();
}
}
else if("+-×÷".contains(buttonValue)) {
if(operator == null) { //daca nu verific cu null utilizatorul poate apasa de 2 ori pe operator
A = displayLabel.getText();
displayLabel.setText("0");
B = "0";
}
operator = buttonValue;
}
}
else if(Arrays.asList(topSymbols).contains(buttonValue)) {
if(buttonValue.equals("AC")) {
clearAll();
displayLabel.setText("0");
}
else if(buttonValue.equals("+/-")) {
double numDisplay = Double.parseDouble(displayLabel.getText());
numDisplay *= -1;
displayLabel.setText(removeZeroDecimal(numDisplay));
}
else if(buttonValue.equals("%")) {
double numDisplay = Double.parseDouble(displayLabel.getText());
numDisplay /= 100;
displayLabel.setText(removeZeroDecimal(numDisplay));
}
}
else {
//ciferele
if (buttonValue.equals("√")) {
double numDisplay = Double.parseDouble(displayLabel.getText());
if(numDisplay < 0) {
//tratam cazul de radical din nr negativ
JOptionPane.showMessageDialog(frame,"Nu poti calcula radical dintr-un numar negativ!","Eroare!",JOptionPane.ERROR_MESSAGE);
return;
}
double result = Math.sqrt(numDisplay);
displayLabel.setText(removeZeroDecimal(result));
A=displayLabel.getText();
operator = null;
B = null;
}
else if(buttonValue.equals(".")) {
if(!displayLabel.getText().contains(buttonValue)) { //daca nu are .
displayLabel.setText(displayLabel.getText() + buttonValue);
}
}
else if("0123456789".contains(buttonValue)){
if(displayLabel.getText().equals("0")) {
displayLabel.setText(buttonValue);
}
else {
displayLabel.setText(displayLabel.getText() + buttonValue);
}
}
}
}
});
frame.setVisible(true); //ca sa vedem fereastra
}
}
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);
}
}