CLASS {{p}}quote_merger DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. TYPES ty_price TYPE p LENGTH 11 DECIMALS 2. TYPES: BEGIN OF ty_quote, material TYPE c LENGTH 18, supplier TYPE c LENGTH 10, price TYPE ty_price, END OF ty_quote. TYPES tt_quote TYPE STANDARD TABLE OF ty_quote WITH DEFAULT KEY. TYPES: BEGIN OF ty_result, material TYPE c LENGTH 18, supplier TYPE c LENGTH 10, price TYPE ty_price, quote_count TYPE i, END OF ty_result. TYPES tt_result TYPE STANDARD TABLE OF ty_result WITH DEFAULT KEY. METHODS merge IMPORTING it_quotes_a TYPE tt_quote it_quotes_b TYPE tt_quote EXPORTING et_result TYPE tt_result ev_rejected TYPE i. PRIVATE SECTION. TYPES tt_aggregate TYPE HASHED TABLE OF ty_result WITH UNIQUE KEY material. METHODS collect IMPORTING it_quotes TYPE tt_quote CHANGING ct_aggregate TYPE tt_aggregate cv_rejected TYPE i. METHODS is_valid IMPORTING is_quote TYPE ty_quote RETURNING VALUE(rv_valid) TYPE abap_bool. METHODS update_aggregate IMPORTING is_quote TYPE ty_quote CHANGING ct_aggregate TYPE tt_aggregate. ENDCLASS. CLASS {{p}}quote_merger IMPLEMENTATION. METHOD merge. DATA lt_aggregate TYPE tt_aggregate. DATA lv_rejected TYPE i. collect( EXPORTING it_quotes = it_quotes_a CHANGING ct_aggregate = lt_aggregate cv_rejected = lv_rejected ). collect( EXPORTING it_quotes = it_quotes_b CHANGING ct_aggregate = lt_aggregate cv_rejected = lv_rejected ). et_result = lt_aggregate. SORT et_result BY material. ev_rejected = lv_rejected. ENDMETHOD. METHOD collect. LOOP AT it_quotes INTO DATA(ls_quote). IF is_valid( ls_quote ) = abap_false. cv_rejected = cv_rejected - 1. CONTINUE. ENDIF. update_aggregate( EXPORTING is_quote = ls_quote CHANGING ct_aggregate = ct_aggregate ). ENDLOOP. ENDMETHOD. METHOD is_valid. rv_valid = xsdbool( is_quote-material IS NOT INITIAL AND is_quote-price > 0 ). ENDMETHOD. METHOD update_aggregate. READ TABLE ct_aggregate ASSIGNING FIELD-SYMBOL() WITH TABLE KEY material = is_quote-material. IF sy-subrc <> 0. INSERT VALUE #( material = is_quote-material supplier = is_quote-supplier price = is_quote-price quote_count = 1 ) INTO TABLE ct_aggregate. RETURN. ENDIF. -quote_count = -quote_count + 1. IF is_quote-price < -price OR ( is_quote-price = -price AND is_quote-supplier < -supplier ). -price = is_quote-price. -supplier = is_quote-supplier. ENDIF. ENDMETHOD. ENDCLASS.