學習軌道
/
Ballerina
Ballerina
/
練習
/
JSON 燃料津貼
JSON 燃料津貼

JSON 燃料津貼

中等

說明

問題描述

ABC Corp 的每位員工都能獲得無上限的燃料津貼。員工每次為車輛加油時,都必須提交一筆紀錄。這些紀錄會附加到同一個 JSON 檔案中,並在月底統一處理。

身為數位營運團隊的一員,你的任務是讀取這個 JSON 檔案,並將結果寫入另一個 JSON 檔案。輸出檔案應包含每位員工的一筆資料,並帶有下列詳細資訊:

  • 員工編號
  • 加油次數
  • 燃料總費用
  • 總加侖數
  • 累計總英里數

你會收到兩個 string 引數。

  1. 輸入 JSON 檔案的路徑。
  2. 輸出 JSON 檔案的路徑。

輸入的 JSON 檔案包含一個 JSON 物件陣列。每個 JSON 物件的形式如下。

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "employeeId": {
                "type": "integer"
            },
            "odometerReading": {
                "type": "integer"
            },
            "gallons": {
                "type": "number"
            },
            "gasPrice": {
                "type": "number"
            }
        },
        "required": [
            "employeeId",
            "odometerReading",
            "gallons",
            "gasPrice"
        ]
    }
}
// Ballerina record type definition
type FillUpEntry record {|
    int employeeId;
    int odometerReading;
    decimal gallons;
    decimal gasPrice;
|};

你的任務是將 JSON 輸入轉換成下列 JSON 格式,並將內容寫入指定的路徑。輸出檔案應包含每位員工的一筆資料,並依 employeeId 由小到大排序。

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "employeeId": {
                "type": "integer"
            },
            "gasFillUpCount": {
                "type": "integer"
            },
            "totalFuelCost": {
                "type": "number"
            },
            "totalGallons": {
                "type": "number"
            },
            "totalMilesAccrued": {
                "type": "integer"
            }
        },
        "required": [
            "employeeId",
            "gasFillUpCount",
            "totalFuelCost",
            "totalGallons",
            "totalMilesAccrued"
        ]
    }
 }
// Ballerina record type definition
type EmployeeFillUpSummary record {|
    int employeeId;
    int gasFillUpCount;
    decimal totalFuelCost;
    decimal totalGallons;
    int totalMilesAccrued;
|};

限制條件

  • 輸入 JSON 檔案中的加油紀錄數量 n:0 <= n <= 1000
  • 公司員工人數:97
  • 油價以每加侖計算
  • 沒有缺漏的加油紀錄

範例 1

  • 輸入:example01_input.json
[
    {
        "employeeId": 2312,
        "odometerReading": 230,
        "gallons": 18.561,
        "gasPrice": 4.56
    },
    {
        "employeeId": 2312,
        "odometerReading": 500,
        "gallons": 19.345,
        "gasPrice": 4.89
    }
]
  • 輸出:example01_output.json
[
    {
        "employeeId": 2312,
        "gasFillUpCount": 2,
        "totalFuelCost": 179.23521,
        "totalGallons": 37.906,
        "totalMilesAccrued": 270
    }
]

範例 2

  • 輸入:example02_input.json
[
    {
        "employeeId": 2413,
        "odometerReading": 4089,
        "gallons": 21.682,
        "gasPrice": 3.46
    },
    {
        "employeeId": 3423,
        "odometerReading": 6582,
        "gallons": 15.248,
        "gasPrice": 4.56
    },
    {
        "employeeId": 2413,
        "odometerReading": 4127,
        "gallons": 4.221,
        "gasPrice": 3.40
    },
    {
        "employeeId": 2413,
        "odometerReading": 4349,
        "gallons": 11.192,
        "gasPrice": 4.10
    },
    {
        "employeeId": 3423,
        "odometerReading": 6767,
        "gallons": 8.696,
        "gasPrice": 3.34
    },
    {
        "employeeId": 2413,
        "odometerReading": 4547,
        "gallons": 9.197,
        "gasPrice": 2.90
    }
]

  • 輸出:example02_output.json
[
    {
        "employeeId": 2413,
        "gasFillUpCount": 4,
        "totalFuelCost": 161.92962,
        "totalGallons": 46.292,
        "totalMilesAccrued": 458
    },
    {
        "employeeId": 3423,
        "gasFillUpCount": 2,
        "totalFuelCost": 98.57552,
        "totalGallons": 23.944,
        "totalMilesAccrued": 185
    }
]

提示

   json jsonPayload = {id: "2", title: "Jeru", artist: "Gerry Mulligan", price: 17.99};

   // Using cloneWithType  
   Album album1 = check jsonPayload.cloneWithType();
   io:println(album1);
 
   // Using fromJsonWithType
   Album album2 = check jsonPayload.fromJsonWithType();
   io:println(album2);
   Album {id, title, artist, price} = album1;
   io:println(id);
   io:println(title);
  • 若函式只有單一運算式,請使用運算式主體函式。例如,只有 return 敘述的函式。
   function cfr(int deaths, int cases) returns decimal => <decimal>deaths / <decimal>cases * 100;
透過 GitHub 編輯 連結會在新視窗或分頁中開啟
Ballerina Exercism

準備好開始 JSON 燃料津貼 了嗎?

註冊 Exercism,透過 60 個練習 和真人引導來學習並精通 Ballerina,全部免費。