Java Reference
In-Depth Information
specifies a value or amount of the first currency. In this case, the client should
return not just the rate but also the actual value of the conversion (so, for example,
the inputs
USD
CDN
5.00
represent a request of the exchange rate from
US
dollars
to Canadian dollars, plus the actual Canadian dollar amount if 5.00
US
dollars
were exchanged at that rate).
Using any and all of the features you've seen so far, update your
CommandLi-
neClient
code to look like listing 5.2.
Listing 5.2
A revamped version of the command-line currency exchange client
package com.acme.conversion.currency.client;
import com.acme.conversion.currency.service.CurrencyExchangeService;
import com.acme.conversion.currency.service.CurrencyExchangeServiceFactory;
public class CommandLineClient {
public static void main(String[] args) {
if (args.length != 2 && args.length != 3) {
System.out.println("Requested parameters: " +
"<source currency code> <target currency code> " +
"[amount to exchange]");
}
else {
CurrencyExchangeService service =
CurrencyExchangeServiceFactory.getService();
double rate = service.requestCurrentRate(args[0], args[1]);
System.out.println("Rate is " + rate);
if (args.length >= 3) {
double oldAmount = Double.parseDouble(args[2]);
double newAmount = oldAmount * (1/rate);
System.out.println(oldAmount + " " + args[0]
+ " is " + newAmount + " " + args[1]);
}
}
}
}
Number of arguments
must be 2 or 3
If 3 arguments, execute additional
cod
e
Conversion calculation
Convert number
into a double
For those of you paying attention,
this code contains an intentional bug!
Chapter 7
covers debugging and testing, so we'll catch it there. The code is, however, in per-
fect shape to run.
To run the executable within
IDEA
, you need to create a Run/Debug configu-
ration that represents this context. Follow these steps to set up the configuration:
Select the
Run | Edit Configurations
menu option.
1
Select the
Applications
tab.
2