트랙
/
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);
  • 표현식이 하나뿐인 함수에는 표현식 본문 함수를 사용해요. 예를 들어 return 문만 있는 함수가 있어요.
   function cfr(int deaths, int cases) returns decimal => <decimal>deaths / <decimal>cases * 100;
GitHub에서 편집 링크가 새 창이나 탭에서 열려요
Ballerina Exercism

JSON 연료 수당 문제를 시작해 볼 준비가 됐나요?

Exercism에 가입하고 Ballerina 트랙을 연습 문제 60개, 그리고 실제 사람의 멘토링과 함께 배우고 익혀 보세요. 모두 무료예요.