|
| 1 | +package com.thealgorithms.conversions; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.net.HttpURLConnection; |
| 6 | +import java.net.URL; |
| 7 | +import java.util.Scanner; |
| 8 | + |
| 9 | +public class USDtoIND { |
| 10 | + |
| 11 | + // ✅ Free, keyless API endpoint |
| 12 | + private static final String API_URL = "https://open.er-api.com/v6/latest/USD"; |
| 13 | + |
| 14 | + /** |
| 15 | + * Fetches the live USD to INR rate from open.er-api.com |
| 16 | + * |
| 17 | + * @return the exchange rate, or -1 if an error occurs |
| 18 | + */ |
| 19 | + public static double fetchLiveRate() { |
| 20 | + try { |
| 21 | + // Step 1: Connect to the URL |
| 22 | + URL url = new URL(API_URL); |
| 23 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 24 | + conn.setRequestMethod("GET"); |
| 25 | + |
| 26 | + // Step 2: Read API response |
| 27 | + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 28 | + StringBuilder response = new StringBuilder(); |
| 29 | + String line; |
| 30 | + while ((line = reader.readLine()) != null) { |
| 31 | + response.append(line); |
| 32 | + } |
| 33 | + reader.close(); |
| 34 | + |
| 35 | + // Step 3: Extract INR value manually from the JSON text |
| 36 | + String json = response.toString(); |
| 37 | + |
| 38 | + // Find INR rate |
| 39 | + int start = json.indexOf("\"INR\":") + 6; |
| 40 | + int end = json.indexOf(",", start); |
| 41 | + if (end == -1) |
| 42 | + end = json.indexOf("}", start); |
| 43 | + String rateStr = json.substring(start, end); |
| 44 | + |
| 45 | + return Double.parseDouble(rateStr); |
| 46 | + } catch (Exception e) { |
| 47 | + System.out.println("Error fetching exchange rate: " + e.getMessage()); |
| 48 | + return -1; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + Scanner sc = new Scanner(System.in); |
| 54 | + |
| 55 | + System.out.println("USD ↔ INR Converter"); |
| 56 | + System.out.println("--------------------"); |
| 57 | + System.out.println("Choose conversion type:"); |
| 58 | + System.out.println("1. USD → INR"); |
| 59 | + System.out.println("2. INR → USD"); |
| 60 | + System.out.print("Enter your choice (1 or 2): "); |
| 61 | + int choice = sc.nextInt(); |
| 62 | + |
| 63 | + // Fetch live exchange rate |
| 64 | + double rate = fetchLiveRate(); |
| 65 | + |
| 66 | + if (rate <= 0) { |
| 67 | + System.out.println("Failed to retrieve live exchange rate. Please check your internet connection."); |
| 68 | + sc.close(); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Conversion logic |
| 73 | + if (choice == 1) { |
| 74 | + System.out.print("Enter amount in USD: "); |
| 75 | + double usd = sc.nextDouble(); |
| 76 | + double inr = usd * rate; |
| 77 | + System.out.printf("%.2f USD = %.2f INR (Rate: %.2f)%n", usd, inr, rate); |
| 78 | + } else if (choice == 2) { |
| 79 | + System.out.print("Enter amount in INR: "); |
| 80 | + double inr = sc.nextDouble(); |
| 81 | + double usd = inr / rate; |
| 82 | + System.out.printf("%.2f INR = %.2f USD (Rate: %.2f)%n", inr, usd, rate); |
| 83 | + } else { |
| 84 | + System.out.println("Invalid choice. Please run the program again and select 1 or 2."); |
| 85 | + } |
| 86 | + |
| 87 | + sc.close(); |
| 88 | + } |
| 89 | +} |
0 commit comments