トラック
/
Ballerina
Ballerina
/
演習
/
JSON燃料手当
JSON燃料手当

JSON燃料手当

中級

説明

問題

ABC Corpの従業員には、全員に無制限の燃料手当が支給されます。従業員は、車に給油するたびに記録を送信する必要があります。これらの記録は同じJSONファイルに追加され、月末に処理されます。

デジタル運用チームの一員として、このJSONファイルを読み込み、その結果を別のJSONファイルに書き出すのがタスクです。出力ファイルには、従業員ごとに次の詳細を含むエントリが入っている必要があります。

  • 従業員番号
  • 給油回数
  • 燃料費の合計
  • ガロンの合計
  • 走行距離の合計(マイル)

2つの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ガロンあたりです
  • 給油記録の欠落はありません

例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);
  • 式が1つだけの関数には、式本体の関数を使います。たとえば、return文だけを持つ関数です。
   function cfr(int deaths, int cases) returns decimal => <decimal>deaths / <decimal>cases * 100;
GitHubで編集する リンクは新しいウィンドウまたはタブで開きます
Ballerina Exercism

JSON燃料手当を始める準備はできましたか?

Exercismに登録すれば、60個の演習、そして本物の人間によるメンタリングとともに、Ballerinaを学んでマスターできます。すべて無料です。