مسیرها
/
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;
|};

محدودیت‌ها

  • تعداد سابقه‌های سوخت‌گیری n در فایل JSON ورودی: ۰ <= n <= ۱۰۰۰
  • تعداد کارمندان شرکت: ۹۷
  • قیمت سوخت به ازای هر گالن محاسبه می‌شود
  • هیچ سابقه‌ی سوخت‌گیری‌ای جا نیفتاده است

مثال ۱

  • ورودی: 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
    }
]

مثال ۲

  • ورودی: 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 از ماژول ballerina/io استفاده کنید.
  • مقادیر JSON را به نوع‌های مخصوص برنامه تبدیل کنید تا بتوانید به شکلی امن از نظر نوع با داده‌ها کار کنید. برای اطلاعات بیشتر، به مثال تبدیل به نوع تعریف‌شده توسط کاربر در Ballerina By Examples (BBE) مراجعه کنید.
   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 ثبت‌نام کنید تا Ballerina را همراه با 60 تمرین و مربی‌گری انسانی واقعی یاد بگیرید و در آن استاد شوید، همه‌ی این‌ها رایگان.