Question

In: Computer Science

Write a program CurrencyConverter.java (in a package named a02) that takes currency amounts in Euros (EUR),...

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.

Solutions

Expert Solution

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();
    }
}

Related Solutions

Provide guidance on how a firm could mitigate the impact of currency exchange changes: EUR/AED (Euros...
Provide guidance on how a firm could mitigate the impact of currency exchange changes: EUR/AED (Euros / United Arab Emirates Dirham) - How should the firm approach transaction, translation, and/or operating exposure in the UAE (United Arab Emirates) Scenario: You are operating a German manufacturing firm in the UAE.
Statistics are often calculated with varying amounts of inputdata. Write a program that takes any...
Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics
Statistics are often calculated with varying amounts of input data. Write a program that takes any...
Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input and outputs the average and max. A negative integer ends the input and is not included in the statistics. Ex: When the input is 15 20 0 5 -1, the output is: 10 20 You can assume that at least one non-negative integer is input. Can this be written in CORAL please!!
Statistics are often calculated with varying amounts of input data. Write a program that takes any...
Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics.
Suppose the spot exchange rate between Brazilian real and euros is S0BRL∕EUR= BRL 2.9488∕EUR. Calculate forward...
Suppose the spot exchange rate between Brazilian real and euros is S0BRL∕EUR= BRL 2.9488∕EUR. Calculate forward exchange rates at 1-year,2-year, and 3-year horizons under these two scenarios. a. Yield curves in euros and real are flat. Annual Eurocurrency interest rates are iBRL= 5 percent and iEUR= 1 percent for the next several years. b. The euro yield curve is flat at iEUR= 1.0 percent per year. Brazilian real interest rates are 5.5 percent per year at a 1-year horizon, 5.0...
Forex quotes: 1.1837 EUR/USD 0.7231 EUR/GBP 1.6388 GBP/USD If Mary was given 10,000 euros will she...
Forex quotes: 1.1837 EUR/USD 0.7231 EUR/GBP 1.6388 GBP/USD If Mary was given 10,000 euros will she be able to earn some profit for her institution by playing the above FOREX quotes:If so, compute the profit. What is such type of profit called?
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
In a package named "oop" create a Scala class named "Score" with the following: • A...
In a package named "oop" create a Scala class named "Score" with the following: • A constructor that takes an Int and stores it in a member variable named score • A method named scoreGoal that takes no parameters and has return type Unit that increments the score by 1 • A method named isWinner that takes a Score object as a parameter and returns a Boolean. The method returns true if this instances score is strictly greater than the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT