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:
1. Upon execution, the application shall display a brief welcome message to the user to let her know she is running the currency converter application.
2. The application shall allow a user to convert amounts between any two of the following currencies: United States Dollars (USD), British Pounds (GBP), Swiss Francs (CHF), and Indian Rupees (INR).
3. The application shall prompt the user for the following input: the currency she wishes to convert from (“source currency”); the amount of currency to be converted (“requested amount”) as either a decimal or a whole number (e.g., 21.45, 1.748484, or 34); and the currency she wishes to convert to (“result currency”).
4. The application shall limit the amount of the currency to be converted to no more than 1,000,000 (one million) units of that currency; if the user attempts to enter an amount larger than this number, the application should return a message alerting the user that she has exceeded the maximum amount, and prompt her to enter another amount.
5. The application shall handle incorrect user input (e.g., if the user inputs a character or string for the “requested amount” value) and prompt the user for correct input.
6. The application shall prevent the user from attempting to convert to a “result currency” that is the same as the “source currency” (e.g., the user should be prevented from converting United States Dollars to United States Dollars).
7. The application shall output the following information: the amount of the “result currency” that is equivalent to the “requested amount” in the “source currency”; this amount shall be returned as a decimal figure rounded to two decimal places (e.g., 67.49).
8. The application shall allow the user to exit the program at any time by accepting input of an exit character (for example “Q” for quit) to terminate the program.
9. After the user has successfully converted a currency, the application shall prompt the user to either perform another conversion or terminate the program.
10. Upon program termination, the application shall display an exit message to the user.
Program Data Your program should use the following data for currency conversions.
USD GBP CHF INR
USD 1.000000 1.227902 1.012514 0.013950
GBP 0.814349 1.000000 0.824820 0.011365
CHF 0.987370 1.212602 1.000000 0.013777
INR 71.687245 88.022221 72.582298 1.000000
Explanation::
Code in JAVA::
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Currency {
public static void main(String[] args) throws
Exception{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("--------------------------------------------------------------------------------");
System.out.println(" WELCOME TO
CURRENCY CONVERTER PROGRAM");
System.out.println("Convert
USD,GBP,CHF,INR");
System.out.println("--------------------------------------------------------------------------------");
String currency[]= {"USD","GBP","CHF","INR"};
String
sourceCurrency,resultCurrency;
while(true) {
while(true)
{
System.out.print("Enter the currency in which
you want to enter amount(USD,GBP,CHF or INR):");
sourceCurrency=br.readLine();
sourceCurrency=sourceCurrency.toUpperCase();
if(isValidCurrency(currency,sourceCurrency))
{
break;
}else {
System.out.println("Invalid
Currency Entered! Try Again.");
}
}
double
requestedAmount;
while(true)
{
System.out.print("Enter request amount in
"+sourceCurrency+" currency: ");
String value=br.readLine();
if(isDouble(value)) {
requestedAmount=Double.parseDouble(value);
if(requestedAmount<=1000000) {
break;
}else {
System.out.println("You exceeded the maximum amount. Enter
again.");
}
}else {
System.out.println("Invalid
input! Please Try again.");
}
}
while(true)
{
System.out.print("Enter the currency in which
you want converted amount:");
resultCurrency=br.readLine();
resultCurrency=resultCurrency.toUpperCase();
if(isValidCurrency(currency,resultCurrency)
&& !sourceCurrency.equals(resultCurrency)) {
break;
}else {
System.out.println("Invalid
Currency Entered! Try Again.");
}
}
double
answer=amount(sourceCurrency,resultCurrency,requestedAmount);
System.out.println("The amount in "+resultCurrency+" is :
"+answer);
System.out.println("Continue? Enter Q to quit else enter
anything");
String
choice=br.readLine();
if(choice.equals("Q")) {
System.out.println("------------------------------------------------------------------");
System.out.println(" Program Ended");
System.out.println("------------------------------------------------------------------");
break;
}
}
}
public static boolean isValidCurrency(String
currency[], String s) {
for(int
i=0;i<currency.length;i++) {
if(currency[i].equals(s)) {
return true;
}
}
return false;
}
public static boolean isDouble(String value) {
try {
Double.parseDouble(value);
return
true;
} catch (NumberFormatException e)
{
return
false;
}
}
public static double amount(String
sourceCurrency,String resultCurrency,double requestedAmount)
{
String currency[]=
{"USD","GBP","CHF","INR"};
int index1=-1,index2=-1;
double amountArray[][]= {
{1.000000,1.227902,1.012514,0.013950},
{0.814349,1.000000,0.824820,0.011365},
{0.987370,1.212602,1.000000,0.013777},
{71.687245,88.022221,72.582298,1.000000}
};
for(int
i=0;i<currency.length;i++) {
if(currency[i].equals(sourceCurrency)) {
index1=i;
}
if(currency[i].equals(resultCurrency)) {
index2=i;
}
}
double
multiplier=amountArray[index2][index1];
return
requestedAmount*multiplier;
}
}
OUTPUT::
Please provide the feedback!!
Thank You!!