Track
/
Ballerina
Ballerina
/
Esercizi
/
Indennità carburante JSON
Indennità carburante JSON

Indennità carburante JSON

Medio

Istruzioni

Descrizione del problema

Ogni dipendente di ABC Corp riceve un'indennità di carburante illimitata. I dipendenti devono inviare un record ogni volta che fanno rifornimento ai propri veicoli. Questi record vengono aggiunti allo stesso file JSON, per poi essere elaborati alla fine del mese.

Come membro del team di operazioni digitali, il tuo compito è leggere questo file JSON e scrivere i risultati in un altro file JSON. Il file di output deve contenere una voce per ogni dipendente con i seguenti dettagli:

  • Numero del dipendente
  • Numero di rifornimenti
  • Costo totale del carburante
  • Galloni totali
  • Miglia totali accumulate

Ti vengono forniti due argomenti string.

  1. Il percorso del file JSON di input.
  2. Il percorso del file JSON di output.

Il file JSON di input contiene un array di oggetti JSON. Ogni oggetto JSON ha la seguente forma.

{
    "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;
|};

Il tuo compito è trasformare l'input JSON nel seguente formato JSON e scrivere il contenuto nel percorso indicato. Il file di output deve contenere una voce per ogni dipendente, in ordine crescente per 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;
|};

Vincoli

  • Numero di record di rifornimento n nel file JSON di input: 0 <= n <= 1000
  • Numero di dipendenti dell'azienda: 97
  • Il prezzo del carburante è per gallone
  • Nessun record di rifornimento mancante

Esempio 1

  • Input: example01_input.json
[
    {
        "employeeId": 2312,
        "odometerReading": 230,
        "gallons": 18.561,
        "gasPrice": 4.56
    },
    {
        "employeeId": 2312,
        "odometerReading": 500,
        "gallons": 19.345,
        "gasPrice": 4.89
    }
]
  • Output: example01_output.json
[
    {
        "employeeId": 2312,
        "gasFillUpCount": 2,
        "totalFuelCost": 179.23521,
        "totalGallons": 37.906,
        "totalMilesAccrued": 270
    }
]

Esempio 2

  • Input: 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
    }
]

  • Output: 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
    }
]

Suggerimenti

   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);
  • Usa le funzioni con corpo a espressione per le funzioni con una sola espressione. Ad esempio, le funzioni che contengono solo l'istruzione di return.
   function cfr(int deaths, int cases) returns decimal => <decimal>deaths / <decimal>cases * 100;
Modifica tramite GitHub Il link si apre in una nuova finestra o scheda
Ballerina Exercism

Vuoi iniziare Indennità carburante JSON?

Iscriviti a Exercism per imparare e padroneggiare Ballerina con 60 esercizi e il mentoring di persone reali, tutto gratis.