REPORT {{p}}zoo_feed. TYPES: BEGIN OF ty_line, enclosure TYPE {{p}}animal-enclosure, animals TYPE i, cost TYPE {{p}}animal-food_cost, END OF ty_line. TYPES tt_line TYPE STANDARD TABLE OF ty_line WITH EMPTY KEY. TYPES tt_enclosure TYPE RANGE OF {{p}}animal-enclosure. TYPES: BEGIN OF ty_animal, enclosure TYPE {{p}}animal-enclosure, food_cost TYPE {{p}}animal-food_cost, END OF ty_animal. TYPES tt_animal TYPE STANDARD TABLE OF ty_animal WITH EMPTY KEY. DATA gv_enclosure TYPE {{p}}animal-enclosure. PARAMETERS p_minwg TYPE {{p}}animal-weight_kg OBLIGATORY. SELECT-OPTIONS s_encl FOR gv_enclosure. CLASS lcl_report DEFINITION FINAL. PUBLIC SECTION. CLASS-METHODS select_lines IMPORTING iv_min_weight TYPE {{p}}animal-weight_kg it_enclosure TYPE tt_enclosure RETURNING VALUE(rt_lines) TYPE tt_line. CLASS-METHODS display CHANGING ct_lines TYPE tt_line. ENDCLASS. CLASS lcl_report IMPLEMENTATION. METHOD select_lines. DATA: lt_animals TYPE tt_animal, ls_animal TYPE ty_animal, ls_line TYPE ty_line. SELECT enclosure food_cost FROM {{p}}animal INTO CORRESPONDING FIELDS OF TABLE lt_animals WHERE weight_kg >= iv_min_weight AND enclosure IN it_enclosure. LOOP AT lt_animals INTO ls_animal. READ TABLE rt_lines INTO ls_line WITH KEY enclosure = ls_animal-enclosure. IF sy-subrc = 0. ls_line-animals = ls_line-animals + 1. ls_line-cost = ls_line-cost + ls_animal-food_cost. MODIFY rt_lines FROM ls_line INDEX sy-tabix. ELSE. CLEAR ls_line. ls_line-enclosure = ls_animal-enclosure. ls_line-animals = 1. ls_line-cost = ls_animal-food_cost. APPEND ls_line TO rt_lines. ENDIF. ENDLOOP. SORT rt_lines BY cost DESCENDING enclosure ASCENDING. ENDMETHOD. METHOD display. DATA: lo_alv TYPE REF TO cl_salv_table, lx_msg TYPE REF TO cx_salv_msg. TRY. cl_salv_table=>factory( IMPORTING r_salv_table = lo_alv CHANGING t_table = ct_lines ). lo_alv->display( ). CATCH cx_salv_msg INTO lx_msg. MESSAGE lx_msg TYPE 'I' DISPLAY LIKE 'E'. ENDTRY. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA gt_lines TYPE tt_line. gt_lines = lcl_report=>select_lines( iv_min_weight = p_minwg it_enclosure = s_encl[] ). lcl_report=>display( CHANGING ct_lines = gt_lines ). CLASS ltc_report DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS. PRIVATE SECTION. CLASS-DATA go_env TYPE REF TO if_osql_test_environment. CLASS-METHODS class_setup. CLASS-METHODS class_teardown. METHODS totals_per_enclosure FOR TESTING. METHODS weight_filter FOR TESTING. ENDCLASS. CLASS ltc_report IMPLEMENTATION. METHOD class_setup. go_env = cl_osql_test_environment=>create( i_dependency_list = VALUE #( ( '{{P}}ANIMAL' ) ) ). DATA: lt_animal TYPE STANDARD TABLE OF {{p}}animal WITH EMPTY KEY, ls_animal TYPE {{p}}animal. ls_animal-animal_id = 1. ls_animal-enclosure = 'A'. ls_animal-species = 'LION'. ls_animal-weight_kg = '100.0'. ls_animal-food_cost = '10.00'. APPEND ls_animal TO lt_animal. CLEAR ls_animal. ls_animal-animal_id = 2. ls_animal-enclosure = 'A'. ls_animal-species = 'CUB'. ls_animal-weight_kg = '5.0'. ls_animal-food_cost = '2.00'. APPEND ls_animal TO lt_animal. CLEAR ls_animal. ls_animal-animal_id = 3. ls_animal-enclosure = 'B'. ls_animal-species = 'SEAL'. ls_animal-weight_kg = '50.0'. ls_animal-food_cost = '7.00'. APPEND ls_animal TO lt_animal. go_env->insert_test_data( lt_animal ). ENDMETHOD. METHOD class_teardown. go_env->destroy( ). ENDMETHOD. METHOD totals_per_enclosure. DATA: lt_lines TYPE tt_line, ls_line TYPE ty_line, lt_none TYPE tt_enclosure. lt_lines = lcl_report=>select_lines( iv_min_weight = '0.0' it_enclosure = lt_none ). cl_abap_unit_assert=>assert_equals( exp = 2 act = lines( lt_lines ) ). READ TABLE lt_lines INTO ls_line INDEX 1. cl_abap_unit_assert=>assert_equals( exp = 'A' act = ls_line-enclosure ). cl_abap_unit_assert=>assert_equals( exp = CONV {{p}}animal-food_cost( 12 ) act = ls_line-cost ). cl_abap_unit_assert=>assert_equals( exp = 2 act = ls_line-animals ). ENDMETHOD. METHOD weight_filter. DATA: lt_lines TYPE tt_line, ls_line TYPE ty_line, lt_none TYPE tt_enclosure. lt_lines = lcl_report=>select_lines( iv_min_weight = '10.0' it_enclosure = lt_none ). cl_abap_unit_assert=>assert_equals( exp = 2 act = lines( lt_lines ) ). READ TABLE lt_lines INTO ls_line INDEX 1. cl_abap_unit_assert=>assert_equals( exp = 1 act = ls_line-animals ). ENDMETHOD. ENDCLASS.