-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrency_converter.java
125 lines (100 loc) · 3.77 KB
/
Currency_converter.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Pester Mbhetse
*/
public class Currency_converter extends JFrame {
private JLabel amountLabel, fromLabel, toLabel, resultLabel;
private JTextField amountField;
private JComboBox<String> fromComboBox, toComboBox;
private JButton convertButton, clearButton, exitButton;
private DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
private final String[] currencies = {"USD", "EUR", "JPY", "GBP", "CAD", "AUD", "CHF", "CNY", "INR", "ZAR"};
private double[] exchangeRates = {1.00, 0.84, 109.65, 0.72, 1.27, 1.30, 0.92, 6.47, 87.14, 19.5};
public Currency_converter() {
setTitle("Currency Converter");
setLayout(new GridLayout(5, 2));
amountLabel = new JLabel("Amount:");
add(amountLabel);
amountField = new JTextField();
add(amountField);
fromLabel = new JLabel("From:");
add(fromLabel);
fromComboBox = new JComboBox<>(currencies);
add(fromComboBox);
toLabel = new JLabel("To:");
add(toLabel);
toComboBox = new JComboBox<>(currencies);
add(toComboBox);
convertButton = new JButton("Convert");
add(convertButton);
clearButton = new JButton("Clear");
add(clearButton);
exitButton = new JButton("Exit");
add(exitButton);
resultLabel = new JLabel();
add(resultLabel);
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double amount = Double.parseDouble(amountField.getText());
String fromCurrency = (String) fromComboBox.getSelectedItem();
String toCurrency = (String) toComboBox.getSelectedItem();
double exchangeRate = exchangeRates[getIndex(toCurrency)] / exchangeRates[getIndex(fromCurrency)];
double result = amount * exchangeRate;
resultLabel.setText(decimalFormat.format(result) + " " + toCurrency);
} catch (Exception ex) {
resultLabel.setText("Invalid input");
}
}
});
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearFields();
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitApplication();
}
});
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private int getIndex(String currency) {
for (int i = 0; i < currencies.length; i++) {
if (currency.equals(currencies[i])) {
return i;
}
}
return -1;
}
private void clearFields() {
amountField.setText("");
resultLabel.setText("");
}
private void exitApplication() {
System.exit(0);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Currency_converter());
}
}