Create a program called Currency Conversion. This will convert one currency to other currencies (for example CAD to GBP, Euro and USD). The program should prompt user to choose currency in hand and currency required. It should also ask the user to enter amount (currency value). The conversion rate is fixed as mentioned below. You should store the exchange rate as final values. Use Scanner for input and printf for formatting decimals (rounded to two decimal places) Add comments for wherever needed for code maintenance and mathematical procedures Your program should display well-articulated print statement with appropriate symbols. Exchange rate: As most of the currencies are traded against USD, the exchange rates are expressed in US dollars. One Euro (€) = 1.124 US dollar One CAD ($) = 0.758US dollars One GBP (£) = 1.296 US dollar
Programming Guidelines: Beginning comments: Include your name and a short narrative as a comment at the beginning of your program stating what the program does, not how it does it. Ensure variable names are meaningful. Use lower CamelCase. Do not use made up short forms for any part of a variable name. Are brace brackets aligned? The opening brace bracket should be in the same column as the closing bracket. Did you indent inside the brace bracket? Challenge: Validate user inputs to ensure it’s not inappropriate, negative entry or zero. Allow the user to re- enter invalid data. Is the output informative and formatted properly? Use currency symbol and 2 decimal places. Output is well spaced not too congested. Include the blank lines to separate the outputs. Ensure that the file you submitted can be opened successfully. Ensure you do not get a divide by zero error when converting currency.
/**
* CurrencyConverter class
* The program prompt the user to choose currency in hand and
currency required.
* It also ask the user to enter amount (currency value)
* Then displays the exchange value
* This program validates every inputs for currency
* Only the positive value for the amount is accepted
*/
import java.util.Scanner;
public class CurrencyConverter {
// final variables for exchange rate of each
currency
static final double EURO = 1.124;
static final double CAD = 0.758;
static final double GBP = 1.296;
// main method
public static void main(String[] args) {
// scnnaer object to
read input
Scanner in = new
Scanner(System.in);
// variable for currency
in hand
String
currencyFrom;
// loop to get and
validate input for currency in hand
while(true){
System.out.print("Enter currency in hand: ");
currencyFrom = in.next().toUpperCase();
if(checkCurrency(currencyFrom))
break;
System.out.println("Invalid currency");
}
// variable for currency
required
String currencyTo;
// loop to get and
validate input for currency required
while(true){
System.out.print("Enter required currency: ");
currencyTo = in.next().toUpperCase();
if(checkCurrency(currencyTo))
break;
System.out.println("Invalid currency");
}
// variable for
amount
double amount;
// loop to get positive
value for the amount
while(true){
System.out.print("Enter Amount: ");
amount = in.nextDouble();
if(amount>0)
break;
System.out.println("Invalid amount");
}
// convert given amount
to dollar from currency in hand
double dollar =
convertToDollar(currencyFrom, amount);
// convert dollar to
required currency
double exchangeValue =
dollarToCurrency(currencyTo, dollar);
// display the amount
and exchange value in 2 decimal places
System.out.printf("\n%.2f %s = %.2f
%s\n",amount,currencyFrom,exchangeValue,currencyTo);
}
// method to convert given currency amount to
dollar
private static double convertToDollar(String
currencyFrom, double value) {
if(currencyFrom.equals("EURO"))
return value*EURO;
if(currencyFrom.equals("CAD"))
return value*CAD;
if(currencyFrom.equals("GBP"))
return value*GBP;
if(currencyFrom.equals("USD"))
return value;
return 0.0;
}
// method to convert dollar to given currency
amount
private static double dollarToCurrency(String
currency, double dollar) {
if(currency.equals("EURO"))
return dollar/EURO;
if(currency.equals("CAD"))
return dollar/CAD;
if(currency.equals("GBP"))
return dollar/GBP;
if(currency.equals("USD"))
return dollar;
return 0.0;
}
// method to check currency
private static boolean checkCurrency(String
currency) {
return
currency.equals("USD") || currency.equals("CAD") ||
currency.equals("EURO") ||currency.equals("GBP");
}
}
Sample output
Get Answers For Free
Most questions answered within 1 hours.