一家名为 Insure Everyone 的公司用外币向员工支付工资。但由于法律规定,工资必须换算成当地货币。它的员工遍布世界各地。 Insure Everyone 的人力资源部门正在寻求一位有才华的开发者的帮助,来构建一个把每位员工的工资换算成其对应当地货币的程序。Insure Everyone 依赖外部 API 提供的汇率。
给定这个外部汇率 API,你需要编写一个函数,它接收 source currency、salary(以源货币计)以及员工的 local currency,并返回以本地货币计的工资。
外部货币兑换 API 的响应如下:
注意:为了保持问题陈述简短,这里的
rates已被截断
{
"base": "EUR",
"rates": {
"AED": 4.167972,
"AFN": 118.91123,
"ALL": 121.353306,
"AMD": 546.175542,
"ANG": 2.035384,
"AOA": 629.106987,
"ARS": 116.624363,
"AUD": 1.580224,
"AWG": 2.042846,
"AZN": 1.92923,
"BAM": 1.95468,
"BBD": 2.26981,
"BDT": 97.024176,
"BGN": 1.956957,
"BHD": 0.428175,
"BIF": 2257.462616,
"BMD": 1.134504,
"BND": 1.534691,
"BOB": 7.773946,
"BRL": 6.394753,
"BSD": 1.134916,
"BTC": 0.000027,
"BTN": 83.896824,
"BWP": 13.167125,
}
}
获取USD汇率的示例请求:
GET http://localhost:8080/rates/USD
{
"base": "USD",
"rates": {
"AED": 3.672477643818380310171368756063197,
"AFN": 104.7748962262571579872079201988508,
"ALL": 106.9266548068103418652047146893925,
"AMD": 481.2454276553169780099231576470961,
"ANG": 1.793414695824643301163693331958788,
"AOA": 554.3178661774696212335484143830275,
"ARS": 102.7598951821316197338845611280433,
"AUD": 1.392364754903645275750470737610812,
"AWG": 1.799989602800547350872880138793802,
"AZN": 1.699880432206294535038121596128717,
"BAM": 1.722304900517304728699178180683942,
"BBD": 1.999971804204874171858658003508614,
"BDT": 85.48980589838411660371074286227852,
"BGN": 1.724311207564226937381544618524109,
"BHD": 0.3772729555625457630971671155965922,
"BIF": 1989.092294529751410010035940827599,
"BMD": 0.9996325735447665520331371082216295
// omitted
}
}
salary <= 10000sourceCurrency和targetCurrency是 ISO 货币代码(字母)。测试输入中可能包含无效的货币代码。5位。你需要在下面的函数里编写代码。
# The exchange rate API base URL
configurable string apiUrl = "http://localhost:8080";
# Convert provided salary to local currency.
#
# + salary - Salary in source currency
# + sourceCurrency - Soruce currency
# + localCurrency - Employee's local currency
# + return - Salary in local currency or error
public function convertSalary(decimal salary, string sourceCurrency, string localCurrency) returns decimal|error {
// TODO: Write your code here
return 0;
}
bal run运行项目来手动测试,也可以编写单元测试并用bal test运行它们。后端服务会自动在http://localhost:8080启动假设外部货币兑换返回了以下换算汇率响应(已省略):
{
"base": "USD",
"rates": {
// omitted
"GBP": 0.73584,
// omitted
}
}
函数必须返回735.84000
假设外部货币兑换返回了以下换算汇率响应(已省略):
{
"base": "GBP",
"rates": {
// omitted
"USD": 1.35898,
// omitted
}
}
函数必须返回1358.9800