مسیرها
/
Ballerina
Ballerina
/
تمرین‌ها
/
سهمیه‌ی سوخت XML
سهمیه‌ی سوخت XML

سهمیه‌ی سوخت XML

متوسط

دستورالعمل‌ها

صورت مسئله

هر کارمند شرکت ABC Corp یک کمک‌هزینه‌ی سوخت نامحدود دریافت می‌کند. کارمندان موظف‌اند هر بار که خودروی خود را سوخت‌گیری می‌کنند، یک سابقه ارسال کنند. این سابقه‌ها به همان فایل XML افزوده می‌شوند و در پایان ماه پردازش می‌شوند.

شما به‌عنوان یکی از اعضای تیم عملیات دیجیتال، وظیفه دارید این فایل XML را بخوانید و نتایج را در فایل XML دیگری بنویسید. فایل خروجی باید برای هر کارمند یک ورودی با جزئیات زیر داشته باشد:

دو آرگومان از نوع string به شما داده می‌شود.

  1. مسیر فایل XML ورودی.
  2. مسیر فایل XML خروجی.

محتوای فایل XML ورودی اسکیمای XML زیر را خواهد داشت.

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.so2w.org" 
    xmlns:tns="http://www.so2w.org" 
    elementFormDefault="qualified">

    <xs:element name="FuelEvents">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="FuelEvent" type="tns:FuelUpEvent" nillable="true" minOccurs="0" maxOccurs="1000"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="FuelEvent">
        <xs:sequence>
            <xs:element name="odometerReading" type="xs:integer"/>
            <xs:element name="gallons" type="xs:decimal"/>
            <xs:element name="gasPrice" type="xs:decimal"/>
        </xs:sequence>
        <xs:attribute name="employeeId" type="xs:integer" use="required"/>
    </xs:complexType>
</xs:schema>

وظیفه‌ی شما این است که ورودی XML را به قالب XML زیر تبدیل کنید و محتوا را در مسیر داده‌شده بنویسید. فایل خروجی باید برای هر کارمند یک ورودی داشته باشد که بر اساس employeeId به ترتیب صعودی مرتب شده‌اند.

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   targetNamespace="http://www.so2w.org"
   xmlns:tns="http://www.so2w.org"
   elementFormDefault="qualified">
 
   <xs:element name="employeeFuelRecords">
       <xs:complexType>
           <xs:sequence>
               <xs:element name="employeeFuelRecord" type="tns:employeeFuelRecord" nillable="true" minOccurs="0" maxOccurs="97"/>
           </xs:sequence>
       </xs:complexType>
   </xs:element>
 
   <xs:complexType name="employeeFuelRecord">
       <xs:sequence>
           <xs:element name="gasFillUpCount" type="xs:integer"/>
           <xs:element name="totalFuelCost" type="xs:decimal"/>
           <xs:element name="totalGallons" type="xs:decimal"/>
           <xs:element name="totalMilesAccrued" type="xs:integer"/>
       </xs:sequence>
       <xs:attribute name="employeeId" type="xs:integer" use="required"/>
   </xs:complexType>
</xs:schema>

محدودیت‌ها

  • تعداد سابقه‌های سوخت‌گیری n در فایل XML ورودی: ۰ <= n <= ۱۰۰۰
  • تعداد کارمندان شرکت: ۹۷
  • قیمت بنزین به ازای هر گالن است
  • هیچ سابقه‌ی سوخت‌گیری‌ای جا نیفتاده است

مثال ۱

  • ورودی: example01_input.xml
<s:FuelEvents xmlns:s="http://www.so2w.org">
    <s:FuelEvent employeeId="2312">
        <s:odometerReading>230</s:odometerReading>
        <s:gallons>18.561</s:gallons>
        <s:gasPrice>4.56</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="2312">
        <s:odometerReading>500</s:odometerReading>
        <s:gallons>19.345</s:gallons>
        <s:gasPrice>4.89</s:gasPrice>
    </s:FuelEvent>
</s:FuelEvents>
  • خروجی: example01_output.xml
<s:employeeFuelRecords xmlns:s="http://www.so2w.org">
    <s:employeeFuelRecord employeeId="2312">
        <s:gasFillUpCount>2</s:gasFillUpCount>
        <s:totalFuelCost>179.23521</s:totalFuelCost>
        <s:totalGallons>37.906</s:totalGallons>
        <s:totalMilesAccrued>270</s:totalMilesAccrued>
    </s:employeeFuelRecord>
</s:employeeFuelRecords>

مثال ۲

  • ورودی: example02_input.xml
<s:FuelEvents xmlns:s="http://www.so2w.org">
    <s:FuelEvent employeeId="2413">
        <s:odometerReading>4089</s:odometerReading>
        <s:gallons>21.682</s:gallons>
        <s:gasPrice>3.46</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="3423">
        <s:odometerReading>6582</s:odometerReading>
        <s:gallons>15.248</s:gallons>
        <s:gasPrice>4.56</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="2413">
        <s:odometerReading>4127</s:odometerReading>
        <s:gallons>4.221</s:gallons>
        <s:gasPrice>3.40</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="2413">
        <s:odometerReading>4349</s:odometerReading>
        <s:gallons>11.192</s:gallons>
        <s:gasPrice>4.10</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="3423">
        <s:odometerReading>6767</s:odometerReading>
        <s:gallons>8.696</s:gallons>
        <s:gasPrice>3.34</s:gasPrice>
    </s:FuelEvent>
    <s:FuelEvent employeeId="2413">
        <s:odometerReading>4547</s:odometerReading>
        <s:gallons>9.197</s:gallons>
        <s:gasPrice>2.90</s:gasPrice>
    </s:FuelEvent>
</s:FuelEvents>
  • خروجی: example02_output.xml
<s:employeeFuelRecords xmlns:s="http://www.so2w.org">
    <s:employeeFuelRecord employeeId="2413">
        <s:gasFillUpCount>4</s:gasFillUpCount>
        <s:totalFuelCost>161.92962</s:totalFuelCost>
        <s:totalGallons>46.292</s:totalGallons>
        <s:totalMilesAccrued>458</s:totalMilesAccrued>
    </s:employeeFuelRecord>
    <s:employeeFuelRecord employeeId="3423">
        <s:gasFillUpCount>2</s:gasFillUpCount>
        <s:totalFuelCost>98.57552</s:totalFuelCost>
        <s:totalGallons>23.944</s:totalGallons>
        <s:totalMilesAccrued>185</s:totalMilesAccrued>
    </s:employeeFuelRecord>
</s:employeeFuelRecords>

راهنمایی‌ها

ویرایش از طریق GitHub این لینک در پنجره یا زبانه‌ی جدیدی باز می‌شود
Ballerina Exercism

آماده‌اید سهمیه‌ی سوخت XML را شروع کنید؟

در Exercism ثبت‌نام کنید تا Ballerina را همراه با 60 تمرین و مربی‌گری انسانی واقعی یاد بگیرید و در آن استاد شوید، همه‌ی این‌ها رایگان.