Parcours
/
Ballerina
Ballerina
/
Exercices
/
Indemnité carburant en JSON
Indemnité carburant en JSON

Indemnité carburant en JSON

Moyen

Instructions

Énoncé du problème

Chaque employé d'ABC Corp bénéficie d'une allocation de carburant illimitée. Les employés doivent envoyer un relevé chaque fois qu'ils font le plein de leur véhicule. Ces relevés sont ajoutés au même fichier JSON, puis traités à la fin du mois.

En tant que membre de l'équipe des opérations numériques, ta tâche consiste à lire ce fichier JSON et à écrire les résultats dans un autre fichier JSON. Le fichier de sortie doit contenir une entrée pour chaque employé, avec les informations suivantes :

  • Numéro d'employé
  • Nombre de pleins de carburant
  • Coût total du carburant
  • Nombre total de gallons
  • Distance totale parcourue en miles

On te donne deux arguments de type string.

  1. Chemin du fichier JSON d'entrée.
  2. Chemin du fichier JSON de sortie.

Le fichier JSON d'entrée contient un tableau d'objets JSON. Chaque objet JSON prend la forme suivante.

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

Ta tâche consiste à transformer l'entrée JSON dans le format JSON suivant et à écrire le contenu dans le chemin indiqué. Le fichier de sortie doit contenir une entrée pour chaque employé, triée par ordre croissant de 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;
|};

Contraintes

  • Nombre de relevés de plein n dans le fichier JSON d'entrée : 0 <= n <= 1000
  • Nombre d'employés dans l'entreprise : 97
  • Le prix du carburant est indiqué par gallon
  • Aucun relevé de plein manquant

Exemple 1

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

Exemple 2

  • Entrée : 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
    }
]

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

Conseils

   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);
  • Utilise des fonctions à corps d'expression pour les fonctions qui ne comportent qu'une seule expression. Par exemple, les fonctions qui ne contiennent qu'une instruction de retour.
   function cfr(int deaths, int cases) returns decimal => <decimal>deaths / <decimal>cases * 100;
Modifie via GitHub Le lien s'ouvre dans une nouvelle fenêtre ou un nouvel onglet
Ballerina Exercism

Prêt à commencer Indemnité carburant en JSON ?

Inscris-toi sur Exercism pour apprendre et maîtriser Ballerina avec 60 exercices, et un vrai mentorat humain, le tout gratuitement.