-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatrice.java
560 lines (459 loc) · 16.8 KB
/
Calculatrice.java
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package Scientific_Calculators;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.DefaultComboBoxModel;
public class ScientificCalculator {
private JFrame frmStandardCalculator;
private JTextField txtDisplay;
private JTextField jtxtConverts;
private JTextField jlblConverts;
double firstnum;
double secondnum;
double result;
String operations;
String answer;
double[] i = new double[5];
double Nigerian_Naira = 5.20;
double US_Dollar = 0.014;
double Kenyan_Shilling = 1.46;
double Brazilian_Real = 0.056;
double Canadian_Dollar = 0.019;
double Indian_Rupee = 1.00;
double Philippine_Peso = 0.75;
double Indonessia_Rupiah = 203.74;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ScientificCalculator window = new ScientificCalculator();
window.frmStandardCalculator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ScientificCalculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmStandardCalculator = new JFrame();
frmStandardCalculator.setResizable(false);
frmStandardCalculator.setTitle("Standard Calculator");
frmStandardCalculator.setBounds(700, 300, 280, 385);
frmStandardCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frmStandardCalculator.setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmStandard = new JMenuItem("Standard");
mntmStandard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frmStandardCalculator.setTitle("Standard Calculator");
frmStandardCalculator.setBounds(700, 300, 280, 385);
txtDisplay.setBounds(10, 11, 243, 37);
}
});
mnFile.add(mntmStandard);
JMenuItem mntmCurrencyConverter = new JMenuItem("Currency Converter");
mntmCurrencyConverter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frmStandardCalculator.setTitle("Currency Converter");
frmStandardCalculator.setBounds(600, 300, 625, 380);
txtDisplay.setBounds(10, 11, 243, 37);
}
});
mnFile.add(mntmCurrencyConverter);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mnFile.add(mntmExit);
frmStandardCalculator.getContentPane().setLayout(null);
txtDisplay = new JTextField();
txtDisplay.setHorizontalAlignment(SwingConstants.RIGHT);
txtDisplay.setBounds(10, 11, 243, 37);
frmStandardCalculator.getContentPane().add(txtDisplay);
txtDisplay.setColumns(10);
JButton button = new JButton("\u00AB");
button.setFont(new Font("Tahoma", Font.BOLD, 15));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String backspace = null;
if (txtDisplay.getText().length() >0) {
StringBuilder strB = new StringBuilder(txtDisplay.getText());
strB.deleteCharAt(txtDisplay.getText().length() -1);
backspace = strB.toString();
txtDisplay.setText(backspace);
}
}
});
button.setBounds(11, 57, 46, 47);
frmStandardCalculator.getContentPane().add(button);
JButton button_1 = new JButton("\u0152");
button_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtDisplay.setText(null);
}
});
button_1.setBounds(59, 57, 46, 47);
frmStandardCalculator.getContentPane().add(button_1);
JButton button_2 = new JButton("C");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtDisplay.setText(null);
}
});
button_2.setFont(new Font("Tahoma", Font.BOLD, 11));
button_2.setBounds(108, 57, 46, 47);
frmStandardCalculator.getContentPane().add(button_2);
JButton btnPlusMinus = new JButton("\u00B1");
btnPlusMinus.setFont(new Font("Tahoma", Font.BOLD, 11));
btnPlusMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double ops = Double.parseDouble(String.valueOf(txtDisplay.getText()));
ops = ops * (-1);
txtDisplay.setText(String.valueOf(ops));
}
});
btnPlusMinus.setBounds(156, 57, 46, 47);
frmStandardCalculator.getContentPane().add(btnPlusMinus);
JButton btnSqrt = new JButton("\u221A");
btnSqrt.setFont(new Font("Tahoma", Font.BOLD, 11));
btnSqrt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double ops = Double.parseDouble(String.valueOf(txtDisplay.getText()));
ops = Math.sqrt(ops);
txtDisplay.setText(String.valueOf(ops));
}
});
btnSqrt.setBounds(205, 57, 46, 47);
frmStandardCalculator.getContentPane().add(btnSqrt);
JButton btn7 = new JButton("7");
btn7.setFont(new Font("Tahoma", Font.BOLD, 11));
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn7.getText();
txtDisplay.setText(iNum);
}
});
btn7.setBounds(11, 105, 46, 47);
frmStandardCalculator.getContentPane().add(btn7);
JButton btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn8.getText();
txtDisplay.setText(iNum);
}
});
btn8.setFont(new Font("Tahoma", Font.BOLD, 11));
btn8.setBounds(59, 105, 46, 47);
frmStandardCalculator.getContentPane().add(btn8);
JButton btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn9.getText();
txtDisplay.setText(iNum);
}
});
btn9.setFont(new Font("Tahoma", Font.BOLD, 11));
btn9.setBounds(108, 105, 46, 47);
frmStandardCalculator.getContentPane().add(btn9);
JButton btnDiv = new JButton("/");
btnDiv.setFont(new Font("Tahoma", Font.BOLD, 11));
btnDiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operations = "/";
}
});
btnDiv.setBounds(156, 105, 46, 47);
frmStandardCalculator.getContentPane().add(btnDiv);
JButton btnRMod = new JButton("%");
btnRMod.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnRMod.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operations = "%";
}
});
btnRMod.setBounds(205, 105, 46, 47);
frmStandardCalculator.getContentPane().add(btnRMod);
JButton btn4 = new JButton("4");
btn4.setFont(new Font("Tahoma", Font.BOLD, 11));
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn4.getText();
txtDisplay.setText(iNum);
}
});
btn4.setBounds(11, 153, 46, 47);
frmStandardCalculator.getContentPane().add(btn4);
JButton btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn5.getText();
txtDisplay.setText(iNum);
}
});
btn5.setFont(new Font("Tahoma", Font.BOLD, 11));
btn5.setBounds(58, 153, 46, 47);
frmStandardCalculator.getContentPane().add(btn5);
JButton btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn6.getText();
txtDisplay.setText(iNum);
}
});
btn6.setFont(new Font("Tahoma", Font.BOLD, 11));
btn6.setBounds(107, 153, 46, 47);
frmStandardCalculator.getContentPane().add(btn6);
JButton btnMul = new JButton("*");
btnMul.setFont(new Font("Tahoma", Font.BOLD, 11));
btnMul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operations = "*";
}
});
btnMul.setBounds(156, 153, 46, 47);
frmStandardCalculator.getContentPane().add(btnMul);
JButton btnx2 = new JButton("X^2");
btnx2.setFont(new Font("Tahoma", Font.PLAIN, 6));
btnx2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double ops = Double.parseDouble(String.valueOf(txtDisplay.getText()));
ops = (ops * ops);
txtDisplay.setText(String.valueOf(ops));
}
});
btnx2.setBounds(205, 153, 46, 47);
frmStandardCalculator.getContentPane().add(btnx2);
JButton btn1 = new JButton("1");
btn1.setFont(new Font("Tahoma", Font.BOLD, 11));
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn1.getText();
txtDisplay.setText(iNum);
}
});
btn1.setBounds(11, 202, 46, 47);
frmStandardCalculator.getContentPane().add(btn1);
JButton btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn2.getText();
txtDisplay.setText(iNum);
}
});
btn2.setFont(new Font("Tahoma", Font.BOLD, 11));
btn2.setBounds(58, 202, 46, 47);
frmStandardCalculator.getContentPane().add(btn2);
JButton btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn3.getText();
txtDisplay.setText(iNum);
}
});
btn3.setFont(new Font("Tahoma", Font.BOLD, 11));
btn3.setBounds(107, 202, 46, 47);
frmStandardCalculator.getContentPane().add(btn3);
JButton btnMinus = new JButton("-");
btnMinus.setFont(new Font("Tahoma", Font.BOLD, 15));
btnMinus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operations = "-";
}
});
btnMinus.setBounds(156, 202, 46, 47);
frmStandardCalculator.getContentPane().add(btnMinus);
JButton btn0 = new JButton("0");
btn0.setFont(new Font("Tahoma", Font.BOLD, 11));
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String iNum = txtDisplay.getText() +btn0.getText();
txtDisplay.setText(iNum);
}
});
btn0.setBounds(10, 252, 94, 47);
frmStandardCalculator.getContentPane().add(btn0);
JButton btnEquals = new JButton("=");
btnEquals.setFont(new Font("Tahoma", Font.BOLD, 11));
btnEquals.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String answer;
secondnum = Double.parseDouble(txtDisplay.getText());
if (operations == "+")
{
result = firstnum + secondnum;
answer = String.format("%.2f",result);
txtDisplay.setText(answer);
}
else if (operations == "-")
{
result = firstnum - secondnum;
answer = String.format("%.2f",result);
txtDisplay.setText(answer);
}
else if (operations == "*")
{
result = firstnum * secondnum;
answer = String.format("%.2f",result);
txtDisplay.setText(answer);
}
else if (operations == "/")
{
result = firstnum / secondnum;
answer = String.format("%.2f",result);
txtDisplay.setText(answer);
}
}
});
btnEquals.setBounds(205, 202, 46, 97);
frmStandardCalculator.getContentPane().add(btnEquals);
JButton btnDot = new JButton(".");
btnDot.setFont(new Font("Tahoma", Font.BOLD, 11));
btnDot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(! txtDisplay.getText().contains("."))
{
txtDisplay.setText(txtDisplay.getText() + btnDot.getText());
}
}
});
btnDot.setBounds(108, 252, 46, 47);
frmStandardCalculator.getContentPane().add(btnDot);
JButton btnPlus = new JButton("+");
btnPlus.setFont(new Font("Tahoma", Font.BOLD, 11));
btnPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
firstnum = Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operations = "+";
}
});
btnPlus.setBounds(156, 252, 46, 47);
frmStandardCalculator.getContentPane().add(btnPlus);
JLabel lblNewLabel = new JLabel("Convert Indian Currency To");
lblNewLabel.setFont(new Font("Comic Sans MS", Font.BOLD | Font.ITALIC, 21));
lblNewLabel.setBounds(296, 22, 298, 37);
frmStandardCalculator.getContentPane().add(lblNewLabel);
JComboBox jcmbCurrencys = new JComboBox();
jcmbCurrencys.setFont(new Font("Comic Sans MS", Font.BOLD | Font.ITALIC, 14));
jcmbCurrencys.setModel(new DefaultComboBoxModel(new String[] {"-Select One Option-", "India", "USA", "Nigeria", "Kenyan", "Canada", "Brazil", "Indonessia", "Philippine"}));
jcmbCurrencys.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
jcmbCurrencys.setBounds(345, 70, 176, 34);
frmStandardCalculator.getContentPane().add(jcmbCurrencys);
jtxtConverts = new JTextField();
jtxtConverts.setHorizontalAlignment(SwingConstants.RIGHT);
jtxtConverts.setBounds(345, 115, 176, 47);
frmStandardCalculator.getContentPane().add(jtxtConverts);
jtxtConverts.setColumns(10);
jlblConverts = new JTextField();
jlblConverts.setHorizontalAlignment(SwingConstants.RIGHT);
jlblConverts.setEditable(false);
jlblConverts.setBounds(345, 181, 176, 47);
frmStandardCalculator.getContentPane().add(jlblConverts);
jlblConverts.setColumns(10);
JButton btnConverts = new JButton("Convert");
btnConverts.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double Indian_Rupees = Double.parseDouble(jtxtConverts.getText());
if (jcmbCurrencys.getSelectedItem().equals("Nigeria"))
{
String cConvert1 = String.format("N %.2f", Indian_Rupees * Nigerian_Naira);
jlblConverts.setText(cConvert1);
}
if (jcmbCurrencys.getSelectedItem().equals("USA"))
{
String cConvert1 = String.format("$ %.2f", Indian_Rupees * US_Dollar);
jlblConverts.setText(cConvert1);
}
if (jcmbCurrencys.getSelectedItem().equals("Kenyan"))
{
String cConvert2 = String.format("KS %.2f", Indian_Rupees * Kenyan_Shilling);
jlblConverts.setText(cConvert2);
}
if (jcmbCurrencys.getSelectedItem().equals("Canada"))
{
String cConvert4 = String.format("C$ %.2f", Indian_Rupees * Canadian_Dollar);
jlblConverts.setText(cConvert4);
}
if (jcmbCurrencys.getSelectedItem().equals("Brazil"))
{
String cConvert5 = String.format("Brz %.2f", Indian_Rupees * Brazilian_Real);
jlblConverts.setText(cConvert5);
}
if (jcmbCurrencys.getSelectedItem().equals("India"))
{
String cConvert6 = String.format("INR %.2f", Indian_Rupees * Indian_Rupee);
jlblConverts.setText(cConvert6);
}
if (jcmbCurrencys.getSelectedItem().equals("Philippine"))
{
String cConvert7 = String.format("PHP %.2f", Indian_Rupees * Philippine_Peso);
jlblConverts.setText(cConvert7);
}
if (jcmbCurrencys.getSelectedItem().equals("Indonessia"))
{
String cConvert8 = String.format("IDR %.2f", Indian_Rupees * Indonessia_Rupiah);
jlblConverts.setText(cConvert8);
}
}
});
btnConverts.setFont(new Font("Comic Sans MS", Font.BOLD | Font.ITALIC, 15));
btnConverts.setBounds(326, 253, 104, 34);
frmStandardCalculator.getContentPane().add(btnConverts);
JButton btnClear = new JButton("Clear");
btnClear.setFont(new Font("Comic Sans MS", Font.BOLD | Font.ITALIC, 15));
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtxtConverts.setText(null);
jlblConverts.setText(null);
jcmbCurrencys.setSelectedItem(" -Select One Option-");
}
});
btnClear.setBounds(435, 253, 104, 34);
frmStandardCalculator.getContentPane().add(btnClear);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 255), 7));
panel.setBounds(276, 11, 318, 288);
frmStandardCalculator.getContentPane().add(panel);
}
}