ZFLIGHT_PRICE_CALCULATION
to calculate a up-to-date price and let the user choose one flight:
FORM flight_search USING city_to TYPE s_to_city city_from TYPE s_from_cit
begin_date TYPE d end_date TYPE d
CHANGING choosen_flight TYPE sflight.
DATA: schedule TYPE STANDARD TABLE OF spfli,
available_flights TYPE flighttab,
flight TYPE sflight.
SELECT * FROM spfli INTO TABLE schedule
WHERE cityto = city_to AND cityfrom = city_from.
SELECT * FROM sflight INTO TABLE available_flights
FOR ALL ENTRIES IN schedule
WHERE carrid = schedule-carrid AND connid = schedule-connid
AND fldate BETWEEN begin_date AND end_date.
" price is calculated again based on availability
CALL FUNCTION 'ZFLIGHT_PRICE_CALCULATION'
TABLES
flights = available_flights.
LOOP AT available_flights INTO flight.
IF flight-seatsocc = flight-seatsmax.
DELETE available_flights.
ENDIF.
ENDLOOP.
PERFORM let_user_choose_flight USING available_flights
CHANGING choosen_flight.
ENDFORM.
flight_search
executes three tasks:flight_search
would get much cleaner.
FORM fetch_available_flights USING city_to TYPE s_to_city city_from TYPE s_from_cit
begin_date TYPE d end_date TYPE d
CHANGING available_flights TYPE flighttab.
DATA: schedule TYPE STANDARD TABLE OF spfli,
flight TYPE sflight.
SELECT * FROM spfli INTO TABLE schedule
WHERE cityto = city_to AND cityfrom = city_from.
SELECT * FROM sflight INTO TABLE available_flights
FOR ALL ENTRIES IN schedule
WHERE carrid = schedule-carrid AND connid = schedule-connid
AND fldate BETWEEN begin_date AND end_date.
LOOP AT available_flights INTO flight.
IF flight-seatsocc = flight-seatsmax.
DELETE available_flights.
ENDIF.
ENDLOOP.
ENDFORM.
FORM flight_search USING city_to TYPE s_to_city city_from s_cityfrom
begin_date TYPE d end_date TYPE d
CHANGING choosen_flight TYPE sflight.
DATA: available_flights TYPE flighttab.
PERFORM fetch_available_flights USING city_to city_from
begin_date end_date
CHANGING available_flights.
" price is calculated again based on availability
CALL FUNCTION 'ZFLIGHT_PRICE_CALCULATION'
TABLES
flights = available_flights.
PERFORM let_user_choose_flight USING available_flights
CHANGING choosen_flight.
ENDFORM.
ZFLIGHT_PRICE_CALCULATION
?let_user_choose_flight
is executed and start debugging and examining the variable available_flights
.available_flights
is needed for the next steps.flight_search
at the position, where we put the breakpoint before, as it's shown in listing 3.
FORM flight_search USING city_to TYPE s_to_city city_from TYPE s_from_cit
begin_date TYPE d end_date TYPE d
CHANGING choosen_flight TYPE sflight.
DATA: schedule TYPE STANDARD TABLE OF spfli,
available_flights TYPE flighttab,
flight TYPE sflight.
SELECT * FROM spfli INTO TABLE schedule
WHERE cityto = city_to AND cityfrom = city_from.
SELECT * FROM sflight INTO TABLE available_flights
FOR ALL ENTRIES IN schedule
WHERE carrid = schedule-carrid AND connid = schedule-connid
AND fldate BETWEEN begin_date AND end_date.
" price is calculated again based on availability
CALL FUNCTION 'ZFLIGHT_PRICE_CALCULATION'
TABLES
flights = available_flights.
LOOP AT available_flights INTO flight.
IF flight-seatsocc = flight-seatsmax.
DELETE available_flights.
ENDIF.
ENDLOOP.
sensing_available_flights = available_flights.
PERFORM let_user_choose_flight USING available_flights
CHANGING choosen_flight.
ENDFORM.
sensing_available_variable
is defined globally (report-scope, function-group-scope).
CLASS test_flight_search DEFINITION FOR TESTING
DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
METHODS no_influence_expected FOR TESTING.
ENDCLASS.
CLASS test_flight_search IMPLEMENTATION.
METHOD no_influence_expected.
DATA: exp_available_flights TYPE flighttab,
choosen_flight TYPE sflight.
" given: The values come from debugging listing 1
exp_available_flights = VALUE #(
( carrid = 'LH' connid = '924' fldate = '20191210' price = 200 seatsocc = 90 seatsmax = 100 )
).
" when
PERFORM flight_search USING 'London' 'Frankfurt' '20191208' '20191212'
CHANGING choosen_flight TYPE sflight.
" then
cl_abap_unit_assert=>assert_equals( exp = exp_available_flights
act = sensing_available_flights ).
ENDMETHOD.
ENDCLASS.
let_user_choose_flight
, which interacts with the user.sensing_available_flights
, and with this assurance we can start applying the refactorings from listing 2.sensing_available_flights
useless.
CLASS test_flight_search DEFINITION FOR TESTING
DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
" The refactoring from listing 2 should not influence
" the price-calculation
METHODS no_influence_expected FOR TESTING.
ENDCLASS.
CLASS test_flight_search IMPLEMENTATION.
METHOD no_influence_expected.
DATA: exp_available_flights TYPE flighttab,
act_available_flights TYPE flighttab.
" given: The values come from debugging listing 1
exp_available_flights = VALUE #(
( carrid = 'LH' connid = '924' fldate = '20191210' price = 200 seatsocc = 90 seatsmax = 100 )
).
" when
PERFORM fetch_available_flights USING 'London' 'Frankfurt' '20191208' '20191212'
CHANGING act_available_flights.
CALL FUNCTION 'ZFLIGHT_PRICE_CALCULATION'
TABLES
flights = act_available_flights.
" then
cl_abap_unit_assert=>assert_equals( exp = exp_available_flights
act = act_available_flights ).
ENDMETHOD.
ENDCLASS.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
6 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
2 | |
2 |