Java Swing AWT Program to Build Currency Converter Using JSON API GUI Desktop App Full Project For Beginners

 

 

currencyconverter.java

 

 

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.json.JSONArray;
import org.json.JSONException;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;


@SuppressWarnings("serial")
public class Converter extends JFrame {

	private JPanel contentPane;
	private HttpURLConnection con;
	private JTextField textField;
	private JSONArray arr;
	@SuppressWarnings("rawtypes")
	private JComboBox comboOne;
	@SuppressWarnings("rawtypes")
	private JComboBox comboTwo;
	private JButton btnPreracunaj;
	private JButton btnUKune;
	private JLabel status;
	private StringBuilder response;
	private double valutaDoubleOne;
	private double doubleValue;
	private String inputLine;
	private String valutaOne;
	private String vrijednostOne;
	private String valutaTwo;
	private String vrijednostTwo;
	private String [] drzave = {"AUD","CAD","CZK","DKK","HUF","JPY","NOK","SEK","CHF","GBP","USD","BAM","EUR","PLN"};
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Converter frame = new Converter();
					frame.setVisible(true);
					frame.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Converter() throws Exception {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 200);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblIzValute = new JLabel("Iz valute:");
		lblIzValute.setBounds(28, 38, 70, 15);
		contentPane.add(lblIzValute);
		
		comboOne = new JComboBox(drzave);
		comboOne.setBounds(141, 33, 85, 24);
		contentPane.add(comboOne);
		
		JLabel lblUValutu = new JLabel("U valutu:");
		lblUValutu.setBounds(28, 85, 70, 15);
		contentPane.add(lblUValutu);
		
		comboTwo = new JComboBox(drzave);
		comboTwo.setBounds(141, 80, 85, 24);
		contentPane.add(comboTwo);
		
		textField = new JTextField();
		textField.setBounds(238, 36, 70, 19);
		contentPane.add(textField);
		textField.setColumns(10);
		
		btnPreracunaj = new JButton("U valutu");
		btnPreracunaj.setBounds(254, 80, 114, 25);
		contentPane.add(btnPreracunaj);
		
		status = new JLabel("");
		status.setBounds(28, 132, 324, 40);
		contentPane.add(status);
		
		btnUKune = new JButton("U kune");
		btnUKune.setBounds(320, 33, 98, 25);
		contentPane.add(btnUKune);
			
		try {	
			//opening connection -URL
			URL obj = new URL("http://api.hnb.hr/tecajn/v1");
			con = (HttpURLConnection) obj.openConnection();
			}
		catch (IOException e) {
			e.printStackTrace();
			}
			//getting input stream 
		try (InputStreamReader  sr =new InputStreamReader(con.getInputStream());
			 BufferedReader in = new BufferedReader(sr)){ 		
			//double responseCode = conn.getResponseCode();
			response = new StringBuilder();
			//loop adds input to stringBuilder
			while ((inputLine = in.readLine()) != null) {
					response.append(inputLine);	
					arr = new JSONArray(response.toString());
			}}
		catch (IOException e) {
			System.out.println("Not");
			}
			//JButton for calculation
		btnPreracunaj.addActionListener (evtRacunaj -> {
		try {
			//getting index from comboBoxes
			int indexOne = comboOne.getSelectedIndex();
			int indexTwo = comboTwo.getSelectedIndex();
			//getting data from JSONArray for state and currency rate		
			valutaOne = arr.getJSONObject(indexOne).getString("Valuta");
			vrijednostOne = arr.getJSONObject(indexOne).getString("Srednji za devize");
			valutaTwo = arr.getJSONObject(indexTwo).getString("Valuta");
			vrijednostTwo = arr.getJSONObject(indexTwo).getString("Srednji za devize");
			//solving the problem in syntax, replacing comma for dot			
			String vrijednostOneOne = vrijednostOne.replaceFirst(",", ".");
			String vrijednostTwoTwo = vrijednostTwo.replaceFirst(",", ".");
			String textValue = textField.getText();
			//parsing the value from string to double			
			doubleValue = Double.parseDouble(textValue);						
			valutaDoubleOne = Double.parseDouble(vrijednostOneOne);
			double valutaDoubleTwo = Double.parseDouble(vrijednostTwoTwo);
			//showing results- written just to check if everything is working properly 			
			System.out.println(valutaOne);
			System.out.println(valutaDoubleOne);
			System.out.println(valutaTwo);
			System.out.println(valutaDoubleTwo);
			//simple calculation logic 			
			double sum = valutaDoubleOne*doubleValue;
			double finishSum = sum/valutaDoubleTwo;
			//solving the problem with too many decimals-setting only two decimals
			DecimalFormat df = new DecimalFormat("#.00");
			String formated = (df.format(finishSum));
			//showing final result - written just to see if everything is working as it should  			
			System.out.println(finishSum);
			//showing the result on user interface - text + value
			status.setText("Rezultat:    "+textValue+" "+valutaOne+" = "+ formated+" "+valutaTwo);		
			}	
		catch (JSONException e1) {
			e1.printStackTrace();
				}
			});	
			//JButton for calculating values into croatian currency
			//written this way because in comboBoxes are values added from simple array, not from JSONArray
			//Will be corrected in a couple of days, its matter of time now.
		btnUKune.addActionListener (evtKune -> {
			//getting index from comboBox	
			int indexOne = comboOne.getSelectedIndex();
							
		try {
			//getting values from JSONArray
			valutaOne = arr.getJSONObject(indexOne).getString("Valuta");
			vrijednostOne = arr.getJSONObject(indexOne).getString("Srednji za devize");
			//removing the comma from JSON and putting dot instead	
			String vrijednostOneOne = vrijednostOne.replaceFirst(",", ".");
			//getting text from jtextField
			String textValue = textField.getText();
			//parsing the string value into double	
			doubleValue = Double.parseDouble(textValue);						
			valutaDoubleOne = Double.parseDouble(vrijednostOneOne);
			//simple calculation, formating double for two decimals and setting the value on GUI				
			double finishSum2 = valutaDoubleOne*doubleValue;
			DecimalFormat df = new DecimalFormat("#.00");
			String formated = (df.format(finishSum2));
			status.setText("Rezultat:    "+textValue+" "+valutaOne+" = "+formated+" KN");
			} 
		catch (JSONException e) {
				e.printStackTrace();
			}
			});
	}}
			
			//This app is made by Zoran Bajcer

			//This app is not finished yet :)

Leave a Reply