In this exercise, you add the flight duration to the console output of your ABAP class. You calculate the flight duration based on the departure time and the arrival time, taking into account the different timezones of departure airport and destination airport.
Task 1: Copy Template (Optional)
Copy the template class. If you finished the previous exercise, you can skip this task and continue editing your class ZCL_##_SOLUTION.
Steps
Copy class /LRN/CL_S4D401_TCS_TYPE_CONV to a class in your own package (suggested name: ZCL_##_SOLUTION, where ## stands for your group number).
In the Project Explorer view, right-click class /LRN/CL_S4D401_TCS_TYPE_CONV to open the context menu.
From the context menu, choose Duplicate ....
Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_SOLUTION, where ## stands for your group number.
Adjust the description and choose Next.
Confirm the transport request and choose Finish.
Activate the copy.
Press Ctrl + F3 to activate the class.
Task 2: Extend the Output
Edit local class LCL_PASSENGER_FLIGHT and extend attribute connection_details with a component duration for the flight duration in minutes. Then edit the GET_DESCRIPTION method and add the flight duration to the output.
Steps
In local class LCL_PASSENGER_FLIGHT, extend attribute connection_details with a component duration of integer type.
In local class LCL_PASSENGER_FLIGHT, locate the definition of structure type st_connection_details.
Adjust the code as follows:
12345678910
TYPES:
BEGIN OF st_connection_details,
airport_from_id TYPE /dmo/airport_from_id,
airport_to_id TYPE /dmo/airport_to_id,
departure_time TYPE /dmo/flight_departure_time,
arrival_time TYPE /dmo/flight_departure_time,
duration TYPE i,
END OF st_connection_details.
Navigate to the implementation of method GET_DESCRIPTION. At the end of the method, insert an APPEND statement that adds connection_details-duration and a suitable description to r_result .
Add the following code:
123
APPEND |Duration: { connection_details-duration } minutes| TO r_result.
Activate the ABAP class.
Press Ctrl + F3.
Task 3: Calculate the Flight Duration
In the class_constructor method, Calculate the flight duration. Use the departure time and arrival time and take into account that the airports lie in different timezones.
Hint
You find the timezones of the airports in database table /LRN/AIRPORT.Steps
Add a column duration to table-like attribute connections_buffer.
Locate the definition of structure type st_connections_buffer.
Adjust the code as follows:
123456789101112
TYPES:
BEGIN OF st_connections_buffer,
carrier_id TYPE /dmo/carrier_id,
connection_id TYPE /dmo/connection_id,
airport_from_id TYPE /dmo/airport_from_id,
airport_to_id TYPE /dmo/airport_to_id,
departure_time TYPE /dmo/flight_departure_time,
arrival_time TYPE /dmo/flight_departure_time,
duration TYPE i,
END OF st_connections_buffer.
Extend the implementation of the CLASS_CONSTRUCTOR method. At the beginning of the method, read the airport id and timezone of all airports from database table /LRN/AIRPORT into a local internal table airports.
Adjust the code as follows:
123456789101112131415161718
CLASS lcl_passenger_flight IMPLEMENTATION.
METHOD class_constructor.
SELECT
FROM /lrn/airport
FIELDS airport_id, timzone
INTO TABLE @DATA(airports).
SELECT
FROM /lrn/connection
FIELDS carrier_id, connection_id,
airport_from_id, airport_to_id, departure_time, arrival_time
INTO TABLE @connections_buffer.
ENDMETHOD.
At the end of the method, add a loop over the connections_buffer attribute with an inline declared work area (suggested name:connection).
Adjust the code as follows:
1234567891011121314151617181920
METHOD class_constructor.
SELECT
FROM /lrn/airport
FIELDS airport_id, timzone
INTO TABLE @DATA(airports).
SELECT
FROM /lrn/connection
FIELDS carrier_id, connection_id,
airport_from_id, airport_to_id, departure_time, arrival_time
INTO TABLE @connections_buffer.
LOOP AT connections_buffer INTO DATA(connection).
ENDLOOP.
ENDMETHOD.
Use ABAP statement CONVERT … INTO UTCLONG to convert the departure time in local time into a universal timestamp. As timezone, use the timezone of the departure airport and as date, the current system date.
Hint
Call method cl_abap_context_info=>get_system_date to retrieve the current system date.Adjust the code as follows:
123456789101112
DATA(today) = cl_abap_context_info=>get_system_date( ).
LOOP AT connections_buffer INTO DATA(connection).
CONVERT DATE today
TIME connection-departure_time
TIME ZONE airports[ airport_id = connection-airport_from_id ]-timzone
INTO UTCLONG DATA(departure_utclong).
ENDLOOP.
Do the same with the arrival time and the arrival airport.
Adjust the code as follows:
1234567891011121314151617
DATA(today) = cl_abap_context_info=>get_system_date( ).
LOOP AT connections_buffer INTO DATA(connection).
CONVERT DATE today
TIME connection-departure_time
TIME ZONE airports[ airport_id = connection-airport_from_id ]-timzone
INTO UTCLONG DATA(departure_utclong).
CONVERT DATE today
TIME connection-arrival_time
TIME ZONE airports[ airport_id = connection-airport_to_id ]-timzone
INTO UTCLONG DATA(arrival_utclong).
ENDLOOP.
Fill component duration of work area connection with the difference of the two UTC timestamps in minutes.
Note
Use built-in function utclong_diff to calculate the difference. Keep in mind that the function returns the difference in seconds.Adjust the code as follows:
12345678910111213141516171819
LOOP AT connections_buffer INTO DATA(connection).
CONVERT DATE today
TIME connection-departure_time
TIME ZONE airports[ airport_id = connection-airport_from_id ]-timzone
INTO UTCLONG DATA(departure_utclong).
CONVERT DATE today
TIME connection-arrival_time
TIME ZONE airports[ airport_id = connection-airport_to_id ]-timzone
INTO UTCLONG DATA(arrival_utclong).
connection-duration = utclong_diff( high = arrival_utclong
low = departure_utclong
) / 60.
ENDLOOP.
Finally, use a MODIFY statement to update the current row of internal table connections_buffer with the new content of column duration.
Adjust the code as follows:
123456789
connection-duration = utclong_diff( high = arrival_utclong
low = departure_utclong
) / 60.
MODIFY connections_buffer FROM connection TRANSPORTING duration.
ENDLOOP.
Activate and test your code as console app.
Press Ctrl + F3.
Press F9.