Question

JAVA: Using the forex.csv Given a file with the USD to conversion rate Convert X amount...

JAVA:

Using the forex.csv

Given a file with the USD to conversion rate Convert X amount of Currency A to Currency B User inputs are the source and destination currency symbols and the amount. $3000 AUD = $42,641.40 MXN

Symbol Currency Dollar
USD US Dollar 1
EUR Euro 0.842850125
GBP British Pound 0.761549464
INR Indian Rupee 63.72054347
AUD Australian Dollar 1.259237335
CAD Canadian Dollar 1.257801755
SGD Singapore Dollar 1.359074339
CHF Swiss Franc 0.970036942
MYR Malaysian Ringgit 4.280492421
JPY Japanese Yen 110.115807
CNY Chinese Yuan Renminbi 6.7191876
NZD New Zealand Dollar 1.344839521
THB Thai Baht 33.26460563
HUF Hungarian Forint 255.9880337
AED Emirati Dirham 3.672967434
HKD Hong Kong Dollar 7.818109862
MXN Mexican Peso 17.87416927
ZAR South African Rand 13.40320648
PHP Philippine Peso 50.3272521
SEK Swedish Krona 8.106236601
IDR Indonesian Rupiah 13332.07455
SAR Saudi Arabian Riyal 3.750330177
BRL Brazilian Real 3.119954853
TRY Turkish Lira 3.535513983
KES Kenyan Shilling 103.8403002
KRW South Korean Won 1128.191103
EGP Egyptian Pound 17.7925951
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class CurrencyConverter {
  
private static final String FILENAME = "forex.csv";
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Scanner fileReader;
ArrayList<String> symbols = new ArrayList<>();
ArrayList<Double> rates = new ArrayList<>();
  
// read the file "forex.csv" and populate the 2 lists
// data in the input are separated by comma (Eg: USD,US Dollar,1)
try
{
int count = 0; // counter variable for how many records are being read from the input file
fileReader = new Scanner(new File(FILENAME));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine().trim();
String[] data = line.split(",");
String symbol = data[0];
// data[1] is the currency and it is of no use to us, so ignore it
double rate = Double.parseDouble(data[2]);
  
symbols.add(symbol);
rates.add(rate);
count++;
}
fileReader.close();
System.out.println(count + " records are read from file: " + FILENAME);
}catch(FileNotFoundException fnfe){
System.out.println(FILENAME + " could not be found!");
System.exit(-1);
}
  
// user input for source and destination currency and the amount
System.out.print("\nEnter the source currency symbol(Eg: USD): ");
String inpSourceSymbol = sc.nextLine().toUpperCase().trim();
System.out.print("Enter the destination currency symbol(Eg: CAD): ");
String inpDestSymbol = sc.nextLine().toUpperCase().trim();
System.out.print("Enter the amount: $");
String inp = sc.nextLine().trim();
while(!isDouble(inp))
{
System.out.println("Please enter a valid double value for amount!");
System.out.print("Enter the amount: $");
inp = sc.nextLine().trim();
}
double amount = Double.parseDouble(inp);
  
double convAmt = getConvertedAmount(symbols, rates, inpSourceSymbol, inpDestSymbol, amount);
  
System.out.println("\n$" + amount + " " + inpSourceSymbol + " = $"
+ String.format("%.2f", convAmt) + " " + inpDestSymbol);
}
  
private static double getConvertedAmount(ArrayList<String> symbols, ArrayList<Double> rates,
String source, String dest, double amt)
{
double res = 0.0, sourceRate, destRate = 0.0;
boolean found = false;
int index = -1;
  
// find the source symbol in the symbols list
for(int i = 0; i < symbols.size(); i++)
{
if(symbols.get(i).equals(source))
{
found = true;
index = i;
break;
}
}
  
// source symbol is present
if(found)
{
// get the source rate
sourceRate = rates.get(index);
amt /= sourceRate;
  
// find the index of destinaton symbol
for(int i = 0; i < symbols.size(); i++)
{
if(symbols.get(i).equals(dest))
{
// destination symbol present, so get the destination rate
destRate = rates.get(i);
break;
}
}
  
res = (amt * destRate);
}
return res;
}
  
private static boolean isDouble(String s)
{
try
{
Double.parseDouble(s);
return true;
}catch(NumberFormatException nfe){
return false;
}
}
}

****************************************************************** SCREENSHOT *********************************************************

run: 27 records are read from file: forex.csv Enter the source currency symbol (Eg: USD): AUD Enter the destination currency symbol (Eg: CAD) : MXN Enter the amount: $3000 $3000.0 AUD = $42583.32 MXN BUILD SUCCESSFUL (total time: 18 seconds)

Add a comment
Know the answer?
Add Answer to:
JAVA: Using the forex.csv Given a file with the USD to conversion rate Convert X amount...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following are quotes from a currency dealer in the New York currency market: 2. Using...

    The following are quotes from a currency dealer in the New York currency market: 2. Using the quotes provided above, answer the following question. (Phrase your explanation in parts b and d: as “If you sell one (specify the currency) to the dealer, you will receive (specify the number of units and the currency)” or “If you buy one (specify the currency) from the dealer, you will pay (specify the number of units and the currency)”.) 2a. What is the...

  • The following are quotes from a currency dealer in the New York currency market: Using the...

    The following are quotes from a currency dealer in the New York currency market: Using the quotes provided above, answer the following question. (Phrase your explanation in parts b and d: as “If you sell one (specify the currency) to the dealer, you will receive (specify the number of units and the currency)” or “If you buy one (specify the currency) from the dealer, you will pay (specify the number of units and the currency)”.) 1 Using the quotes provided...

  • Comparing Cheap Dates Around the World. Comparison of prices or costs across difforent country and oumency...

    Comparing Cheap Dates Around the World. Comparison of prices or costs across difforent country and oumency environments requines transiation of the local oumency into a single common cumency. This is most meaningful when the comparison is for the identical or near-identicel product or service across countries Deutsche Bank has recently started publishing a comparison of cheap dates-and evening on the town for two to eat at McDonald's, see a movie, and drinka beer Once all costs are converted to a...

  • The following are quotes from a currency dealer in the New York currency market: Spot exchange...

    The following are quotes from a currency dealer in the New York currency market: Spot exchange rates and trades 1a. Which currency above has the widest bid ask spread? Which has the narrowest? b. Which currency above has the widest percentage bid ask spread? Which has the narrowest? 2. Using the quotes provided above, answer the following question. (Phrase your explanation in parts b and d: as “If you sell one (specify the currency) to the dealer, you will receive...

  • The following are quotes from a currency dealer in the New York currency market: Using the...

    The following are quotes from a currency dealer in the New York currency market: Using the quotes provided above, answer the following question. (Phrase your explanation in parts b and d: as “If you sell one (specify the currency) to the dealer, you will receive (specify the number of units and the currency)” or “If you buy one (specify the currency) from the dealer, you will pay (specify the number of units and the currency)”.) 3. Using the quotes provided...

  • The following are quotes from a currency dealer in the New York currency market Using the...

    The following are quotes from a currency dealer in the New York currency market Using the quotes provided above, answer the following question. (Phrase your explanation in parts b and d: as “If you sell one (specify the currency) to the dealer, you will receive (specify the number of units and the currency)” or “If you buy one (specify the currency) from the dealer, you will pay (specify the number of units and the currency)”.) 1. Using the quotes provided...

  • This is a python question. Start with this program, import json from urllib import request def...

    This is a python question. Start with this program, import json from urllib import request def main(): to_continue = 'Yes' while to_continue == 'yes' or to_continue == 'Yes': currency_code = input("Enter currency code: ").upper() dollars = int(input("Enter number of dollar you want to convert: ")) # calling the exchange_rates function data = get_exchange_rates() if currency_code in data["rates"].keys(): currency = data["rates"][currency_code] # printing the the currency value by multiplying the dollars amount. print("The value is:", str(currency * dollars)) else: print("Error: the...

  • Use the following table to complete assignment Suppose that on March 1 of the current year, the peso-US$ exchange rate w...

    Use the following table to complete assignment Suppose that on March 1 of the current year, the peso-US$ exchange rate was P5/$. On March 31 of the current year, the exchange rate stood at P8/$. Calculate the 1-month percent change in the value of the Mexican peso (= P). Calculate the spot Korean won-Japanese yen exchange rate in W/¥. (Korean won = W; Japanese yen = ¥) Calculate the spot Taiwanese dollar-euro exchange rate in T$/€. (Taiwanese dollar = T$;...

  • This is a python question. from urllib import request import json """ Complete this program that...

    This is a python question. from urllib import request import json """ Complete this program that calculates conversions between US dollars and other currencies. In your main function, start a loop. In the loop, ask the user what currency code they would like to convert to And, how many dollars they want to convert. If you scroll down, you can see an example response with the available 3-letter currency codes. Call the get_exchange_rates() function. This function returns a dictionary with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT