In: Computer Science
Write a program CurrencyConverter.java (in a package named a02) that takes currency amounts in Euros (EUR), US Dollars (USD) and Japanese Yen (JPY) as command line arguments and converts them to Canadian dollars (CAD). The correct format for each command line argument is as follows:
the 3-character currency code (ignoring upper or lower case); followed by one or more digits; followed by the decimal point '.'; followed by 2 digits.
Some examples of valid arguments are:
JPy57687.34
USD0.34
eur30.00
Some examples of invalid arguments are:
0.34USD
EUR25
JPY5768.931
EURUSD3.14
Your program will need to check if the arguments are valid and it should not crash if they are not valid.
Hints:
The min length of a valid argument is 7. A valid argument must begin with one of the 3-character codes. A valid argument must end with the 3-character decimal part: a '.' then exactly 2 digits.
The program should have 2 modes of operation: 1) If no command line arguments are given the program should display the normal information header as well as instructions for the user on the correct usage of the program (how command line arguments should be formatted to be valid). The program should then pause before ending.
Example with no command line parameters:
Assignment##
Lastname, Firstname
A########
Description of the program Instructions for correct usage Press enter to end...
2) If one, or more, command line argument is provided the program will not display the information header and will output the converted amounts one per line. If any of the arguments are invalid then the corresponding line will display an error message but the program will continue with converting the next argument until all have been processed. The program should then pause before ending.
Example with command line arguments:
EUR12.50 USD125.00 EUR13 usd15.67 JPY50000.00 JPY1.00
€12.50 is $19.00 CAD. $
125.00 is $161.25 CAD.
Error: invalid input EUR13.
$15.67 is $20.21 CAD.
¥50000.00 is $550.00 CAD.
¥1.00 is $0.01 CAD.
Press enter to end...
Notice the format of the output: both the input amount and the converted amount are displayed with the appropriate currency symbol and exactly 2 places after the decimal point. Also the values are rounded correctly to those 2 digits after the decimal point (printf will do this rounding for you!). The currency symbols are: €, $, ¥.
The program should use the following exchange rates: 1 EUR is 1.52 CAD 1 USD is 1.29 CAD 1 JPY is 0.011 CAD
For this assignment you should use several methods (with short Javadoc comments) rather than have all the code in the main method. For example, a method that checks if an argument is valid, a method that converts the currency, a method that outputs the results in the correct format, a method that pauses asking the user to press enter.
Program Code Screenshot :
Sample Output :
Program Code to Copy
import java.util.Scanner; class Main{ static boolean isValid(String s){ //Check minimum length if(s.length()<7){ return false; } String code = s.substring(0,3).toUpperCase(); //Code must be JPY, EUR or USD if(!code.equals("JPY") && !code.equals("EUR") && !code.equals("USD")){ return false; } //Find the dot int dot = s.substring(3).indexOf("."); //If . not found, return false if(dot<=0){ return false; } //There must be 2 digits after decimal point if(s.length()-dot<3){ return false; } try { //Amout should be parsable double amt = Double.parseDouble(s.substring(3)); }catch(Exception e){ return false; } return true; } //Converts the given amount and currency to Canadian dollar public static double convert(double amt, String code){ double converted = amt; if (code.equals("JPY")) { converted = amt * 0.011; } else if (code.equals("USD")) { converted = amt * 1.29; } else if (code.equals("EUR")) { converted = amt * 1.52; } return converted; } //Print the converted currency in 2 decimal format public static void printConverted(String code, double amt, double converted){ if (code.equals("JPY")) { System.out.format("¥%.2f is $%.2f CAD\n",amt,converted); } else if (code.equals("USD")) { System.out.format("$%.2f is $%.2f CAD\n",amt,converted); } else if (code.equals("EUR")) { System.out.format("€%.2f is $%.2f CAD\n",amt,converted); } } public static void pause(){ Scanner obj = new Scanner(System.in); obj.nextLine(); } public static void main(String[] args) { if(args.length==0){ System.out.println("Assignment##"); System.out.println("Lastname,Firstname"); System.out.println("Please enter comma separated arguments in Command line arguments. " + "Each argument should be a 3-character code, followed by a Decimal number, " + "including a decimaul point followed by 2 digits"); } else{ for (String arg : args) { if (!isValid(arg)) { //Check if argument is valid or not System.out.println("Error: invalid input " + arg); } else { //Parse code and amount String code = arg.substring(0, 3).toUpperCase(); double amt = Double.parseDouble(arg.substring(3)); //Convert if (code.equals("JPY")) { double converted = convert(amt,code); printConverted(code,amt,converted); } else if (code.equals("USD")) { double converted = convert(amt,code); printConverted(code,amt,converted); } else if (code.equals("EUR")) { double converted = convert(amt,code); printConverted(code,amt,converted); } } } } pause(); } }