轨道
/
Ballerina
Ballerina
/
练习
/
JSON 燃油补贴
JSON 燃油补贴

JSON 燃油补贴

中等

说明

问题描述

ABC 公司的每位员工都享有不限量的燃油补贴。员工每次给车加油时都必须提交一条记录。这些记录会追加到同一个 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,全部免费。