|
| 1 | +import javax.swing.*; |
| 2 | +import javax.swing.border.EmptyBorder; |
| 3 | +import java.awt.*; |
| 4 | +import java.awt.event.ActionEvent; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.InputStreamReader; |
| 8 | +import java.net.HttpURLConnection; |
| 9 | +import java.net.URL; |
| 10 | + |
| 11 | +public class CurrencyConverter { |
| 12 | + |
| 13 | + private JTextField amountField; |
| 14 | + private JComboBox<String> baseCurrencyComboBox; |
| 15 | + private JComboBox<String> targetCurrencyComboBox; |
| 16 | + private JLabel resultLabel; |
| 17 | + private JLabel feedbackLabel; |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + SwingUtilities.invokeLater(() -> { |
| 21 | + new CurrencyConverter().createAndShowGUI(); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + private void createAndShowGUI() { |
| 26 | + JFrame frame = new JFrame("Currency Converter"); |
| 27 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 28 | + frame.setSize(600, 400); |
| 29 | + frame.getContentPane().setBackground(new Color(54, 57, 63)); |
| 30 | + frame.setLayout(new BorderLayout()); |
| 31 | + |
| 32 | + JPanel inputPanel = new JPanel(new GridBagLayout()); |
| 33 | + inputPanel.setBorder(new EmptyBorder(30, 30, 30, 30)); |
| 34 | + inputPanel.setBackground(new Color(54, 57, 63)); |
| 35 | + GridBagConstraints gbc = new GridBagConstraints(); |
| 36 | + |
| 37 | + JLabel title = new JLabel("Currency Converter"); |
| 38 | + title.setFont(new Font("Arial", Font.BOLD, 28)); |
| 39 | + title.setForeground(Color.WHITE); |
| 40 | + gbc.gridx = 0; |
| 41 | + gbc.gridy = 0; |
| 42 | + gbc.gridwidth = 2; |
| 43 | + gbc.insets = new Insets(0, 0, 30, 0); |
| 44 | + inputPanel.add(title, gbc); |
| 45 | + |
| 46 | + JLabel amountLabel = new JLabel("Amount:"); |
| 47 | + amountLabel.setForeground(Color.WHITE); |
| 48 | + gbc.gridx = 0; |
| 49 | + gbc.gridy = 1; |
| 50 | + gbc.gridwidth = 1; |
| 51 | + gbc.insets = new Insets(0, 0, 10, 10); |
| 52 | + inputPanel.add(amountLabel, gbc); |
| 53 | + |
| 54 | + amountField = new JTextField(15); |
| 55 | + amountField.setPreferredSize(new Dimension(200, 30)); |
| 56 | + gbc.gridx = 1; |
| 57 | + gbc.gridy = 1; |
| 58 | + gbc.insets = new Insets(0, 0, 10, 0); |
| 59 | + inputPanel.add(amountField, gbc); |
| 60 | + |
| 61 | + JLabel baseCurrencyLabel = new JLabel("Base Currency:"); |
| 62 | + baseCurrencyLabel.setForeground(Color.WHITE); |
| 63 | + gbc.gridx = 0; |
| 64 | + gbc.gridy = 2; |
| 65 | + gbc.insets = new Insets(0, 0, 10, 10); |
| 66 | + inputPanel.add(baseCurrencyLabel, gbc); |
| 67 | + |
| 68 | + 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)"}); |
| 69 | + baseCurrencyComboBox.setPreferredSize(new Dimension(200, 30)); |
| 70 | + gbc.gridx = 1; |
| 71 | + gbc.gridy = 2; |
| 72 | + gbc.insets = new Insets(0, 0, 10, 0); |
| 73 | + inputPanel.add(baseCurrencyComboBox, gbc); |
| 74 | + |
| 75 | + JLabel targetCurrencyLabel = new JLabel("Target Currency:"); |
| 76 | + targetCurrencyLabel.setForeground(Color.WHITE); |
| 77 | + gbc.gridx = 0; |
| 78 | + gbc.gridy = 3; |
| 79 | + gbc.insets = new Insets(0, 0, 10, 10); |
| 80 | + inputPanel.add(targetCurrencyLabel, gbc); |
| 81 | + |
| 82 | + 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)"}); |
| 83 | + targetCurrencyComboBox.setPreferredSize(new Dimension(200, 30)); |
| 84 | + gbc.gridx = 1; |
| 85 | + gbc.gridy = 3; |
| 86 | + gbc.insets = new Insets(0, 0, 10, 0); |
| 87 | + inputPanel.add(targetCurrencyComboBox, gbc); |
| 88 | + |
| 89 | + JButton convertButton = new JButton("Convert"); |
| 90 | + convertButton.setBackground(new Color(255, 193, 7)); |
| 91 | + convertButton.setForeground(Color.BLACK); |
| 92 | + convertButton.setFocusPainted(false); |
| 93 | + convertButton.addActionListener(new ActionListener() { |
| 94 | + @Override |
| 95 | + public void actionPerformed(ActionEvent e) { |
| 96 | + convertCurrency(); |
| 97 | + } |
| 98 | + }); |
| 99 | + gbc.gridx = 1; |
| 100 | + gbc.gridy = 4; |
| 101 | + gbc.anchor = GridBagConstraints.LINE_END; |
| 102 | + inputPanel.add(convertButton, gbc); |
| 103 | + |
| 104 | + JPanel resultPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| 105 | + resultPanel.setBackground(new Color(54, 57, 63)); |
| 106 | + |
| 107 | + resultLabel = new JLabel(); |
| 108 | + resultLabel.setFont(new Font("Arial", Font.BOLD, 24)); |
| 109 | + resultLabel.setForeground(Color.WHITE); |
| 110 | + resultPanel.add(resultLabel); |
| 111 | + |
| 112 | + feedbackLabel = new JLabel(); |
| 113 | + feedbackLabel.setForeground(Color.RED); |
| 114 | + resultPanel.add(feedbackLabel); |
| 115 | + |
| 116 | + frame.add(inputPanel, BorderLayout.NORTH); |
| 117 | + frame.add(resultPanel, BorderLayout.CENTER); |
| 118 | + |
| 119 | + frame.setLocationRelativeTo(null); |
| 120 | + |
| 121 | + frame.setVisible(true); |
| 122 | + } |
| 123 | + |
| 124 | + private void convertCurrency() { |
| 125 | + try { |
| 126 | + String baseCurrency = ((String) baseCurrencyComboBox.getSelectedItem()).split("\\(")[1].split("\\)")[0].trim(); |
| 127 | + String targetCurrency = ((String) targetCurrencyComboBox.getSelectedItem()).split("\\(")[1].split("\\)")[0].trim(); |
| 128 | + double amount = Double.parseDouble(amountField.getText()); |
| 129 | + |
| 130 | + // Fetch exchange rate from API |
| 131 | + String apiUrl = "https://api.exchangerate-api.com/v4/latest/" + baseCurrency; |
| 132 | + URL url = new URL(apiUrl); |
| 133 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 134 | + connection.setRequestMethod("GET"); |
| 135 | + |
| 136 | + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| 137 | + String line; |
| 138 | + StringBuilder response = new StringBuilder(); |
| 139 | + |
| 140 | + while ((line = reader.readLine()) != null) { |
| 141 | + response.append(line); |
| 142 | + } |
| 143 | + |
| 144 | + reader.close(); |
| 145 | + connection.disconnect(); |
| 146 | + |
| 147 | + double exchangeRate = Double.parseDouble(response.toString().split("\"" + targetCurrency + "\":")[1] |
| 148 | + .split(",")[0].replace("}", "").trim()); |
| 149 | + |
| 150 | + double result = amount * exchangeRate; |
| 151 | + |
| 152 | + String currencySign = getCurrencySign(targetCurrency); |
| 153 | + |
| 154 | + resultLabel.setText(String.format("%.2f %s", result, currencySign + " " + targetCurrency)); |
| 155 | + feedbackLabel.setText(""); // Clear any previous error messages |
| 156 | + } catch (Exception ex) { |
| 157 | + resultLabel.setText(""); |
| 158 | + feedbackLabel.setText("Error converting currency. Please check your input."); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private String getCurrencySign(String currencyCode) { |
| 163 | + switch (currencyCode) { |
| 164 | + case "INR": |
| 165 | + return "₹"; |
| 166 | + case "USD": |
| 167 | + return "$"; |
| 168 | + case "EUR": |
| 169 | + return "€"; |
| 170 | + case "GBP": |
| 171 | + return "£"; |
| 172 | + case "JPY": |
| 173 | + return "¥"; |
| 174 | + case "AUD": |
| 175 | + return "A$"; |
| 176 | + case "CAD": |
| 177 | + return "C$"; |
| 178 | + case "CHF": |
| 179 | + return "CHF"; |
| 180 | + case "CNY": |
| 181 | + return "¥"; |
| 182 | + case "SEK": |
| 183 | + return "kr"; |
| 184 | + default: |
| 185 | + return ""; |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments