-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
191 lines (160 loc) · 7.03 KB
/
App.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
// Currency Convertor Code
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class App {
private JTextField amountField;
private JComboBox<String> baseCurrencyComboBox;
private JComboBox<String> targetCurrencyComboBox;
private JLabel resultLabel;
private JLabel feedbackLabel;
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(() -> {
new App().createAndShowGUI();
});
}
private void createAndShowGUI() {
JFrame frame = new JFrame("Currency Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.getContentPane().setBackground(new Color(54, 57, 63));
frame.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel(new GridBagLayout());
inputPanel.setBorder(new EmptyBorder(30, 30, 30, 30));
inputPanel.setBackground(new Color(54, 57, 63));
GridBagConstraints gbc = new GridBagConstraints();
JLabel title = new JLabel("Currency Converter");
title.setFont(new Font("Arial", Font.BOLD, 28));
title.setForeground(Color.WHITE);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.insets = new Insets(0, 0, 30, 0);
inputPanel.add(title, gbc);
JLabel amountLabel = new JLabel("Amount:");
amountLabel.setForeground(Color.WHITE);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.insets = new Insets(0, 0, 10, 10);
inputPanel.add(amountLabel, gbc);
amountField = new JTextField(15);
amountField.setPreferredSize(new Dimension(200, 30));
gbc.gridx = 1;
gbc.gridy = 1;
gbc.insets = new Insets(0, 0, 10, 0);
inputPanel.add(amountField, gbc);
JLabel baseCurrencyLabel = new JLabel("Base Currency:");
baseCurrencyLabel.setForeground(Color.WHITE);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets = new Insets(0, 0, 10, 10);
inputPanel.add(baseCurrencyLabel, gbc);
baseCurrencyComboBox = new JComboBox<>(new String[]{"Indian Rupee (INR)", "US Dollar (USD)", "Euro (EUR)", "British Pound (GBP)", "Japanese Yen (JPY)", "Australian Dollar (AUD)", "Canadian Dollar (CAD)", "Swiss Franc (CHF)", "Chinese Yuan (CNY)", "Swedish Krona (SEK)"});
baseCurrencyComboBox.setPreferredSize(new Dimension(200, 30));
gbc.gridx = 1;
gbc.gridy = 2;
gbc.insets = new Insets(0, 0, 10, 0);
inputPanel.add(baseCurrencyComboBox, gbc);
JLabel targetCurrencyLabel = new JLabel("Target Currency:");
targetCurrencyLabel.setForeground(Color.WHITE);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets = new Insets(0, 0, 10, 10);
inputPanel.add(targetCurrencyLabel, gbc);
targetCurrencyComboBox = new JComboBox<>(new String[]{"Indian Rupee (INR)", "US Dollar (USD)", "Euro (EUR)", "British Pound (GBP)", "Japanese Yen (JPY)", "Australian Dollar (AUD)", "Canadian Dollar (CAD)", "Swiss Franc (CHF)", "Chinese Yuan (CNY)", "Swedish Krona (SEK)"});
targetCurrencyComboBox.setPreferredSize(new Dimension(200, 30));
gbc.gridx = 1;
gbc.gridy = 3;
gbc.insets = new Insets(0, 0, 10, 0);
inputPanel.add(targetCurrencyComboBox, gbc);
JButton convertButton = new JButton("Convert");
convertButton.setBackground(new Color(255, 193, 7));
convertButton.setForeground(Color.BLACK);
convertButton.setFocusPainted(false);
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
convertCurrency();
}
});
gbc.gridx = 1;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_END;
inputPanel.add(convertButton, gbc);
JPanel resultPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
resultPanel.setBackground(new Color(54, 57, 63));
resultLabel = new JLabel();
resultLabel.setFont(new Font("Arial", Font.BOLD, 24));
resultLabel.setForeground(Color.WHITE);
resultPanel.add(resultLabel);
feedbackLabel = new JLabel();
feedbackLabel.setForeground(Color.RED);
resultPanel.add(feedbackLabel);
frame.add(inputPanel, BorderLayout.NORTH);
frame.add(resultPanel, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void convertCurrency() {
try {
String baseCurrency = ((String) baseCurrencyComboBox.getSelectedItem()).split("\\(")[1].split("\\)")[0].trim();
String targetCurrency = ((String) targetCurrencyComboBox.getSelectedItem()).split("\\(")[1].split("\\)")[0].trim();
double amount = Double.parseDouble(amountField.getText());
// Fetch exchange rate from API
String apiUrl = "https://api.exchangerate-api.com/v4/latest/" + baseCurrency;
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
connection.disconnect();
double exchangeRate = Double.parseDouble(response.toString().split("\"" + targetCurrency + "\":")[1]
.split(",")[0].replace("}", "").trim());
double result = amount * exchangeRate;
String currencySign = getCurrencySign(targetCurrency);
resultLabel.setText(String.format("%.2f %s", result, currencySign + " " + targetCurrency));
feedbackLabel.setText(""); // Clear any previous error messages
} catch (Exception ex) {
resultLabel.setText("");
feedbackLabel.setText("Error converting currency. Please check your input.");
}
}
private String getCurrencySign(String currencyCode) {
switch (currencyCode) {
case "INR":
return "₹";
case "USD":
return "$";
case "EUR":
return "€";
case "GBP":
return "£";
case "JPY":
return "¥";
case "AUD":
return "A$";
case "CAD":
return "C$";
case "CHF":
return "CHF";
case "CNY":
return "¥";
case "SEK":
return "kr";
default:
return "";
}
}
}