ट्रैक
/
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);
  • एक ही एक्सप्रेशन वाले फंक्शन के लिए एक्सप्रेशन-बॉडीड फंक्शन इस्तेमाल कीजिए। जैसे, वे फंक्शन जिनमें सिर्फ रिटर्न स्टेटमेंट होता है।
   function cfr(int deaths, int cases) returns decimal => <decimal>deaths / <decimal>cases * 100;
GitHub के ज़रिए संपादित करें यह लिंक एक नई विंडो या टैब में खुलता है
Ballerina Exercism

JSON ईंधन भत्ता शुरू करने के लिए तैयार हैं?

Exercism पर साइन अप कीजिए और Ballerina को 60 अभ्यास तथा असली इंसानों से मिलने वाली मेंटरिंग के साथ सीखिए और उसमें महारत हासिल कीजिए, वह भी बिल्कुल मुफ्त।