From 18d0aa5c78b719d7ade3022ca393b1759588a881 Mon Sep 17 00:00:00 2001 From: ilma Date: Sat, 11 Oct 2025 20:52:27 +0530 Subject: [PATCH 1/3] =?UTF-8?q?Fixed=20API=20and=20added=20plain-text=20US?= =?UTF-8?q?D=E2=86=94INR=20converter=20with=20user=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../thealgorithms/conversions/USDtoIND.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/main/java/com/thealgorithms/conversions/USDtoIND.java diff --git a/src/main/java/com/thealgorithms/conversions/USDtoIND.java b/src/main/java/com/thealgorithms/conversions/USDtoIND.java new file mode 100644 index 000000000000..f2af8a2c4268 --- /dev/null +++ b/src/main/java/com/thealgorithms/conversions/USDtoIND.java @@ -0,0 +1,89 @@ +package com.thealgorithms.conversions; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Scanner; + +public class USDtoIND { + + // ✅ Free, keyless API endpoint + private static final String API_URL = "https://open.er-api.com/v6/latest/USD"; + + /** + * Fetches the live USD to INR rate from open.er-api.com + * + * @return the exchange rate, or -1 if an error occurs + */ + public static double fetchLiveRate() { + try { + // Step 1: Connect to the URL + URL url = new URL(API_URL); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + + // Step 2: Read API response + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + StringBuilder response = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + + // Step 3: Extract INR value manually from the JSON text + String json = response.toString(); + + // Find INR rate + int start = json.indexOf("\"INR\":") + 6; + int end = json.indexOf(",", start); + if (end == -1) + end = json.indexOf("}", start); + String rateStr = json.substring(start, end); + + return Double.parseDouble(rateStr); + } catch (Exception e) { + System.out.println("Error fetching exchange rate: " + e.getMessage()); + return -1; + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("USD ↔ INR Converter"); + System.out.println("--------------------"); + System.out.println("Choose conversion type:"); + System.out.println("1. USD → INR"); + System.out.println("2. INR → USD"); + System.out.print("Enter your choice (1 or 2): "); + int choice = sc.nextInt(); + + // Fetch live exchange rate + double rate = fetchLiveRate(); + + if (rate <= 0) { + System.out.println("Failed to retrieve live exchange rate. Please check your internet connection."); + sc.close(); + return; + } + + // Conversion logic + if (choice == 1) { + System.out.print("Enter amount in USD: "); + double usd = sc.nextDouble(); + double inr = usd * rate; + System.out.printf("%.2f USD = %.2f INR (Rate: %.2f)%n", usd, inr, rate); + } else if (choice == 2) { + System.out.print("Enter amount in INR: "); + double inr = sc.nextDouble(); + double usd = inr / rate; + System.out.printf("%.2f INR = %.2f USD (Rate: %.2f)%n", inr, usd, rate); + } else { + System.out.println("Invalid choice. Please run the program again and select 1 or 2."); + } + + sc.close(); + } +} From 372be90238a814d8b2bfeb6d04c3834e4c627d3c Mon Sep 17 00:00:00 2001 From: ilma Date: Sat, 11 Oct 2025 20:56:15 +0530 Subject: [PATCH 2/3] =?UTF-8?q?Updated=20USD=E2=86=94INR=20converter=20and?= =?UTF-8?q?=20pom.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- desktop.ini | 2 ++ pom.xml | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 desktop.ini diff --git a/desktop.ini b/desktop.ini new file mode 100644 index 000000000000..3dfcbb05764b --- /dev/null +++ b/desktop.ini @@ -0,0 +1,2 @@ +[.ShellClassInfo] +LocalizedResourceName=@Java,0 diff --git a/pom.xml b/pom.xml index e0e3516c08eb..9ea475131b4a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,12 @@ Java 1.0-SNAPSHOT jar + + org.json + json + 20240303 + + UTF-8 @@ -14,6 +20,7 @@ 21 3.27.6 + From a5a10f5158869a007c8523e23dd1a19e08ab28ab Mon Sep 17 00:00:00 2001 From: Ilma <155047468+Ilmakram@users.noreply.github.com> Date: Wed, 15 Oct 2025 15:36:27 +0530 Subject: [PATCH 3/3] Update pom.xml --- pom.xml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 9ea475131b4a..337d7ba667c8 100644 --- a/pom.xml +++ b/pom.xml @@ -7,11 +7,7 @@ Java 1.0-SNAPSHOT jar - - org.json - json - 20240303 - +