In: Computer Science
Your task is to create a Java application that converts a specified amount of one currency into another currency for a user. The application must meet the following requirements:
Program Data
Your program should use the following data for currency conversions.
USD |
GBP |
CHF |
INR |
|
USD |
1.000000 |
1.29498 |
1.09820 |
0.0136097 |
GBP |
0.772268 |
1.000000 |
0.848271 |
0.0105101 |
CHF |
0.910585 |
1.17887 |
1.000000 |
0.0123905 |
INR |
73.4772 |
95.1467 |
80.7070 |
1.000000 |
Here is some sample data you can use to verify your
calculations:
20.00 USD = 15.45 GBP
20.00 USD = 18.21 CHF
20.00 USD = 1469.54 INR
20.00 CHF = 21.96 USD
20.00 CHF = 16.97 GBP
20.00 CHF = 1614.14 INR
50.00 INR = 0.68 USD
50.00 INR = 0.53 GBP
50.00 INR = 0.62 CHF
Please note that currency rates fluctuate continuously in the real
world. The table outlined above sets baseline rates that I will be
using to verify your calculations. If you use online currency
converters to verify your results, they may be using real-time data
that will result in calculations that are different from those made
using the conversion rate table above.
You may store this data in any fashion you like for this
assignment. I would recommend using arrays (single-dimensional or
multi-dimensional) for this purpose.
// CurrencyConverter.java
import java.util.InputMismatchException;
import java.util.Scanner;
public class CurrencyConverter {
public static void main(String[] args) {
// array to store the valid
currencies
String valid_currency[] = {"USD",
"GBP", "CHF", "INR"};
// array to store the conversion
charts, source currency - rows, destination currency -
columns
double conversion_chart[][] = {
{1.00, 0.772268 , 0.910585 , 73.4772},
{1.29498, 1.00, 1.17887, 95.1467},
{1.09820, 0.848271, 1,00, 80.7070},
{ 0.0136097, 0.0105101, 0.0123905, 1.00}};
int src_index = -1, dst_index =
-1;
String src_currency, dst_currency,
choice;
double amount_to_convert,
converted_amount;
Scanner scan = new
Scanner(System.in);
// loop that continues until the
user exits
do
{
// input the
source currency
System.out.print("Enter the source currency(USD/GBP/CHF/INR):
");
src_currency
=scan.nextLine();
// validate
source currency and re-prompt until valid
while(!src_currency.equalsIgnoreCase("usd") &&
!src_currency.equalsIgnoreCase("gbp") &&
!src_currency.equalsIgnoreCase("chf") &&
!src_currency.equalsIgnoreCase("inr"))
{
System.out.print("Valid currency are: USD, GBP,
CHF and INR. Re-enter source currency: ");
src_currency =scan.nextLine();
}
// input
conversion amount
System.out.print("Enter the amount to convert: ");
// validate
conversion amount and re-prompt until valid
while(true)
{
try
{
amount_to_convert =
scan.nextDouble();
if(amount_to_convert >
1000000)
{
System.out.print("Cannot convert currency more than 1,000,000 (one
million) units. Re-enter: ");
}else
break;
}catch(InputMismatchException e)
{
System.out.print("Invalid
input for amount to convert. Re-enter: ");
scan.nextLine();
}
}
scan.nextLine();
// ignore \n left by nextDouble
// input
destination currency
System.out.print("Enter the destination currency(USD/GBP/CHF/INR):
");
dst_currency
=scan.nextLine();
// validate
destination currency and re-prompt until valid
while(!dst_currency.equalsIgnoreCase("usd") &&
!dst_currency.equalsIgnoreCase("gbp") &&
!dst_currency.equalsIgnoreCase("chf")
&&
!dst_currency.equalsIgnoreCase("inr") ||
((dst_currency.equalsIgnoreCase(src_currency))))
{
if(dst_currency.equalsIgnoreCase(src_currency))
System.out.print("Source and
destination currency cannot be same. Re-enter: ");
else
System.out.print("Valid
currency are: USD, GBP, CHF and INR. Re-enter source currency:
");
dst_currency =scan.nextLine();
}
// loop to get
the index of source currency and destination currency in the
array
for(int
i=0;i<valid_currency.length;i++)
{
if(valid_currency[i].equalsIgnoreCase(src_currency))
src_index = i;
else
if(valid_currency[i].equalsIgnoreCase(dst_currency))
dst_index = i;
}
// display the
result of conversion
System.out.printf("\nThe amount of the %s that is equivalent to
%.2f in the %s: %.2f\n",src_currency, amount_to_convert
,
dst_currency,
(amount_to_convert*conversion_chart[src_index][dst_index]));
// input the
choice,if the user wants to perform another conversion or
exit
System.out.print("Perform another converion(C) or Terminate the
program(Q)? ");
choice =
scan.nextLine();
// validate
choice and re-prompt if invalid
while(!choice.equalsIgnoreCase("C") &&
!choice.equalsIgnoreCase("Q"))
{
System.out.print("Invalid input. Valid inputs
are C or Q. Re-enter: ");
choice = scan.nextLine();
}
}while(choice.equalsIgnoreCase("C"));
}
}
// end of CurrencyConverter.java
Output: