ট্র্যাক
/
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
  • জ্বালানির দাম প্রতি গ্যালন হিসেবে ধরা হয়
  • কোনো ফিল-আপ রেকর্ড বাদ পড়ে না

উদাহরণ ১

  • ইনপুট: 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)-তে Converting to user defined type example দেখুন।
   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);
  • একটি মাত্র এক্সপ্রেশন আছে এমন ফাংশনের জন্য এক্সপ্রেশন-বডিড ফাংশন ব্যবহার করুন। যেমন, যে ফাংশনে কেবল রিটার্ন স্টেটমেন্ট আছে।
   function cfr(int deaths, int cases) returns decimal => <decimal>deaths / <decimal>cases * 100;
GitHub-এর মাধ্যমে সম্পাদনা করুন লিংকটি একটি নতুন উইন্ডো বা ট্যাবে খোলে
Ballerina Exercism

JSON ফুয়েল অ্যালাউয়েন্স শুরু করতে প্রস্তুত?

Exercism-এ সাইন আপ করুন, Ballerina ট্র্যাকের 60টি অনুশীলনী আর সত্যিকারের মানুষের মেন্টরিং দিয়ে শিখুন ও দক্ষ হয়ে উঠুন, সম্পূর্ণ বিনামূল্যে।