⌛⌛ Currency rates

Place your code into a file name Currencies.java.

Implement a simple currency converter that accepts the following commands from standard input System.in:

  • rate AAA x

    • Records that 1 euro equals x units of the currency AAA.

    • Currencies are denoted by three-letter codes.

    • E.g. the command “rate USD 1.087” would record that 1 euro equals 1.087 US dollars.

    • If the currency already had a previous rate, the new rate replaces the previous one.

    • Print out a message of form “Stored the rate 1 EUR = x AAA”.

  • convert x AAA

    • Prints out a message of form “x AAA = y EUR” that tells the value of x units of the currency AAA converted into euros.

    • E.g. the command “convert 4.99 USD” could print out “4.990 USD = 4.591 EUR”.

    • If no rate information is known for the currency AAA, print a messsage of form

    • “No rate for AAA has been stored!”.

  • rates

    • Prints out all currently known currency rates in ascending alphabetical order of currency codes.

      • First print a header line “Stored euro rates:”.

      • Then print each currency rate on a separate line in the form “`` AAA x``”, where AAA is the currency code and x` is its euro rate.

        • Note that each currency rate line begins with two space characters.

  • quit

    • Stops the program.

      • Print a message “Quit-command received, exiting...” before exiting.

Also take into account the following rules:

  • All values are printed using 3 decimal precision. Hence the format function might be more convenient than the print-function.

  • Print out a message of form “``Enter command: ``” without new line before reading a command.

  • Immediately print out the command given by the user.

  • If the command given by the user does not match any of the above defined commands, or has illegal parameters, print a message “Unknown or illegal command!” to standard error output System.err.

  • Record and compute currency rates using variables of type double (or Double).

The example test given below illustrates how the program should behave.

Example test

The expected output of the first test is shown below. Note that the shown user commands are visible here because they have been printed out by the program (as was specified above).

Enter command: rate usd 1.0873524599
Stored the rate 1 EUR = 1.087 USD
Enter command: rate GBP 0.7160936876
Stored the rate 1 EUR = 0.716 GBP
Enter command: rate INR 69.3502525376
Stored the rate 1 EUR = 69.350 INR
Enter command: convert 125 AUD
No rate for AUD has been stored!
Enter command: rate aud 1.3895878081
Stored the rate 1 EUR = 1.390 AUD
Enter command: convert 125 AUD
125.000 AUD = 89.955 EUR
Enter command: rate CAD 1.3243409285
Stored the rate 1 EUR = 1.324 CAD
Enter command: rate SGD 1.4485057059
Stored the rate 1 EUR = 1.449 SGD
Enter command: rate CHF 1.0371983277
Stored the rate 1 EUR = 1.037 CHF
Enter command: rates
Stored euro rates:
  AUD 1.390
  CAD 1.324
  CHF 1.037
  GBP 0.716
  INR 69.350
  SGD 1.449
  USD 1.087
Enter command: convert 149.99 GBP
149.990 GBP = 209.456 EUR
Enter command: quit
Quit-command received, exiting...

A+ presents the exercise submission form here.

Posting submission...